open-telemetry / open-telemetry/opentelemetry-python-contrib

Add environment variable for exclude_spans parameter in FastAPI/ASGI instrumentation

Open
#3,992 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature-request
Dominant language
Python
Stars
1.1k
Forks
1.1k
Avg merge
4d 15h
Merged PRs (30d)
16

Description

What problem do you want to solve?

When using auto-instrumentation (opentelemetry-instrument), there's no way to exclude the send and receive spans that the ASGI middleware generates. These spans are very noisy for typical REST APIs — a simple "hello world" endpoint generates 3 spans instead of 1, and the child http send/http receive spans rarely provide actionable information.
The exclude_spans parameter exists and works great when using programmatic instrumentation:

FastAPIInstrumentor.instrument_app(app, exclude_spans=["send", "receive"])

However, there's no environment variable equivalent, which means users of auto-instrumentation cannot use this feature without switching to manual instrumentation.

Describe the solution you'd like

Add an environment variable like OTEL_PYTHON_FASTAPI_EXCLUDE_SPANS (or OTEL_PYTHON_ASGI_EXCLUDE_SPANS for the base ASGI instrumentation) that accepts a comma-separated list of span types to exclude.
Example:

OTEL_PYTHON_FASTAPI_EXCLUDE_SPANS="send,receive"

This would be consistent with other env var patterns in the instrumentation like OTEL_PYTHON_FASTAPI_EXCLUDED_URLS.

Describe alternatives you've considered

Switching from auto-instrumentation to programmatic instrumentation just to set this one option — this defeats much of the convenience of auto-instrumentation.
Using a custom span processor to filter spans at export time — this works but is more complex and filters spans after they've already been created (wasted work).
Using a custom sampler to drop spans by name — same issue as above, plus it can cause problems (e.g., #2087 where http.status_code wasn't set on the server span when child spans were dropped).

Additional Context

Related issues where users have complained about the noisy send/receive spans:

#831
#635

The exclude_spans parameter was added to address this, but the lack of env var support limits its usefulness for auto-instrumentation users.

Would you like to implement a fix?

None

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at FastAPIInstrumentor.instrument_app and trace how the existing OTEL_PYTHON_FASTAPI_EXCLUDED_URLS setting is configured for auto-instrumentation. Add equivalent comma-separated environment-variable support for exclude_spans, then verify that values such as "send,receive" prevent those ASGI spans while preserving programmatic behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
fastapi, python
Domain
api, backend, observability
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.