Skip to content

fix(tracing): Skip child span creation in streaming path when no current span (HTTP clients)#6811

Open
sentrivana wants to merge 6 commits into
masterfrom
ivana/streaming-child-spans-http-clients
Open

fix(tracing): Skip child span creation in streaming path when no current span (HTTP clients)#6811
sentrivana wants to merge 6 commits into
masterfrom
ivana/streaming-child-spans-http-clients

Conversation

@sentrivana

Copy link
Copy Markdown
Contributor

Summary

  • When span streaming is enabled and there is no current span, HTTP client 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: httpx, httpx2, pyreqwest, boto3, stdlib (urllib)

Test plan

  • Existing tests pass
  • Verify that in streaming mode, no orphan root segments are created for outgoing HTTP requests when there's no active span

🤖 Generated with Claude Code

…ent span (HTTP clients)

When span streaming is enabled and there is no current span,
HTTP client 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: httpx, httpx2, pyreqwest, boto3, stdlib.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Comment thread sentry_sdk/integrations/httpx2.py
Comment thread sentry_sdk/integrations/httpx.py
…an guards

httpx2: Wrap HTTP calls in parent span context so child spans are created.
strawberry: Remove async/sync branching since both paths now produce 5 spans.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

91854 passed | ⏭️ 6302 skipped | Total: 98156 | Pass Rate: 93.58% | Execution Time: 310m 25s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +69
Passed Tests 📈 +69
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 94.92%. Project has 2460 uncovered lines.
❌ Project coverage is 89.74%. Comparing base (base) to head (head).

Files with missing lines (3)
File Patch % Lines
sentry_sdk/integrations/stdlib.py 86.67% ⚠️ 2 Missing and 2 partials
sentry_sdk/integrations/boto3.py 50.00% ⚠️ 1 Missing and 1 partials
sentry_sdk/integrations/pyreqwest.py 100.00% ⚠️ 1 partials
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    89.77%    89.74%    -0.03%
==========================================
  Files          193       193         —
  Lines        23972     23967        -5
  Branches      8328      8314       -14
==========================================
+ Hits         21518     21507       -11
- Misses        2454      2460        +6
- Partials      1373      1376        +3

Generated by Codecov Action

ericapisani and others added 2 commits July 14, 2026 10:42
The streaming child-span guard added for HTTP client integrations skips
child-span creation when there is no current span. The httpx2 and
strawberry tests were updated for this, but the equivalent httpx
streaming tests were missed, so no HTTP client span was produced and
the tests failed with StopIteration.

Wrap each streaming test's request in a parent span so a child HTTP
client span is created, mirroring the httpx2 test updates.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

@ericapisani ericapisani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking because we're losing trace propagation within some integrations with these changes. Will work on fixes now

…nt span (HTTP clients)

Previously, when span streaming was enabled and there was no active
span, httpx/httpx2/httplib skipped trace header propagation entirely
by returning early. This diverged from the legacy behavior and dropped
distributed tracing continuity for outgoing requests made outside of
a span.

Extract the shared header-propagation logic into
`propagate_trace_headers()` in tracing_utils.py and call it from the
no-current-span early-return branches so headers are still attached
from the scope's propagation context.
@ericapisani ericapisani marked this pull request as ready for review July 15, 2026 16:45
@ericapisani ericapisani requested a review from a team as a code owner July 15, 2026 16:45
@ericapisani ericapisani dismissed their stale review July 15, 2026 16:46

I've made changes to propagate traces

Comment thread sentry_sdk/integrations/httpx.py
The httpx sync and async streaming paths returned early without
propagating trace headers when there was no active span, dropping
distributed tracing continuity for outgoing requests made outside of
a span. Call propagate_trace_headers() in both no-current-span
branches so headers are attached from the scope's propagation context,
matching httpx2 and httplib.

Add sync and async tests covering trace propagation with span
streaming enabled and no active span.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c8c9924. Configure here.

span: "Union[Span, StreamedSpan]"
if span_streaming:
if sentry_sdk.traces.get_current_span() is None:
return old_popen_init(self, *a, **kw)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Subprocess trace env not propagated

Medium Severity

With span streaming and no active streamed span, Popen.__init__ returns before injecting SUBPROCESS_* trace variables, while the httplib patch in the same integration still attaches sentry-trace/baggage from the scope propagation context when span is None.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c8c9924. Configure here.

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