Skip to content

fix: anchor FastAPI trace exclusions and honor the exclusion environment variables - #282

Merged
lesnik512 merged 1 commit into
mainfrom
fix/anchor-excluded-urls
Oct 10, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix/anchor-excluded-urls

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Why

Unanchored FastAPI exclusions. FastAPI handed FastAPIInstrumentor the derived metrics and health paths as bare strings. OpenTelemetry's ExcludeList searches each pattern anywhere in the full URL, so a route merely containing one was silently left untraced. With 1.12.0, /metrics also 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. FastAPIInstrumentor and FastMCP's OpenTelemetryMiddleware read OTEL_PYTHON_FASTAPI_EXCLUDED_URLS / OTEL_PYTHON_STARLETTE_EXCLUDED_URLS (falling back to OTEL_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

  • FastMCP's anchoring moves to OpenTelemetryInstrument._build_excluded_url_patterns(environment_key), and FastAPI uses it too:
    • Derived paths become ^\w+://[^/]*<path>(?:/|$), with the trailing slash optional, so they exclude both slash forms but no lookalike or sub-path.
    • opentelemetry_excluded_urls and the environment variable entries pass through verbatim, because they are regexes.
    • The environment variables are read the way OpenTelemetry reads them: the framework-specific one, else the generic one.
  • Litestar keeps its own path-based patterns from feat!: trace Litestar through its own OpenTelemetryPlugin #279; its plugin already reads OTEL_PYTHON_LITESTAR_EXCLUDED_URLS.
  • _build_excluded_urls is unchanged: ADR-0002 cites it, and the FastStream tests pin it.
  • I checked that the URL ExcludeList sees has no query string, from asgi 0.49b0 through the current release, so the $ anchor is enough.

No config changes. Your own opentelemetry_excluded_urls behave as before; only the derived paths stop over-matching.

Tests

  • FastAPI, regression: /custom-metrics/ and /custom-health/ produce no span, while /api/custom-metrics/report and /custom-healthy do. This test failed before the fix.
  • FastAPI: each of OTEL_PYTHON_FASTAPI_EXCLUDED_URLS and OTEL_PYTHON_EXCLUDED_URLS excludes a route.
  • FastMCP: OTEL_PYTHON_STARLETTE_EXCLUDED_URLS excludes POST /mcp.
  • just lint, ty 0.0.85 and just test-ci pass: 362 passed, 100% coverage.

…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.
@lesnik512
lesnik512 merged commit caa79ab into main Oct 10, 2026
30 checks passed
@lesnik512
lesnik512 deleted the fix/anchor-excluded-urls branch October 10, 2026 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant