fix(tracing): Skip child span creation in streaming path when no current span (HTTP clients)#6811
fix(tracing): Skip child span creation in streaming path when no current span (HTTP clients)#6811sentrivana wants to merge 6 commits into
Conversation
…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>
…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>
Codecov Results 📊✅ 91854 passed | ⏭️ 6302 skipped | Total: 98156 | Pass Rate: 93.58% | Execution Time: 310m 25s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 94.92%. Project has 2460 uncovered lines. Files with missing lines (3)
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 +3Generated by Codecov Action |
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
left a comment
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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) |
There was a problem hiding this comment.
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.
Reviewed by Cursor Bugbot for commit c8c9924. Configure here.


Summary
sentry_sdk.traces.get_current_span() is Noneguard before creating spans in the streaming pathTest plan
🤖 Generated with Claude Code