Skip to content

opentelemetry-instrumentation-asgi: add OTEL_PYTHON_ASGI_EXCLUDE_SPANS environment variable - #5255

Open
vivek-raj1 wants to merge 4 commits into
open-telemetry:mainfrom
vivek-raj1:asgi-exclude-spans-env
Open

vivek-raj1 wants to merge 4 commits into
open-telemetry:mainfrom
vivek-raj1:asgi-exclude-spans-env

Conversation

@vivek-raj1

Copy link
Copy Markdown

Description

Fixes #3992

exclude_spans can only be set through instrument_app(...) or OpenTelemetryMiddleware(...), so apps using opentelemetry-instrument cannot drop the internal http send / http receive spans.

OpenTelemetryMiddleware now falls back to OTEL_PYTHON_ASGI_EXCLUDE_SPANS (comma delimited send and/or receive) when exclude_spans is not passed, the same way it already falls back to the OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_* variables. An explicit exclude_spans argument, 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:app

AI-assisted: I used AI assistance to prepare this change and reviewed and tested it myself.

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

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 that exclude_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.

  • I am a human and not a bot
  • I have run tox -e precommit
  • I have run tox -e typecheck
  • I have run the relevant tests and linting tox environments (lint-instrumentation-asgi, lint-instrumentation-fastapi)

…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
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

@vivek-raj1
vivek-raj1 requested a review from a team as a code owner October 28, 2026 16:48
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-11-02 11:19 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@github-project-automation github-project-automation Bot moved this to Approved PRs in Python PR digest Oct 29, 2026

@xrmx xrmx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have approved but since both starlette and fastapi are getting native instrumentation maybe this is not so important anymore?

@vivek-raj1

Copy link
Copy Markdown
Author

@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 OpenTelemetryMiddleware without FastAPI or Starlette. Native instrumentation in those two frameworks would not cover them.

It also helps until native instrumentation is available. People using opentelemetry-instrument can't pass exclude_spans in code today, so an environment variable is the only way for them to drop the http send and http receive spans.

If you still think it's better to leave this out, I'm happy to close it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Approved PRs

Development

Successfully merging this pull request may close these issues.

Add environment variable for exclude_spans parameter in FastAPI/ASGI instrumentation

2 participants