Skip to content

fix(tracing): Correctly generate traceparent from NoOpStreamedSpans#6725

Closed
sentrivana wants to merge 1 commit into
masterfrom
ivana/fix-traceparent-noop
Closed

fix(tracing): Correctly generate traceparent from NoOpStreamedSpans#6725
sentrivana wants to merge 1 commit into
masterfrom
ivana/fix-traceparent-noop

Conversation

@sentrivana

Copy link
Copy Markdown
Contributor

Description

When a NoOpStreamedSpan was active, I'd report an incorrect traceparent. Make NoOpStreamedSpans aware of the trace ID and make them generate their own unique span ID.

Reminders

@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Codecov Results 📊

90078 passed | ❌ 15 failed | ⏭️ 6298 skipped | Total: 96391 | Pass Rate: 93.45% | Execution Time: 320m 17s

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +45
Passed Tests 📈 +30
Failed Tests 📈 +15
Skipped Tests

➕ New Tests (15)

View new tests
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing
  • test_continue_trace_unsampled
    • File: tests.tracing.test_span_streaming
    • Status: ❌ Failing

❌ Failed Tests

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.10-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.10-gevent
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.11-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.11-gevent
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.12-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.12-gevent
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.13-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.14-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.14t-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.6-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.6-gevent
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.7-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.8-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.8-gevent
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

test_continue_trace_unsampled

File: tests.tracing.test_span_streaming
Suite: py3.9-common
Error: AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000' - 00000000000000000000000000000000 + 0af7651916cd43dd8448eb211c80319c

Stack Trace
tests/tracing/test_span_streaming.py:747: in test_continue_trace_unsampled
    assert span.trace_id == "00000000000000000000000000000000"
E   AssertionError: assert '0af7651916cd43dd8448eb211c80319c' == '00000000000000000000000000000000'
E     
E     - 00000000000000000000000000000000
E     + 0af7651916cd43dd8448eb211c80319c

✅ Patch coverage is 100.00%. Project has 2435 uncovered lines.
✅ Project coverage is 89.78%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    89.77%    89.78%    +0.01%
==========================================
  Files          192       192         —
  Lines        23824     23820        -4
  Branches      8226      8226         —
==========================================
+ Hits         21386     21385        -1
- Misses        2438      2435        -3
- Partials      1349      1348        -1

Generated by Codecov Action

@sentry-warden sentry-warden Bot left a comment

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.

Ignored root-level span still generates a random trace_id instead of inheriting from propagation context (sentry_sdk/scope.py:1301)

The NoOpStreamedSpan returned for an ignored root span (lines 1301–1304) is missing trace_id=propagation_context.trace_id, so it will generate a random trace ID instead of using the one from the propagation context — the same bug this PR fixes for the unsampled case.

Evidence
  • propagation_context is fetched at line 1300 before the is_ignored_span check.
  • The unsampled branch at line 1316 was updated by this PR to pass trace_id=propagation_context.trace_id.
  • The ignored-span branch at lines 1301–1304 creates NoOpStreamedSpan(scope=self, unsampled_reason="ignored") with no trace_id.
  • NoOpStreamedSpan.trace_id inherits StreamedSpan.trace_id, which calls uuid.uuid4().hex when self._trace_id is falsy — producing a fresh random ID unrelated to the active trace.
  • Any outgoing HTTP call made while this span is active will propagate an incorrect sentry-trace header.

Existing unsampled-span test still asserts old hardcoded zero trace/span IDs and will now fail (sentry_sdk/traces.py:722)

This PR makes NoOpStreamedSpan trace-aware: Scope._start_streamed_span now constructs the unsampled segment span with trace_id=propagation_context.trace_id (scope.py:1317-1320), and NoOpStreamedSpan inherits trace_id/span_id from StreamedSpan, which returns the propagated trace_id and lazily generates a random span_id (uuid.uuid4().hex[16:]). The existing test test_continue_trace_unsampled in tests/tracing/test_span_streaming.py:747-748 still asserts span.trace_id == "00000000000000000000000000000000" and span.span_id == "0000000000000000". Since that test starts an unsampled segment after continue_trace with trace_id="0af7651916cd43dd8448eb211c80319c", span.trace_id will now be that propagated value and span.span_id will be a random hex, so both assertions will fail. This test file was not updated in the PR (only test_http_headers.py was), so CI will break.

Evidence
  • scope.py:1317-1320 now creates the unsampled segment as NoOpStreamedSpan(scope=self, trace_id=propagation_context.trace_id, unsampled_reason=outcome).
  • NoOpStreamedSpan (traces.py:611) does not override trace_id/span_id, so it inherits StreamedSpan.trace_id (returns _trace_id) and StreamedSpan.span_id (lazy uuid.uuid4().hex[16:]) at traces.py:463-474.
  • test_continue_trace_unsampled (test_span_streaming.py:721) continues a trace with trace_id="0af7651916cd43dd8448eb211c80319c", so span.trace_id now returns that value and span.span_id returns random hex — never zeros.
  • Lines 747-748 still assert the old "0000..." values, and test_span_streaming.py is not in the PR's changed files, so these assertions will fail.

Identified by Warden code-review

@sentrivana sentrivana closed this Jul 2, 2026
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