0010: Trace with FastAPI's own OpenTelemetry¶
Status: accepted · Reviewed: 2026-10-31
Logging ownership narrowed by 0015: native
logging configuration is left to the application with configure_logging=False.
Context¶
FastAPI 0.142.0 (fastapi/fastapi#16403) added native OpenTelemetry. Once a global provider is set, FastAPI itself creates request spans and HTTP metrics, and exports log records for validation failures and unhandled exceptions. It steps aside when OpenTelemetry's FastAPI instrumentation is in the middleware stack.
fastapi-observer added that instrumentation only when tracing was on. So with OTLP log export on and tracing off, FastAPI exported its own error logs, with exception messages and stack traces, through the logger provider fastapi-observer had set up. Those logs never passed through fastapi-observer's redaction (0004), and they duplicated its request.failed lines.
Decision¶
- On FastAPI 0.142 and later, tracing uses FastAPI's own support instead of
FastAPIInstrumentor. Excluded URLs, including routes found when the app starts, go to FastAPI'sexcludehook (otel/native.py). Sampling, trace IDs in logs, and the query and HTTP-call totals from 0008 work as before, because they live on the tracer provider or in the logs. - Whenever fastapi-observer exports OTLP logs, it turns FastAPI's own log export off. Our pipeline already logs both events, redacted.
- On older FastAPI, or with
FastAPI(telemetry={"tracing": False}), tracing uses the instrumentation as before.
Evidence¶
test_native_telemetry.pychecks three things. Requests are traced by FastAPI, with excluded and hidden routes skipped, and therequest.completedline carries the span's trace ID. The instrumentation is used when FastAPI's tracing is off. No record from FastAPI's own logger reaches the OTLP exporter, with tracing on or off. Before the change, the last test failed with tracing off.- CPU that fastapi-observer and tracing add to a request, with every request traced and the spans discarded, from
examples/benchmarks/request_cpu.pyon an Apple M1, median of 5 runs. It's 174.5 µs with FastAPI's tracing (traced) and 187.0 µs with the instrumentation (traced-instrumentation), despite three more spans per request. Untraced, it's 50.4 µs, unchanged.
What it cost¶
- Settings go through a private attribute. FastAPI takes telemetry settings only as
FastAPI(telemetry=...), andinstall_observability()runs after the app exists. So it editsapp._telemetry, which FastAPI reads on every request. If that changes, tracing falls back to the instrumentation, and the tests above fail. - Different OTLP HTTP metrics. They are FastAPI's
http.server.request.durationin seconds andhttp.server.active_requests. The instrumentation'shttp.server.durationin milliseconds andhttp.server.response.sizeare gone. - No exception event on the server span. A failed request's span has
error.typeand anERRORstatus. The exception message and stack trace are only in the redacted log line with the same trace ID. - FastAPI's own error logs are off whenever fastapi-observer exports OTLP logs, even if the app enabled them in
FastAPI(telemetry=...).