Skip to content

Commit 9c8bfff

Browse files
committed
Cleanup outgoing propagation_context logic
1 parent 2ce4379 commit 9c8bfff

File tree

3 files changed

+60
-62
lines changed

3 files changed

+60
-62
lines changed

sentry_sdk/scope.py

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -525,14 +525,12 @@ 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+
Deprecated: Logic moved to PropagationContext, don't use directly.
528530
"""
529531
if self._propagation_context is None:
530532
return None
531533

532-
baggage = self.get_baggage()
533-
if baggage is not None:
534-
self._propagation_context.baggage = baggage
535-
536534
return self._propagation_context.dynamic_sampling_context
537535

538536
def get_traceparent(self, *args, **kwargs):
@@ -547,16 +545,13 @@ def get_traceparent(self, *args, **kwargs):
547545
if has_tracing_enabled(client.options) and self.span is not None:
548546
return self.span.to_traceparent()
549547

550-
# 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
548+
# else return traceparent from the propagation context
549+
propagation_context = self.get_active_propagation_context()
550+
if propagation_context is not None:
551+
return propagation_context.get_traceparent()
557552

558-
# Fall back to isolation scope's traceparent. It always has one
559-
return self.get_isolation_scope().get_traceparent()
553+
# TODO-neel will never happen
554+
return None
560555

561556
def get_baggage(self, *args, **kwargs):
562557
# type: (Any, Any) -> Optional[Baggage]
@@ -570,12 +565,13 @@ def get_baggage(self, *args, **kwargs):
570565
if has_tracing_enabled(client.options) and self.span is not None:
571566
return self.span.to_baggage()
572567

573-
# 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)
568+
# else return baggage from the propagation context
569+
propagation_context = self.get_active_propagation_context()
570+
if propagation_context is not None:
571+
return propagation_context.get_baggage()
576572

577-
# Fall back to isolation scope's baggage. It always has one
578-
return self.get_isolation_scope().get_baggage()
573+
# TODO-neel will never happen
574+
return None
579575

580576
def get_trace_context(self):
581577
# type: () -> Dict[str, Any]
@@ -599,7 +595,7 @@ def get_trace_context(self):
599595
"trace_id": propagation_context.trace_id,
600596
"span_id": propagation_context.span_id,
601597
"parent_span_id": propagation_context.parent_span_id,
602-
"dynamic_sampling_context": self.get_dynamic_sampling_context(),
598+
"dynamic_sampling_context": propagation_context.dynamic_sampling_context,
603599
}
604600

605601
def trace_propagation_meta(self, *args, **kwargs):
@@ -616,36 +612,19 @@ def trace_propagation_meta(self, *args, **kwargs):
616612

617613
meta = ""
618614

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-
)
615+
for name, content in self.iter_trace_propagation_headers():
616+
meta += f'<meta name="{name}" content="{content}">'
632617

633618
return meta
634619

635620
def iter_headers(self):
636621
# type: () -> Iterator[Tuple[str, str]]
637622
"""
638623
Creates a generator which returns the `sentry-trace` and `baggage` headers from the Propagation Context.
624+
Deprecated: use PropagationContext.iter_headers instead.
639625
"""
640626
if self._propagation_context is not None:
641-
traceparent = self.get_traceparent()
642-
if traceparent is not None:
643-
yield SENTRY_TRACE_HEADER_NAME, traceparent
644-
645-
dsc = self.get_dynamic_sampling_context()
646-
if dsc is not None:
647-
baggage = Baggage(dsc).serialize()
648-
yield BAGGAGE_HEADER_NAME, baggage
627+
yield from self._propagation_context.iter_headers()
649628

650629
def iter_trace_propagation_headers(self, *args, **kwargs):
651630
# type: (Any, Any) -> Generator[Tuple[str, str], None, None]
@@ -671,23 +650,10 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
671650
for header in span.iter_headers():
672651
yield header
673652
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():
653+
propagation_context = self.get_active_propagation_context()
654+
if propagation_context is not None:
655+
for header in propagation_context.iter_headers():
678656
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
691657

692658
def get_active_propagation_context(self):
693659
# 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

tests/test_propagationcontext.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def test_empty_context():
2525

2626
assert ctx.parent_span_id is None
2727
assert ctx.parent_sampled is None
28-
assert ctx.dynamic_sampling_context is None
28+
assert ctx.dynamic_sampling_context == {}
2929

3030

3131
def test_context_with_values():
@@ -72,7 +72,7 @@ def test_property_setters():
7272
assert ctx.trace_id == "X234567890abcdef1234567890abcdef"
7373
assert ctx._span_id == "X234567890abcdef"
7474
assert ctx.span_id == "X234567890abcdef"
75-
assert ctx.dynamic_sampling_context is None
75+
assert ctx.dynamic_sampling_context == {}
7676

7777

7878
def test_update():
@@ -93,7 +93,7 @@ def test_update():
9393
assert ctx._span_id is not None
9494
assert ctx.parent_span_id == "Z234567890abcdef"
9595
assert not ctx.parent_sampled
96-
assert ctx.dynamic_sampling_context is None
96+
assert ctx.dynamic_sampling_context == {}
9797

9898
assert not hasattr(ctx, "foo")
9999

0 commit comments

Comments
 (0)