Repository navigation
refactor: OpenTelemetry instrumentations record HTTP and process metrics - #388
Merged
Merged
Conversation
…ics, and MetricsMiddleware is gone FastAPIInstrumentor ran inside the lifespan, after Starlette had built the middleware stack on the lifespan's own ASGI call, so it never wrapped anything and the app recorded no HTTP spans or OTel HTTP metrics. create_app now instruments the app before the stack exists, and the hand-written MetricsMiddleware goes. The backend image opts into the stable HTTP semantic conventions, so requests show up as http_server_request_duration_seconds and friends, labelled by FastAPI's route template. The psutil gauges go too. The collector's host_metrics receiver already scrapes host CPU and memory, with CPU utilization now turned on, and each process reports its own memory, CPU, threads and file descriptors through opentelemetry-instrumentation-system-metrics. setup_metrics moves to app/core/metrics/base.py and psutil stops being a direct dependency. The HTTP dashboard reads the new series, the contract test builds the instrumentations' families the way the app configures them, and the middleware, metrics reference and contract testing docs describe the new setup.
HardMax71
enabled auto-merge (squash)
October 13, 2026 23:58
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configuration
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
|
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
6 of 17 tasks
HardMax71
added a commit
that referenced
this pull request
Oct 18, 2026
…stead of straight to Jaeger (#391) Every span now reaches the collector, which #389 needs so its connectors can turn spans into metrics before tail sampling drops most of them. Jaeger gets what the collector's existing tail sampling keeps: traces with an error, traces over 1 s and a 10 % sample, 10 to 20 s after they end. The tracing doc also no longer says FastAPI is instrumented in the lifespan, since #388 moved that into create_app.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



The FastAPI OpenTelemetry instrumentation never worked.
dishka_lifespan.pycalledinstrument_appinside the lifespan, but Starlette builds the middleware stack on its first ASGI call, which is the lifespan startup itself, andinstrument_apponly patchesbuild_middleware_stackwithout rebuilding. So no request produced an OTel span or HTTP metric, andMetricsMiddlewarewas the only HTTP telemetry. A probe that drives the lifespan the way uvicorn does recorded nothing with the old placement and all three HTTP instruments with the new one.create_appnow instruments the app before the stack exists, so the OTel middleware wraps everything, andMetricsMiddlewareis deleted. The backend image setsOTEL_SEMCONV_STABILITY_OPT_IN=http(pytest's env does too), so requests land ashttp_server_request_duration_seconds,http_server_active_requestsand the two body size histograms, labelled by FastAPI's route template instead of our regexes. Since the OTel middleware is outermost, the 403, 429 and 413 answers from CSRF, rate limiting and the size limit now show up in the metrics; before they didn't.The psutil gauges go as well. The collector's
host_metricsreceiver already scraped host memory, and now host CPU utilization too, and every backend and worker process reports its own memory, CPU, threads and file descriptors throughopentelemetry-instrumentation-system-metrics.setup_metricsmoves toapp/core/metrics/base.py, and psutil is no longer a direct dependency.The HTTP dashboard's panels read the new series. The contract test builds the instrumentations' families on a bare app the way
create_appconfigures them, and passes. The middleware, metrics reference and contract testing docs describe the new setup.Checked with ruff, mypy, grimp, 564 unit tests, the 3 contract tests and a strict docs build. Against the real
create_appwith an in-memory reader, a request recordedhttp.server.request.durationwith method, status and scheme, and the security headers still came back.Summary by cubic
Fixes the FastAPI OpenTelemetry instrumentation, which ran inside the lifespan after Starlette had already built the middleware stack and so never wrapped a single request. HTTP and process metrics now come from OpenTelemetry instrumentations, and the hand-written
MetricsMiddlewareand psutil gauges are removed.HTTP metrics
http_server_request_duration_seconds,http_server_active_requestsand the two body size histograms, labelled by FastAPI route templates instead of regex-normalized paths.OTEL_SEMCONV_STABILITY_OPT_IN=http, now set in the backend image and pytest.Process metrics
opentelemetry-instrumentation-system-metrics; host CPU and memory come from the collector'shost_metricsreceiver.setup_metricsmoved toapp/core/metrics/base.py, and psutil stops being a direct dependency.The Grafana HTTP dashboard, the metrics reference and middleware docs, and the contract test now match the new series.
Written for commit a2c7eb6. Summary will update on new commits.