-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
39 lines (33 loc) · 1.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from random import randint
from flask import Flask, request
import logging
import datetime
from datetime import timezone
from logging.config import dictConfig
from pythonjsonlogger import jsonlogger
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.logging import LoggingInstrumentor
##### LOG CONFIGURATION #####
class CustomJsonFormatter(jsonlogger.JsonFormatter):
def add_fields(self, log_record, record, message_dict):
super(CustomJsonFormatter, self).add_fields(log_record, record, message_dict)
log_record['timestamp'] = datetime.datetime.now(timezone.utc)
log_record['level'] = record.levelname
# traceID=<id> is the default formated used by tempo in grafana cloud.
log_record['traceID'] = f"traceID={record.otelTraceID}"
logger = logging.getLogger()
logger.setLevel('INFO')
logHandler = logging.StreamHandler()
formatter = CustomJsonFormatter('%(timestamp)s %(level)s %(name)s %(message)s')
logHandler.setFormatter(formatter)
logger.addHandler(logHandler)
#############################
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
LoggingInstrumentor().instrument()
@app.route("/rolldice")
def roll_dice():
logger.info("This is an example log message.")
return str(do_roll())
def do_roll():
return randint(1, 6)