Skip to content

Commit 7a0d9d6

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 7a0d9d6

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-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: 23 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,8 @@ 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."""
443444

444445
@classmethod
445446
def from_incoming_data(cls, incoming_data):
@@ -456,9 +457,8 @@ def from_incoming_data(cls, incoming_data):
456457

457458
baggage_header = normalized_data.get(BAGGAGE_HEADER_NAME)
458459
if baggage_header:
459-
propagation_context.dynamic_sampling_context = Baggage.from_incoming_header(
460-
baggage_header
461-
).dynamic_sampling_context()
460+
baggage = Baggage.from_incoming_header(baggage_header)
461+
propagation_context.baggage = baggage
462462

463463
propagation_context._fill_sample_rand()
464464

@@ -493,6 +493,11 @@ def span_id(self, value):
493493
# type: (str) -> None
494494
self._span_id = value
495495

496+
@property
497+
def dynamic_sampling_context(self):
498+
# type: () -> Optional[Dict[str, Any]]
499+
return self.baggage.dynamic_sampling_context() if self.baggage else None
500+
496501
def update(self, other_dict):
497502
# type: (Dict[str, Any]) -> None
498503
"""
@@ -506,20 +511,20 @@ def update(self, other_dict):
506511

507512
def __repr__(self):
508513
# type: (...) -> str
509-
return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} dynamic_sampling_context={}>".format(
514+
return "<PropagationContext _trace_id={} _span_id={} parent_span_id={} parent_sampled={} baggage={}>".format(
510515
self._trace_id,
511516
self._span_id,
512517
self.parent_span_id,
513518
self.parent_sampled,
514-
self.dynamic_sampling_context,
519+
self.baggage,
515520
)
516521

517522
def _fill_sample_rand(self):
518523
# type: () -> None
519524
"""
520-
Ensure that there is a valid sample_rand value in the dynamic_sampling_context.
525+
Ensure that there is a valid sample_rand value in the baggage.
521526
522-
If there is a valid sample_rand value in the dynamic_sampling_context, we keep it.
527+
If there is a valid sample_rand value in the baggage, we keep it.
523528
Otherwise, we generate a sample_rand value according to the following:
524529
525530
- If we have a parent_sampled value and a sample_rate in the DSC, we compute
@@ -532,23 +537,19 @@ def _fill_sample_rand(self):
532537
533538
The sample_rand is deterministically generated from the trace_id, if present.
534539
535-
This function does nothing if there is no dynamic_sampling_context.
540+
This function does nothing if there is no baggage.
536541
"""
537-
if self.dynamic_sampling_context is None:
542+
if self.baggage is None:
538543
return
539544

540-
sample_rand = try_convert(
541-
float, self.dynamic_sampling_context.get("sample_rand")
542-
)
545+
sample_rand = try_convert(float, self.baggage.sentry_items.get("sample_rand"))
543546
if sample_rand is not None and 0 <= sample_rand < 1:
544547
# sample_rand is present and valid, so don't overwrite it
545548
return
546549

547550
# Get the sample rate and compute the transformation that will map the random value
548551
# 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-
)
552+
sample_rate = try_convert(float, self.baggage.sentry_items.get("sample_rate"))
552553
lower, upper = _sample_rand_range(self.parent_sampled, sample_rate)
553554

554555
try:
@@ -564,15 +565,15 @@ def _fill_sample_rand(self):
564565
)
565566
return
566567

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

569570
def _sample_rand(self):
570571
# 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:
572+
"""Convenience method to get the sample_rand value from the baggage."""
573+
if self.baggage is None:
573574
return None
574575

575-
return self.dynamic_sampling_context.get("sample_rand")
576+
return self.baggage.sentry_items.get("sample_rand")
576577

577578

578579
class Baggage:

0 commit comments

Comments
 (0)