File tree Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Expand file tree Collapse file tree 2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,9 @@ containers:
9
9
dev :
10
10
poetry run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory --reload
11
11
12
+ otel :
13
+ OTEL_SERVICE_NAME=bootstrap-fastapi OTEL_TRACES_EXPORTER=none OTEL_METRICS_EXPORTER=none OTEL_LOGS_EXPORTER=none poetry run opentelemetry-instrument uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory
14
+
12
15
run :
13
16
poetry run uvicorn http_app:create_app --host 0.0.0.0 --port 8000 --factory
14
17
Original file line number Diff line number Diff line change 1
1
from typing import Union
2
2
3
3
from fastapi import FastAPI , Request
4
+ from opentelemetry .instrumentation .asgi import OpenTelemetryMiddleware
4
5
from starlette .responses import JSONResponse
5
6
from starlette_prometheus import PrometheusMiddleware , metrics
6
7
from structlog import get_logger
@@ -24,9 +25,20 @@ def create_app(
24
25
25
26
init_routes (app )
26
27
28
+ """
29
+ OpenTelemetry prometheus exporter does not work together with automatic
30
+ instrumentation, for now we keep the prometheus middleware even if
31
+ having 2 different middlewares will add overhead.
32
+ """
27
33
app .add_middleware (PrometheusMiddleware )
28
34
app .add_route ("/metrics/" , metrics )
29
35
36
+ """
37
+ OpenTelemetry middleware has to be the last one to make sure the
38
+ tracing data handling is the outermost logic
39
+ """
40
+ app .add_middleware (OpenTelemetryMiddleware )
41
+
30
42
return app
31
43
32
44
You can’t perform that action at this time.
0 commit comments