Skip to content

Commit 9a9fbfe

Browse files
authored
fix: Make PropagationContext.from_incoming_data always return a PropagationContext (#5186)
### Description When there is any sort of incoming data, and since `continue_trace` is always intended to be at a system boundary, we always want to force a new trace if there's no incoming propagation or a mismatched propagation headers (for `strict_trace_continuation`). What previously happened in these cases: a single `trace_id` was kept alive in the `propagation_context` even when these were meant to be new independent traces (see screenshot that shows undesired behavior before this fix). I think this will actually solve many complaints about long living traces that were never supposed to be such. <img width="1828" height="649" alt="image" src="https://github.com/user-attachments/assets/dd14e74a-0c0c-44a0-affc-994519655ab3" />
1 parent 9c9510d commit 9a9fbfe

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

sentry_sdk/scope.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,12 @@ def generate_propagation_context(self, incoming_data=None):
510510
If there is `incoming_data` overwrite existing propagation context.
511511
If there is no `incoming_data` create new propagation context, but do NOT overwrite if already existing.
512512
"""
513-
if incoming_data:
514-
propagation_context = PropagationContext.from_incoming_data(incoming_data)
515-
if propagation_context is not None:
516-
self._propagation_context = propagation_context
513+
if incoming_data is not None:
514+
self._propagation_context = PropagationContext.from_incoming_data(
515+
incoming_data
516+
)
517517

518+
# TODO-neel this below is a BIG code smell but requires a bunch of other refactoring
518519
if self._type != ScopeType.CURRENT:
519520
if self._propagation_context is None:
520521
self.set_new_propagation_context()

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,25 +447,25 @@ def __init__(
447447

448448
@classmethod
449449
def from_incoming_data(cls, incoming_data):
450-
# type: (Dict[str, Any]) -> Optional[PropagationContext]
450+
# type: (Dict[str, Any]) -> PropagationContext
451+
propagation_context = PropagationContext()
451452
normalized_data = normalize_incoming_data(incoming_data)
452453

453454
sentry_trace_header = normalized_data.get(SENTRY_TRACE_HEADER_NAME)
454455
sentrytrace_data = extract_sentrytrace_data(sentry_trace_header)
455456

456457
# nothing to propagate if no sentry-trace
457458
if sentrytrace_data is None:
458-
return None
459+
return propagation_context
459460

460461
baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
461462
baggage = (
462463
Baggage.from_incoming_header(baggage_header) if baggage_header else None
463464
)
464465

465466
if not _should_continue_trace(baggage):
466-
return None
467+
return propagation_context
467468

468-
propagation_context = PropagationContext()
469469
propagation_context.update(sentrytrace_data)
470470
if baggage:
471471
propagation_context.baggage = baggage

tests/tracing/test_integration_tests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,14 @@ def test_continue_trace_strict_trace_continuation(
419419
)
420420
assert transaction.parent_span_id != "1234567890abcdef"
421421
assert not transaction.parent_sampled
422+
423+
424+
def test_continue_trace_forces_new_traces_when_no_propagation(sentry_init):
425+
"""This is to make sure we don't have a long running trace because of TWP logic for the no propagation case."""
426+
427+
sentry_init(traces_sample_rate=1.0)
428+
429+
tx1 = continue_trace({}, name="tx1")
430+
tx2 = continue_trace({}, name="tx2")
431+
432+
assert tx1.trace_id != tx2.trace_id

0 commit comments

Comments
 (0)