Skip to content

Commit 94587f8

Browse files
committed
Cleanup outgoing propagation_context logic
1 parent 2ce4379 commit 94587f8

File tree

2 files changed

+58
-59
lines changed

2 files changed

+58
-59
lines changed

sentry_sdk/scope.py

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -525,15 +525,14 @@ def get_dynamic_sampling_context(self):
525525
"""
526526
Returns the Dynamic Sampling Context from the Propagation Context.
527527
If not existing, creates a new one.
528-
"""
529-
if self._propagation_context is None:
530-
return None
531-
532-
baggage = self.get_baggage()
533-
if baggage is not None:
534-
self._propagation_context.baggage = baggage
535528
536-
return self._propagation_context.dynamic_sampling_context
529+
Deprecated: Logic moved to PropagationContext, don't use directly.
530+
"""
531+
return (
532+
self._propagation_context.dynamic_sampling_context
533+
if self._propagation_context
534+
else None
535+
)
537536

538537
def get_traceparent(self, *args, **kwargs):
539538
# type: (Any, Any) -> Optional[str]
@@ -548,15 +547,9 @@ def get_traceparent(self, *args, **kwargs):
548547
return self.span.to_traceparent()
549548

550549
# If this scope has a propagation context, return traceparent from there
551-
if self._propagation_context is not None:
552-
traceparent = "%s-%s" % (
553-
self._propagation_context.trace_id,
554-
self._propagation_context.span_id,
555-
)
556-
return traceparent
557-
558-
# Fall back to isolation scope's traceparent. It always has one
559-
return self.get_isolation_scope().get_traceparent()
550+
propagation_context = self.get_active_propagation_context()
551+
if propagation_context is not None:
552+
return propagation_context.get_traceparent()
560553

561554
def get_baggage(self, *args, **kwargs):
562555
# type: (Any, Any) -> Optional[Baggage]
@@ -571,11 +564,9 @@ def get_baggage(self, *args, **kwargs):
571564
return self.span.to_baggage()
572565

573566
# If this scope has a propagation context, return baggage from there
574-
if self._propagation_context is not None:
575-
return self._propagation_context.baggage or Baggage.from_options(self)
576-
577-
# Fall back to isolation scope's baggage. It always has one
578-
return self.get_isolation_scope().get_baggage()
567+
propagation_context = self.get_active_propagation_context()
568+
if propagation_context is not None:
569+
return propagation_context.get_baggage()
579570

580571
def get_trace_context(self):
581572
# type: () -> Dict[str, Any]
@@ -599,7 +590,7 @@ def get_trace_context(self):
599590
"trace_id": propagation_context.trace_id,
600591
"span_id": propagation_context.span_id,
601592
"parent_span_id": propagation_context.parent_span_id,
602-
"dynamic_sampling_context": self.get_dynamic_sampling_context(),
593+
"dynamic_sampling_context": propagation_context.dynamic_sampling_context,
603594
}
604595

605596
def trace_propagation_meta(self, *args, **kwargs):
@@ -616,36 +607,25 @@ def trace_propagation_meta(self, *args, **kwargs):
616607

617608
meta = ""
618609

619-
sentry_trace = self.get_traceparent()
620-
if sentry_trace is not None:
621-
meta += '<meta name="%s" content="%s">' % (
622-
SENTRY_TRACE_HEADER_NAME,
623-
sentry_trace,
624-
)
625-
626-
baggage = self.get_baggage()
627-
if baggage is not None:
628-
meta += '<meta name="%s" content="%s">' % (
629-
BAGGAGE_HEADER_NAME,
630-
baggage.serialize(),
631-
)
610+
for name, content in self.iter_trace_propagation_headers():
611+
meta += f'<meta name="{name}" content="{content}">'
632612

633613
return meta
634614

635615
def iter_headers(self):
636616
# type: () -> Iterator[Tuple[str, str]]
637617
"""
638618
Creates a generator which returns the `sentry-trace` and `baggage` headers from the Propagation Context.
619+
Deprecated: use PropagationContext.iter_headers instead.
639620
"""
640621
if self._propagation_context is not None:
641622
traceparent = self.get_traceparent()
642623
if traceparent is not None:
643624
yield SENTRY_TRACE_HEADER_NAME, traceparent
644625

645-
dsc = self.get_dynamic_sampling_context()
646-
if dsc is not None:
647-
baggage = Baggage(dsc).serialize()
648-
yield BAGGAGE_HEADER_NAME, baggage
626+
baggage = self.get_baggage()
627+
if baggage is not None:
628+
yield BAGGAGE_HEADER_NAME, baggage.serialize()
649629

650630
def iter_trace_propagation_headers(self, *args, **kwargs):
651631
# type: (Any, Any) -> Generator[Tuple[str, str], None, None]
@@ -671,23 +651,10 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
671651
for header in span.iter_headers():
672652
yield header
673653
else:
674-
# If this scope has a propagation context, return headers from there
675-
# (it could be that self is not the current scope nor the isolation scope)
676-
if self._propagation_context is not None:
677-
for header in self.iter_headers():
654+
propagation_context = self.get_active_propagation_context()
655+
if propagation_context is not None:
656+
for header in propagation_context.iter_headers():
678657
yield header
679-
else:
680-
# otherwise try headers from current scope
681-
current_scope = self.get_current_scope()
682-
if current_scope._propagation_context is not None:
683-
for header in current_scope.iter_headers():
684-
yield header
685-
else:
686-
# otherwise fall back to headers from isolation scope
687-
isolation_scope = self.get_isolation_scope()
688-
if isolation_scope._propagation_context is not None:
689-
for header in isolation_scope.iter_headers():
690-
yield header
691658

692659
def get_active_propagation_context(self):
693660
# type: () -> Optional[PropagationContext]

sentry_sdk/tracing_utils.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
from typing import Generator
3636
from typing import Optional
3737
from typing import Union
38+
from typing import Iterator
39+
from typing import Tuple
3840

3941
from types import FrameType
4042

@@ -506,7 +508,28 @@ def span_id(self, value):
506508
@property
507509
def dynamic_sampling_context(self):
508510
# type: () -> Optional[Dict[str, Any]]
509-
return self.baggage.dynamic_sampling_context() if self.baggage else None
511+
return self.get_baggage().dynamic_sampling_context()
512+
513+
def get_traceparent(self):
514+
# type: () -> str
515+
return f"{self.trace_id}-{self.span_id}"
516+
517+
def get_baggage(self):
518+
# type: () -> Baggage
519+
if self.baggage is None:
520+
self.baggage = Baggage.populate_from_propagation_context(self)
521+
return self.baggage
522+
523+
def iter_headers(self):
524+
# type: () -> Iterator[Tuple[str, str]]
525+
"""
526+
Creates a generator which returns the propagation_context's ``sentry-trace`` and ``baggage`` headers.
527+
"""
528+
yield SENTRY_TRACE_HEADER_NAME, self.get_traceparent()
529+
530+
baggage = self.get_baggage().serialize()
531+
if baggage:
532+
yield BAGGAGE_HEADER_NAME, baggage
510533

511534
def update(self, other_dict):
512535
# type: (Dict[str, Any]) -> None
@@ -649,18 +672,27 @@ def from_incoming_header(
649672
@classmethod
650673
def from_options(cls, scope):
651674
# type: (sentry_sdk.scope.Scope) -> Optional[Baggage]
675+
"""
676+
Deprecated: use populate_from_propagation_context
677+
"""
678+
if scope._propagation_context is None:
679+
return Baggage({})
652680

681+
return Baggage.populate_from_propagation_context(scope._propagation_context)
682+
683+
@classmethod
684+
def populate_from_propagation_context(cls, propagation_context):
685+
# type: (PropagationContext) -> Baggage
653686
sentry_items = {} # type: Dict[str, str]
654687
third_party_items = ""
655688
mutable = False
656689

657690
client = sentry_sdk.get_client()
658691

659-
if not client.is_active() or scope._propagation_context is None:
692+
if not client.is_active():
660693
return Baggage(sentry_items)
661694

662695
options = client.options
663-
propagation_context = scope._propagation_context
664696

665697
if propagation_context is not None:
666698
sentry_items["trace_id"] = propagation_context.trace_id

0 commit comments

Comments
 (0)