Skip to content

Add environment variable for exclude_spans parameter in FastAPI/ASGI instrumentation #3992

@shaariqch-carma

Description

@shaariqch-carma

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions