Repository navigation
opentelemetry-instrumentation-asgi: add OTEL_PYTHON_ASGI_EXCLUDE_SPANS environment variable - #5255
vivek-raj1 wants to merge 4 commits into
Conversation
…S environment variable Allow auto-instrumented ASGI apps (FastAPI, Starlette) to drop the internal http send/receive spans without switching to programmatic instrumentation. An explicit exclude_spans argument still takes precedence over the environment variable. Fixes open-telemetry#3992
Pull request dashboard statusWaiting on maintainers · refreshed 2026-11-02 11:19 UTC Merge when ready. Status above doesn't look right?
|
xrmx
left a comment
There was a problem hiding this comment.
I have approved but since both starlette and fastapi are getting native instrumentation maybe this is not so important anymore?
|
@xrmx Thanks for the review. I think this is still useful for two reasons. This change is in the ASGI middleware, so it also helps apps that use It also helps until native instrumentation is available. People using If you still think it's better to leave this out, I'm happy to close it. |
Description
Fixes #3992
exclude_spanscan only be set throughinstrument_app(...)orOpenTelemetryMiddleware(...), so apps usingopentelemetry-instrumentcannot drop the internalhttp send/http receivespans.OpenTelemetryMiddlewarenow falls back toOTEL_PYTHON_ASGI_EXCLUDE_SPANS(comma delimitedsendand/orreceive) whenexclude_spansis not passed, the same way it already falls back to theOTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_*variables. An explicitexclude_spansargument, including[], takes precedence. FastAPI and Starlette pick this up without changes since they build on the ASGI middleware.OTEL_PYTHON_ASGI_EXCLUDE_SPANS="send,receive" opentelemetry-instrument uvicorn main:appAI-assisted: I used AI assistance to prepare this change and reviewed and tested it myself.
Type of change
How Has This Been Tested?
tox -e py312-test-instrumentation-asgi: 107 passed. New tests cover each env value (receive,send,send,receive, empty) and thatexclude_spans=[]overrides the env var.tox -e py312-test-instrumentation-fastapi-oldest,py312-test-instrumentation-fastapi-latest: 110 passed each. New test covers auto-instrumentation with the env var set.tox -e py312-test-instrumentation-starlette-latest: 62 passed.Checklist:
See contributing.md for styleguide, changelog guidelines, and more.
tox -e precommittox -e typechecklint-instrumentation-asgi,lint-instrumentation-fastapi)