diff --git a/sentry_sdk/integrations/opentelemetry/scope.py b/sentry_sdk/integrations/opentelemetry/scope.py index 82828d61de..56df9a774a 100644 --- a/sentry_sdk/integrations/opentelemetry/scope.py +++ b/sentry_sdk/integrations/opentelemetry/scope.py @@ -125,8 +125,17 @@ def start_span(self, **kwargs): return POTelSpan(**kwargs, scope=self) -_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT) -_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION) +_INITIAL_CURRENT_SCOPE = None +_INITIAL_ISOLATION_SCOPE = None + + +def _setup_initial_scopes(): + global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE + _INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT) + _INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION) + + +_setup_initial_scopes() @contextmanager diff --git a/sentry_sdk/integrations/rq.py b/sentry_sdk/integrations/rq.py index 7e016bfa9a..06eebd9f94 100644 --- a/sentry_sdk/integrations/rq.py +++ b/sentry_sdk/integrations/rq.py @@ -108,11 +108,9 @@ def sentry_patched_handle_exception(self, job, *exc_info, **kwargs): @ensure_integration_enabled(RqIntegration, old_enqueue_job) def sentry_patched_enqueue_job(self, job, **kwargs): # type: (Queue, Any, **Any) -> Any - scope = sentry_sdk.get_current_scope() - if scope.span is not None: - job.meta["_sentry_trace_headers"] = dict( - scope.iter_trace_propagation_headers() - ) + job.meta["_sentry_trace_headers"] = dict( + sentry_sdk.get_current_scope().iter_trace_propagation_headers() + ) return old_enqueue_job(self, job, **kwargs) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 2a6700b178..48b8571b98 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -497,7 +497,11 @@ def get_traceparent(self, *args, **kwargs): client = self.get_client() # If we have an active span, return traceparent from there - if has_tracing_enabled(client.options) and self.span is not None: + if ( + has_tracing_enabled(client.options) + and self.span is not None + and self.span.is_valid + ): return self.span.to_traceparent() # If this scope has a propagation context, return traceparent from there @@ -521,7 +525,11 @@ def get_baggage(self, *args, **kwargs): client = self.get_client() # If we have an active span, return baggage from there - if has_tracing_enabled(client.options) and self.span is not None: + if ( + has_tracing_enabled(client.options) + and self.span is not None + and self.span.is_valid + ): return self.span.to_baggage() # If this scope has a propagation context, return baggage from there @@ -610,7 +618,7 @@ def iter_trace_propagation_headers(self, *args, **kwargs): span = kwargs.pop("span", None) span = span or self.span - if has_tracing_enabled(client.options) and span is not None: + if has_tracing_enabled(client.options) and span is not None and span.is_valid: for header in span.iter_headers(): yield header else: @@ -1311,7 +1319,11 @@ def _apply_contexts_to_event(self, event, hint, options): # Add "trace" context if contexts.get("trace") is None: - if has_tracing_enabled(options) and self._span is not None: + if ( + has_tracing_enabled(options) + and self._span is not None + and self._span.is_valid + ): contexts["trace"] = self._span.get_trace_context() else: contexts["trace"] = self.get_trace_context() diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 70744d2d71..a69a6f98be 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -1389,6 +1389,11 @@ def span_id(self): # type: () -> str return format_span_id(self._otel_span.get_span_context().span_id) + @property + def is_valid(self): + # type: () -> bool + return self._otel_span.get_span_context().is_valid + @property def sampled(self): # type: () -> Optional[bool] diff --git a/tests/conftest.py b/tests/conftest.py index c7ade0bcdc..94fdf55707 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -74,8 +74,7 @@ def clean_scopes(): scope._isolation_scope.set(None) scope._current_scope.set(None) - potel_scope._INITIAL_CURRENT_SCOPE.clear() - potel_scope._INITIAL_ISOLATION_SCOPE.clear() + potel_scope._setup_initial_scopes() @pytest.fixture(autouse=True)