Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 17 additions & 31 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)

import typing
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, cast

if TYPE_CHECKING:
from collections.abc import Mapping
Expand Down Expand Up @@ -546,12 +546,7 @@ def get_traceparent(self, *args, **kwargs):
return self.span.to_traceparent()

# else return traceparent from the propagation context
propagation_context = self.get_active_propagation_context()
if propagation_context is not None:
return propagation_context.to_traceparent()

# TODO-neel will never happen
return None
return self.get_active_propagation_context().to_traceparent()

def get_baggage(self, *args, **kwargs):
# type: (Any, Any) -> Optional[Baggage]
Expand All @@ -566,12 +561,7 @@ def get_baggage(self, *args, **kwargs):
return self.span.to_baggage()

# else return baggage from the propagation context
propagation_context = self.get_active_propagation_context()
if propagation_context is not None:
return propagation_context.get_baggage()

# TODO-neel will never happen
return None
return self.get_active_propagation_context().get_baggage()

def get_trace_context(self):
# type: () -> Dict[str, Any]
Expand All @@ -588,8 +578,6 @@ def get_trace_context(self):
return {"trace_id": trace_id, "span_id": span_id}

propagation_context = self.get_active_propagation_context()
if propagation_context is None:
return {}

return {
"trace_id": propagation_context.trace_id,
Expand Down Expand Up @@ -650,13 +638,11 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
for header in span.iter_headers():
yield header
else:
propagation_context = self.get_active_propagation_context()
if propagation_context is not None:
for header in propagation_context.iter_headers():
yield header
for header in self.get_active_propagation_context().iter_headers():
yield header

def get_active_propagation_context(self):
# type: () -> Optional[PropagationContext]
# type: () -> PropagationContext
if self._propagation_context is not None:
return self._propagation_context

Expand All @@ -665,10 +651,10 @@ def get_active_propagation_context(self):
return current_scope._propagation_context

isolation_scope = self.get_isolation_scope()
if isolation_scope._propagation_context is not None:
return isolation_scope._propagation_context

return None
# should actually never happen, but just in case someone calls scope.clear
if isolation_scope._propagation_context is None:
isolation_scope._propagation_context = PropagationContext()
return isolation_scope._propagation_context

def clear(self):
# type: () -> None
Expand Down Expand Up @@ -1057,10 +1043,11 @@ def start_transaction(
# update the sample rate in the dsc
if transaction.sample_rate is not None:
propagation_context = self.get_active_propagation_context()
if propagation_context:
baggage = propagation_context.baggage
if baggage is not None:
baggage.sentry_items["sample_rate"] = str(transaction.sample_rate)
baggage = propagation_context.baggage

if baggage is not None:
baggage.sentry_items["sample_rate"] = str(transaction.sample_rate)

if transaction._baggage:
transaction._baggage.sentry_items["sample_rate"] = str(
transaction.sample_rate
Expand Down Expand Up @@ -1134,8 +1121,7 @@ def start_span(self, instrumenter=INSTRUMENTER.SENTRY, **kwargs):
# New spans get the `trace_id` from the scope
if "trace_id" not in kwargs:
propagation_context = self.get_active_propagation_context()
if propagation_context is not None:
kwargs["trace_id"] = propagation_context.trace_id
kwargs["trace_id"] = propagation_context.trace_id

span = Span(**kwargs)
else:
Expand All @@ -1154,7 +1140,7 @@ def continue_trace(
self.generate_propagation_context(environ_or_headers)

# generate_propagation_context ensures that the propagation_context is not None.
propagation_context = typing.cast(PropagationContext, self._propagation_context)
propagation_context = cast(PropagationContext, self._propagation_context)

optional_kwargs = {}
if name:
Expand Down