Skip to content

Commit fccf50b

Browse files
authored
Cleanup span is not None checks (#3765)
1 parent 766f4c5 commit fccf50b

File tree

5 files changed

+36
-13
lines changed

5 files changed

+36
-13
lines changed

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,17 @@ def start_span(self, **kwargs):
125125
return POTelSpan(**kwargs, scope=self)
126126

127127

128-
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
129-
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
128+
_INITIAL_CURRENT_SCOPE = None
129+
_INITIAL_ISOLATION_SCOPE = None
130+
131+
132+
def _setup_initial_scopes():
133+
global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE
134+
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
135+
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
136+
137+
138+
_setup_initial_scopes()
130139

131140

132141
@contextmanager

sentry_sdk/integrations/rq.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,9 @@ def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
108108
@ensure_integration_enabled(RqIntegration, old_enqueue_job)
109109
def sentry_patched_enqueue_job(self, job, **kwargs):
110110
# type: (Queue, Any, **Any) -> Any
111-
scope = sentry_sdk.get_current_scope()
112-
if scope.span is not None:
113-
job.meta["_sentry_trace_headers"] = dict(
114-
scope.iter_trace_propagation_headers()
115-
)
111+
job.meta["_sentry_trace_headers"] = dict(
112+
sentry_sdk.get_current_scope().iter_trace_propagation_headers()
113+
)
116114

117115
return old_enqueue_job(self, job, **kwargs)
118116

sentry_sdk/scope.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,11 @@ def get_traceparent(self, *args, **kwargs):
497497
client = self.get_client()
498498

499499
# If we have an active span, return traceparent from there
500-
if has_tracing_enabled(client.options) and self.span is not None:
500+
if (
501+
has_tracing_enabled(client.options)
502+
and self.span is not None
503+
and self.span.is_valid
504+
):
501505
return self.span.to_traceparent()
502506

503507
# If this scope has a propagation context, return traceparent from there
@@ -521,7 +525,11 @@ def get_baggage(self, *args, **kwargs):
521525
client = self.get_client()
522526

523527
# If we have an active span, return baggage from there
524-
if has_tracing_enabled(client.options) and self.span is not None:
528+
if (
529+
has_tracing_enabled(client.options)
530+
and self.span is not None
531+
and self.span.is_valid
532+
):
525533
return self.span.to_baggage()
526534

527535
# If this scope has a propagation context, return baggage from there
@@ -610,7 +618,7 @@ def iter_trace_propagation_headers(self, *args, **kwargs):
610618
span = kwargs.pop("span", None)
611619
span = span or self.span
612620

613-
if has_tracing_enabled(client.options) and span is not None:
621+
if has_tracing_enabled(client.options) and span is not None and span.is_valid:
614622
for header in span.iter_headers():
615623
yield header
616624
else:
@@ -1311,7 +1319,11 @@ def _apply_contexts_to_event(self, event, hint, options):
13111319

13121320
# Add "trace" context
13131321
if contexts.get("trace") is None:
1314-
if has_tracing_enabled(options) and self._span is not None:
1322+
if (
1323+
has_tracing_enabled(options)
1324+
and self._span is not None
1325+
and self._span.is_valid
1326+
):
13151327
contexts["trace"] = self._span.get_trace_context()
13161328
else:
13171329
contexts["trace"] = self.get_trace_context()

sentry_sdk/tracing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,11 @@ def span_id(self):
13891389
# type: () -> str
13901390
return format_span_id(self._otel_span.get_span_context().span_id)
13911391

1392+
@property
1393+
def is_valid(self):
1394+
# type: () -> bool
1395+
return self._otel_span.get_span_context().is_valid
1396+
13921397
@property
13931398
def sampled(self):
13941399
# type: () -> Optional[bool]

tests/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ def clean_scopes():
7474
scope._isolation_scope.set(None)
7575
scope._current_scope.set(None)
7676

77-
potel_scope._INITIAL_CURRENT_SCOPE.clear()
78-
potel_scope._INITIAL_ISOLATION_SCOPE.clear()
77+
potel_scope._setup_initial_scopes()
7978

8079

8180
@pytest.fixture(autouse=True)

0 commit comments

Comments
 (0)