Skip to content

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

Open
sentrivana wants to merge 3 commits into
masterfrom
ivana/streaming-child-spans-web-frameworks
Open

fix(tracing): Skip child span creation in streaming path when no current span (web frameworks)#6810
sentrivana wants to merge 3 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 thread sentry_sdk/integrations/aiohttp.py
…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>
@ericapisani ericapisani marked this pull request as ready for review July 14, 2026 19:43
@ericapisani ericapisani requested a review from a team as a code owner July 14, 2026 19:43
Comment on lines +1573 to 1578
# The outgoing http.client span is suppressed because there is no active
# span when the test client makes the request.
assert len(items) == 0


@pytest.mark.asyncio

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.

Outgoing trace header propagation dropped when no active span in streaming mode

This test previously verified that the sentry-trace header was propagated on outgoing requests, but now asserts zero spans and no header assertions; the underlying early-return in aiohttp.py (lines 356-358) skips adding trace headers entirely when there is no current span, which can break distributed trace continuation for outgoing HTTP calls.

Evidence
  • aiohttp.py:356-358 returns early (trace_config_ctx.span = None; return) before the block that attaches sentry-trace/baggage headers when get_current_span() is None.
  • The revised test drops the prior assertions on resp.request_info.headers['sentry-trace'] and now only checks len(items) == 0, removing coverage for outgoing trace-header propagation.
  • Distributed tracing normally propagates trace context from the propagation context even absent an active span, so suppressing the header can sever the trace chain.

Identified by Warden code-review · DYK-FKT

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

91785 passed | ⏭️ 6302 skipped | Total: 98087 | Pass Rate: 93.58% | Execution Time: 317m 41s

📊 Comparison with Base Branch

Metric Change
Total Tests
Passed Tests
Failed Tests
Skipped Tests

✨ No test changes detected

All tests are passing successfully.

❌ Patch coverage is 55.17%. Project has 2456 uncovered lines.
❌ Project coverage is 89.75%. Comparing base (base) to head (head).

Files with missing lines (4)
File Patch % Lines
sentry_sdk/integrations/strawberry.py 38.46% ⚠️ 8 Missing and 5 partials
sentry_sdk/integrations/litestar.py 50.00% ⚠️ 3 Missing and 3 partials
sentry_sdk/integrations/starlette.py 50.00% ⚠️ 1 Missing and 1 partials
sentry_sdk/integrations/starlite.py 50.00% ⚠️ 1 Missing and 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.79%    89.75%    -0.04%
==========================================
  Files          193       193         —
  Lines        23929     23958       +29
  Branches      8286      8310       +24
==========================================
+ Hits         21485     21502       +17
- Misses        2444      2456       +12
- Partials      1361      1371       +10

Generated by Codecov Action

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.

2 participants