Skip to content

feat: make the ASGI receive/send spans opt-out on FastAPI - #225

Merged
lesnik512 merged 1 commit into
mainfrom
feat/otel-exclude-spans
Sep 19, 2026
Merged

lesnik512 merged 1 commit into
mainfrom
feat/otel-exclude-spans

Conversation

@lesnik512

@lesnik512 lesnik512 commented Sep 19, 2026

Copy link
Copy Markdown
Member

Closes #185.

opentelemetry_exclude_spans on FastAPIConfig, passed to FastAPIInstrumentor.instrument_app.

Where the field lives

On FastAPIConfig, not the shared OpenTelemetryConfig. exclude_spans is a parameter of
FastAPIInstrumentor.instrument_app; Litestar's instrumentor has no counterpart, so a shared field
would be inert for every bootstrapper but one. opentelemetry_middleware_cls on FastStreamConfig
is the same shape, and the issue anticipated this.

The default does not change

Empty, so all three spans are recorded exactly as today. ["receive", "send"] is measurably cheaper
— the issue benchmarked 33 µs/request, a quarter of the instrument's cost — but it removes two
thirds of the spans from an existing service's trace view. That is a knob to document, not a default
to impose in a library upgrade.

Passed unconditionally, like the sampler in #222: [] and None are identical downstream, where
the ASGI middleware reads "receive" in exclude_spans if exclude_spans else False.

No dependency change

exclude_spans exists at the declared floor. I checked the 0.49b0 wheel directly rather than the
installed 0.65b0: instrument_app carries the parameter there too, so lowest-direct stays green
without a bump.

Tests

Two, following test_faststream_opentelemetry_excluded_urls_in_built_set: they pin what
lite-bootstrap hands the instrumentor — [] by default, and the configured value when set — rather
than re-testing upstream's span output. An end-to-end assertion would have to reach the
process-global tracer provider, which is set-once and already owned by whichever test bootstrapped
first.

No ADR

An earlier revision of this branch carried one. It failed the bar in the /domain-modeling skill:
the placement is reversible (a field move plus the silent alias this repo already does for renames),
it is not surprising once the one-line comment at the field says why, and the trade-off is thin
since Litestar having no counterpart nearly forces it. The comment and the docs bullet carry what a
reader needs.

@lesnik512
lesnik512 force-pushed the feat/otel-exclude-spans branch from 1e7b554 to 5cbee38 Compare September 19, 2026 11:30
Every request produces three spans today: the server span plus one each for the
ASGI receive and send events. #185 benchmarked dropping the latter two at 33
µs/request, a quarter of the instrument's cost.

The field is FastAPI-scoped because the parameter is: Litestar's instrumentor has
no counterpart, so a shared OpenTelemetryConfig field would be inert everywhere
but one bootstrapper. The default stays empty, since dropping spans changes what
an existing service sees in Jaeger or Tempo.
@lesnik512
lesnik512 force-pushed the feat/otel-exclude-spans branch from 5cbee38 to 793458e Compare September 19, 2026 11:35
@lesnik512
lesnik512 merged commit 717fb1d into main Sep 19, 2026
13 checks passed
@lesnik512
lesnik512 deleted the feat/otel-exclude-spans branch September 19, 2026 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pass exclude_spans to FastAPIInstrumentor so ASGI send/receive spans are opt-out

1 participant