Repository navigation
fix: anchor FastAPI trace exclusions and honor the exclusion environment variables - #282
Merged
Merged
Conversation
…ent variables FastAPI handed OpenTelemetry the derived metrics and health paths unanchored, and ExcludeList searches them anywhere in the full URL, so /custom-metrics/ also silenced /api/custom-metrics/report. The anchoring FastMCP already used moves to OpenTelemetryInstrument._build_excluded_url_patterns and FastAPI uses it too. Passing an exclude list made the FastAPI and FastMCP instrumentation ignore OTEL_PYTHON_FASTAPI_EXCLUDED_URLS / OTEL_PYTHON_STARLETTE_EXCLUDED_URLS (and OTEL_PYTHON_EXCLUDED_URLS); their entries are now added to the list.
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.
Why
Unanchored FastAPI exclusions. FastAPI handed
FastAPIInstrumentorthe derived metrics and health paths as bare strings. OpenTelemetry'sExcludeListsearches each pattern anywhere in the full URL, so a route merely containing one was silently left untraced. With 1.12.0,/metricsalso excluded/api/metrics-report, and/custom-metrics/excludes/api/custom-metrics/report. This is the same class of bug #248 fixed for Litestar.Ignored environment variables.
FastAPIInstrumentorand FastMCP'sOpenTelemetryMiddlewarereadOTEL_PYTHON_FASTAPI_EXCLUDED_URLS/OTEL_PYTHON_STARLETTE_EXCLUDED_URLS(falling back toOTEL_PYTHON_EXCLUDED_URLS) only when no exclude list is passed. lite-bootstrap always passes one, so the variables never worked. Litestar honors its variable since #279.Changes
OpenTelemetryInstrument._build_excluded_url_patterns(environment_key), and FastAPI uses it too:^\w+://[^/]*<path>(?:/|$), with the trailing slash optional, so they exclude both slash forms but no lookalike or sub-path.opentelemetry_excluded_urlsand the environment variable entries pass through verbatim, because they are regexes.OTEL_PYTHON_LITESTAR_EXCLUDED_URLS._build_excluded_urlsis unchanged: ADR-0002 cites it, and the FastStream tests pin it.ExcludeListsees has no query string, from asgi 0.49b0 through the current release, so the$anchor is enough.No config changes. Your own
opentelemetry_excluded_urlsbehave as before; only the derived paths stop over-matching.Tests
/custom-metrics/and/custom-health/produce no span, while/api/custom-metrics/reportand/custom-healthydo. This test failed before the fix.OTEL_PYTHON_FASTAPI_EXCLUDED_URLSandOTEL_PYTHON_EXCLUDED_URLSexcludes a route.OTEL_PYTHON_STARLETTE_EXCLUDED_URLSexcludesPOST /mcp.just lint,ty0.0.85 andjust test-cipass: 362 passed, 100% coverage.