Skip to content

Anchor derived trace exclusions for FastAPI and FastMCP - #157

Merged
lesnik512 merged 1 commit into
mainfrom
fix-anchor-trace-exclusions
Oct 10, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
fix-anchor-trace-exclusions

Conversation

@lesnik512

@lesnik512 lesnik512 commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Fixes #158 (part of #156). The Litestar half, #159, waits for #154, which rewrites the same Litestar exclusion code.

Motivation

OpenTelemetry's ExcludeList searches every pattern anywhere in the full URL. The default opentelemetry_exclude_urls = ["/metrics"], and the health-check path added when opentelemetry_generate_health_check_spans=False, therefore also silenced any route merely containing them: /api/metrics-report and /api/health/details produced no server span on FastAPI and FastMCP.

Changes

  • The metrics path is derived from prometheus_metrics_path (newly mirrored on OpentelemetryConfig, the way health_checks_path already is) instead of being the default value of opentelemetry_exclude_urls, which now defaults to [].
  • New define_excluded_paths() returns the paths the library derives: the metrics path, plus the health path when health spans are off.
  • New define_excluded_url_patterns(), used by FastAPI and FastMCP, anchors those paths as ^\w+://[^/]*<path>(?:/|$), with the trailing slash optional. /metrics then excludes /metrics and /metrics/ but not /api/metrics or /metrics-report.
  • User-supplied opentelemetry_exclude_urls entries are documented as regexes and stay verbatim.
  • define_exclude_urls(), which Litestar uses, returns the same list as before for default settings. Litestar's behavior is unchanged, and the shared module does not conflict with Use Litestar's OpenTelemetryPlugin in the Litestar bootstrapper #154.
  • README: both opentelemetry_exclude_urls descriptions updated.

Behavior changes

  • The default value of opentelemetry_exclude_urls is [] instead of ["/metrics"]; the metrics route stays excluded by default.
  • Setting opentelemetry_exclude_urls to a custom list used to drop the /metrics exclusion as a side effect. Now prometheus_metrics_path is always excluded, on every framework, so a custom prometheus_metrics_path is excluded too. To trace the metrics route, set prometheus_metrics_path="".

Tests

  • FastAPI and FastMCP: with health spans off, /metrics and /health/ produce no span, while /api/metrics-report and /api/health/details do. FastMCP also checks /health without the slash. These tests failed before the change.
  • FastAPI and FastMCP: a custom prometheus_metrics_path is excluded.
  • Three existing FastMCP tests used /metrics as their "still traced" control, which relied on a custom list dropping the default; they now use /health/ or an unknown path.
  • ruff format --check, ruff check and mypy . pass; just test: 346 passed. Untouched files have the same uncovered lines as before.

OpenTelemetry's ExcludeList searches every pattern anywhere in the full url, so
the default /metrics and the health-check path also silenced routes such as
/api/metrics-report and /api/health/details. The metrics path is now derived from
prometheus_metrics_path instead of being the default of opentelemetry_exclude_urls,
and FastAPI and FastMCP anchor the derived paths to the start of the path and a
segment end. User-supplied opentelemetry_exclude_urls stay verbatim regexes.
@codecov

codecov Bot commented Oct 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 99.34% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
microbootstrap/bootstrappers/fastapi.py 100.00% <ø> (ø)
microbootstrap/bootstrappers/fastmcp.py 100.00% <ø> (ø)
...obootstrap/instruments/opentelemetry_instrument.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lesnik512
lesnik512 requested a review from vrslev October 10, 2026 08:31
@lesnik512
lesnik512 merged commit 9b35cc2 into main Oct 10, 2026
18 checks passed
@lesnik512
lesnik512 deleted the fix-anchor-trace-exclusions branch October 10, 2026 08:34
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.

Anchor derived trace exclusions for FastAPI and FastMCP

1 participant