Repository navigation
Anchor derived trace exclusions for FastAPI and FastMCP - #157
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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.
Fixes #158 (part of #156). The Litestar half, #159, waits for #154, which rewrites the same Litestar exclusion code.
Motivation
OpenTelemetry's
ExcludeListsearches every pattern anywhere in the full URL. The defaultopentelemetry_exclude_urls = ["/metrics"], and the health-check path added whenopentelemetry_generate_health_check_spans=False, therefore also silenced any route merely containing them:/api/metrics-reportand/api/health/detailsproduced no server span on FastAPI and FastMCP.Changes
prometheus_metrics_path(newly mirrored onOpentelemetryConfig, the wayhealth_checks_pathalready is) instead of being the default value ofopentelemetry_exclude_urls, which now defaults to[].define_excluded_paths()returns the paths the library derives: the metrics path, plus the health path when health spans are off.define_excluded_url_patterns(), used by FastAPI and FastMCP, anchors those paths as^\w+://[^/]*<path>(?:/|$), with the trailing slash optional./metricsthen excludes/metricsand/metrics/but not/api/metricsor/metrics-report.opentelemetry_exclude_urlsentries 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.opentelemetry_exclude_urlsdescriptions updated.Behavior changes
opentelemetry_exclude_urlsis[]instead of["/metrics"]; the metrics route stays excluded by default.opentelemetry_exclude_urlsto a custom list used to drop the/metricsexclusion as a side effect. Nowprometheus_metrics_pathis always excluded, on every framework, so a customprometheus_metrics_pathis excluded too. To trace the metrics route, setprometheus_metrics_path="".Tests
/metricsand/health/produce no span, while/api/metrics-reportand/api/health/detailsdo. FastMCP also checks/healthwithout the slash. These tests failed before the change.prometheus_metrics_pathis excluded./metricsas 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 checkandmypy .pass;just test: 346 passed. Untouched files have the same uncovered lines as before.