@@ -476,18 +476,12 @@ def get_dynamic_sampling_context(self):
476476 # type: () -> Optional[Dict[str, str]]
477477 """
478478 Returns the Dynamic Sampling Context from the Propagation Context.
479- If not existing, creates a new one.
480479 """
481- if self ._propagation_context is None :
482- return None
483-
484- baggage = self .get_baggage ()
485- if baggage is not None :
486- self ._propagation_context .dynamic_sampling_context = (
487- baggage .dynamic_sampling_context ()
488- )
489-
490- return self ._propagation_context .dynamic_sampling_context
480+ return (
481+ self ._propagation_context .dynamic_sampling_context
482+ if self ._propagation_context
483+ else None
484+ )
491485
492486 def get_traceparent (self , * args , ** kwargs ):
493487 # type: (Any, Any) -> Optional[str]
@@ -517,6 +511,7 @@ def get_baggage(self, *args, **kwargs):
517511 """
518512 Returns the Sentry "baggage" header containing trace information from the
519513 currently active span or the scopes Propagation Context.
514+ If not existing, creates a new one.
520515 """
521516 client = self .get_client ()
522517
@@ -525,14 +520,11 @@ def get_baggage(self, *args, **kwargs):
525520 return self .span .to_baggage ()
526521
527522 # If this scope has a propagation context, return baggage from there
523+ # populate a fresh one if it doesn't exist
528524 if self ._propagation_context is not None :
529- dynamic_sampling_context = (
530- self ._propagation_context .dynamic_sampling_context
531- )
532- if dynamic_sampling_context is None :
533- return Baggage .from_options (self )
534- else :
535- return Baggage (dynamic_sampling_context )
525+ if self ._propagation_context .baggage is None :
526+ self ._propagation_context .baggage = Baggage .from_options (self )
527+ return self ._propagation_context .baggage
536528
537529 # Fall back to isolation scope's baggage. It always has one
538530 return self .get_isolation_scope ().get_baggage ()
@@ -594,10 +586,9 @@ def iter_headers(self):
594586 if traceparent is not None :
595587 yield SENTRY_TRACE_HEADER_NAME , traceparent
596588
597- dsc = self .get_dynamic_sampling_context ()
598- if dsc is not None :
599- baggage = Baggage (dsc ).serialize ()
600- yield BAGGAGE_HEADER_NAME , baggage
589+ baggage = self .get_baggage ()
590+ if baggage is not None :
591+ yield BAGGAGE_HEADER_NAME , baggage .serialize ()
601592
602593 def iter_trace_propagation_headers (self , * args , ** kwargs ):
603594 # type: (Any, Any) -> Generator[Tuple[str, str], None, None]
0 commit comments