fix(streaming): Set segment source if unset - #6791
Conversation
There was a problem hiding this comment.
Streaming span payload accessed with wrong key "transaction" instead of attributes["sentry.segment.name"]
In test_continue_trace_span_streaming, the falsy-branch asserts trace1_payload["transaction"] == "hi", but streaming span envelopes use the _to_json() format which has "name", not "transaction". The else-branch of the same test correctly uses trace1_payload["attributes"]["sentry.segment.name"], making the falsy-branch a copy-paste error that will raise KeyError at runtime.
Evidence
StreamedSpan._to_json()(traces.py:592) produces a dict with keys"name","trace_id","attributes", etc., but no top-level"transaction"key.test_continue_trace_span_streamingusescapture_envelopes, sotrace1.items[0].payload.jsonis the raw_to_json()output.- The
else-branch of the same test correctly accessestrace1_payload["attributes"]["sentry.segment.name"], confirming the expected format. - The non-streaming counterpart
test_continue_tracecallstrace1.get_transaction_event()which does return a"transaction"key — this is the source of the copy-paste.
Identified by Warden code-review
- Construct sentry-trace headers manually since StreamedSpan.sampled is always True, making it impossible to vary parent_sampled via real spans - Use context managers for streaming spans so they finish and get sent - Fix sampling condition to match streaming behavior (uses sample_rate/ sample_rand, not parent_sampled directly) - Update span name via span.name instead of scope.transaction - Fix copy-paste errors in DSC test (wrong transaction name, missing URL encoding) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ac2f342 to
d8b7724
Compare
b6e7f84 to
2092b87
Compare
Codecov Results 📊✅ 96365 passed | ⏭️ 6340 skipped | Total: 102705 | Pass Rate: 93.83% | Execution Time: 336m 7s 📊 Comparison with Base Branch
All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 2508 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 89.67% 89.69% +0.02%
==========================================
Files 193 193 —
Lines 24321 24323 +2
Branches 8572 8574 +2
==========================================
+ Hits 21809 21815 +6
- Misses 2512 2508 -4
- Partials 1397 1396 -1Generated by Codecov Action |
### Description Set segment source if not set. This mirrors legacy tracing where the source would be set to `custom` if not set by an integration or explicitly. Also, add streaming versions of tests in `tracing/test_integration_tests.py`. (That was how this PR originally started -- I discovered that the source was missing while porting the tests.) #### Issues Part of getsentry#5395 #### Reminders - Please add tests to validate your changes, and lint your code using `uv run ruff`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr) --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Description
Set segment source if not set. This mirrors legacy tracing where the source would be set to
customif not set by an integration or explicitly.Also, add streaming versions of tests in
tracing/test_integration_tests.py. (That was how this PR originally started -- I discovered that the source was missing while porting the tests.)Issues
Part of #5395
Reminders
uv run ruff.feat:,fix:,ref:,meta:)