Skip to content

fix(tracing): Skip child span creation in streaming path when no current span (web frameworks)#6810

Draft
sentrivana wants to merge 2 commits into
masterfrom
ivana/streaming-child-spans-web-frameworks
Draft

fix(tracing): Skip child span creation in streaming path when no current span (web frameworks)#6810
sentrivana wants to merge 2 commits into
masterfrom
ivana/streaming-child-spans-web-frameworks

Conversation

@sentrivana

Copy link
Copy Markdown
Contributor

Summary

  • When span streaming is enabled and there is no current span, web framework integrations should not create new root segments for child-span operations
  • Adds a sentry_sdk.traces.get_current_span() is None guard before creating spans in the streaming path
  • Affected integrations: starlette, starlite, litestar, graphene, strawberry, aiohttp

Test plan

  • Existing tests pass
  • Verify that in streaming mode, no orphan root segments are created for web framework sub-operations when there's no active span

🤖 Generated with Claude Code

…ent span (web frameworks)

When span streaming is enabled and there is no current span,
web framework integrations should not create new root segments
for child-span operations. This adds a guard check using
`sentry_sdk.traces.get_current_span()` before creating spans
in the streaming path.

Affected integrations: starlette, starlite, litestar, graphene,
strawberry, aiohttp.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment on lines +356 to +358
if sentry_sdk.traces.get_current_span() is None:
trace_config_ctx.span = None
return

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.

Early return skips trace propagation headers when no current span in streaming mode

The early return when there is no current span skips the should_propagate_trace/iter_trace_propagation_headers block, so outgoing aiohttp requests lose distributed-trace headers that would otherwise be derived from the scope's propagation context.

Evidence
  • scope.iter_trace_propagation_headers (scope.py:680) falls through to get_active_propagation_context().iter_headers() and still yields headers even when span is None.
  • The new guard at lines 356-358 sets trace_config_ctx.span = None and returns before reaching the should_propagate_trace(...) header-injection block later in on_request_start.
  • In the non-streaming path a span is always created, so propagation headers are always added; the streaming no-span path now bypasses propagation entirely.

Identified by Warden code-review · 4TJ-8ZY

…uards

The outgoing http.client span from the test fixture is now suppressed
when there is no active span, reducing span counts in tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant