Skip to content

Commit 8aba654

Browse files
committed
Make PropagationContext hold baggage instead of dynamic_sampling_context
The tracing incoming headers implementation and the Transaction constructor take a `Baggage` object so this was incosistent with how I originally designed the baggage handling and created spaghetti loops between the two concepts.
1 parent 80ba8fb commit 8aba654

File tree

2 files changed

+29
-32
lines changed

2 files changed

+29
-32
lines changed

sentry_sdk/scope.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,7 @@ def get_dynamic_sampling_context(self):
530530

531531
baggage = self.get_baggage()
532532
if baggage is not None:
533-
self._propagation_context.dynamic_sampling_context = (
534-
baggage.dynamic_sampling_context()
535-
)
533+
self._propagation_context.baggage = baggage
536534

537535
return self._propagation_context.dynamic_sampling_context
538536

@@ -573,13 +571,7 @@ def get_baggage(self, *args, **kwargs):
573571

574572
# If this scope has a propagation context, return baggage from there
575573
if self._propagation_context is not None:
576-
dynamic_sampling_context = (
577-
self._propagation_context.dynamic_sampling_context
578-
)
579-
if dynamic_sampling_context is None:
580-
return Baggage.from_options(self)
581-
else:
582-
return Baggage(dynamic_sampling_context)
574+
return self._propagation_context.baggage or Baggage.from_options(self)
583575

584576
# Fall back to isolation scope's baggage. It always has one
585577
return self.get_isolation_scope().get_baggage()

sentry_sdk/tracing_utils.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ class PropagationContext:
411411
"_span_id",
412412
"parent_span_id",
413413
"parent_sampled",
414-
"dynamic_sampling_context",
414+
"baggage",
415415
)
416416

417417
def __init__(
@@ -421,6 +421,7 @@ def __init__(
421421
parent_span_id=None, # type: Optional[str]
422422
parent_sampled=None, # type: Optional[bool]
423423
dynamic_sampling_context=None, # type: Optional[Dict[str, str]]
424+
baggage=None, # type: Optional[Baggage]
424425
):
425426
# type: (...) -> None
426427
self._trace_id = trace_id
@@ -438,8 +439,12 @@ def __init__(
438439
Important when the parent span originated in an upstream service,
439440
because we want to sample the whole trace, or nothing from the trace."""
440441

441-
self.dynamic_sampling_context = dynamic_sampling_context
442-
"""Data that is used for dynamic sampling decisions."""
442+
self.baggage = baggage
443+
"""Parsed baggage header that is used for dynamic sampling decisions."""
444+
445+
"""DEPRECATED this only exists for backwards compat of constructor."""
446+
if baggage is None and dynamic_sampling_context is not None:
447+
self.baggage = Baggage(dynamic_sampling_context)
443448

444449
@classmethod
445450
def from_incoming_data(cls, incoming_data):
@@ -456,9 +461,8 @@ def from_incoming_data(cls, incoming_data):
456461

457462
baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
458463
if baggage_header:
459-
propagation_context.dynamic_sampling_context = Baggage.from_incoming_header(
460-
baggage_header
461-
).dynamic_sampling_context()
464+
baggage = Baggage.from_incoming_header(baggage_header)
465+
propagation_context.baggage = baggage
462466

463467
propagation_context._fill_sample_rand()
464468

@@ -493,6 +497,11 @@ def span_id(self, value):
493497
# type: (str) -> None
494498
self._span_id = value
495499

500+
@property
501+
def dynamic_sampling_context(self):
502+
# type: () -> Optional[Dict[str, Any]]
503+
return self.baggage.dynamic_sampling_context() if self.baggage else None
504+
496505
def update(self, other_dict):
497506
# type: (Dict[str, Any]) -> None
498507
"""
@@ -506,20 +515,20 @@ def update(self, other_dict):
506515

507516
def __repr__(self):
508517
# type: (...) -> str
509-
return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} dynamic_sampling_context={}>".format(
518+
return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} baggage={}>".format(
510519
self._trace_id,
511520
self._span_id,
512521
self.parent_span_id,
513522
self.parent_sampled,
514-
self.dynamic_sampling_context,
523+
self.baggage,
515524
)
516525

517526
def _fill_sample_rand(self):
518527
# type: () -> None
519528
"""
520-
Ensure that there is a valid sample_rand value in the dynamic_sampling_context.
529+
Ensure that there is a valid sample_rand value in the baggage.
521530
522-
If there is a valid sample_rand value in the dynamic_sampling_context, we keep it.
531+
If there is a valid sample_rand value in the baggage, we keep it.
523532
Otherwise, we generate a sample_rand value according to the following:
524533
525534
- If we have a parent_sampled value and a sample_rate in the DSC, we compute
@@ -532,23 +541,19 @@ def _fill_sample_rand(self):
532541
533542
The sample_rand is deterministically generated from the trace_id, if present.
534543
535-
This function does nothing if there is no dynamic_sampling_context.
544+
This function does nothing if there is no baggage.
536545
"""
537-
if self.dynamic_sampling_context is None:
546+
if self.baggage is None:
538547
return
539548

540-
sample_rand = try_convert(
541-
float, self.dynamic_sampling_context.get("sample_rand")
542-
)
549+
sample_rand = try_convert(float, self.baggage.sentry_items.get("sample_rand"))
543550
if sample_rand is not None and 0 <= sample_rand < 1:
544551
# sample_rand is present and valid, so don't overwrite it
545552
return
546553

547554
# Get the sample rate and compute the transformation that will map the random value
548555
# to the desired range: [0, 1), [0, sample_rate), or [sample_rate, 1).
549-
sample_rate = try_convert(
550-
float, self.dynamic_sampling_context.get("sample_rate")
551-
)
556+
sample_rate = try_convert(float, self.baggage.sentry_items.get("sample_rate"))
552557
lower, upper = _sample_rand_range(self.parent_sampled, sample_rate)
553558

554559
try:
@@ -564,15 +569,15 @@ def _fill_sample_rand(self):
564569
)
565570
return
566571

567-
self.dynamic_sampling_context["sample_rand"] = f"{sample_rand:.6f}" # noqa: E231
572+
self.baggage.sentry_items["sample_rand"] = f"{sample_rand:.6f}" # noqa: E231
568573

569574
def _sample_rand(self):
570575
# type: () -> Optional[str]
571-
"""Convenience method to get the sample_rand value from the dynamic_sampling_context."""
572-
if self.dynamic_sampling_context is None:
576+
"""Convenience method to get the sample_rand value from the baggage."""
577+
if self.baggage is None:
573578
return None
574579

575-
return self.dynamic_sampling_context.get("sample_rand")
580+
return self.baggage.sentry_items.get("sample_rand")
576581

577582

578583
class Baggage:

0 commit comments

Comments
 (0)