From 766f4c5b3c1946129395eddfe5458926883515e7 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 12 Nov 2024 16:08:08 +0100 Subject: [PATCH 1/2] Fix leftover starlette tests --- tests/integrations/starlette/test_starlette.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/integrations/starlette/test_starlette.py b/tests/integrations/starlette/test_starlette.py index 22ed10b7cb..a45c900f12 100644 --- a/tests/integrations/starlette/test_starlette.py +++ b/tests/integrations/starlette/test_starlette.py @@ -1244,6 +1244,7 @@ def test_transaction_http_method_default(sentry_init, capture_events): """ sentry_init( traces_sample_rate=1.0, + auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. integrations=[StarletteIntegration()], ) events = capture_events() @@ -1269,6 +1270,7 @@ def test_transaction_http_method_default(sentry_init, capture_events): def test_transaction_http_method_custom(sentry_init, capture_events): sentry_init( traces_sample_rate=1.0, + auto_enabling_integrations=False, # Make sure that httpx integration is not added, because it adds tracing information to the starlette test clients request. integrations=[ StarletteIntegration( http_methods_to_capture=( From fccf50bb406d5be14305067564a7afdaa45295c0 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Tue, 12 Nov 2024 16:51:57 +0100 Subject: [PATCH 2/2] Cleanup span is not None checks (#3765) --- .../integrations/opentelemetry/scope.py | 13 ++++++++++-- sentry_sdk/integrations/rq.py | 8 +++----- sentry_sdk/scope.py | 20 +++++++++++++++---- sentry_sdk/tracing.py | 5 +++++ tests/conftest.py | 3 +-- 5 files changed, 36 insertions(+), 13 deletions(-) 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)