From 4501cbaba664e59322324268ae1dafd8035f6b19 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 22 Feb 2024 13:16:03 +0100 Subject: [PATCH] Fixed tests --- sentry_sdk/api.py | 17 ++--------------- sentry_sdk/scope.py | 36 ++++++++++++++++++++---------------- tests/test_api.py | 9 +++------ 3 files changed, 25 insertions(+), 37 deletions(-) diff --git a/sentry_sdk/api.py b/sentry_sdk/api.py index 1d614b5ce8..3d08a25ada 100644 --- a/sentry_sdk/api.py +++ b/sentry_sdk/api.py @@ -318,14 +318,7 @@ def get_traceparent(): """ Returns the traceparent either from the active span or from the scope. """ - current_scope = Scope.get_current_scope() - traceparent = current_scope.get_traceparent() - - if traceparent is None: - isolation_scope = Scope.get_isolation_scope() - traceparent = isolation_scope.get_traceparent() - - return traceparent + return Scope.get_current_scope().get_traceparent() def get_baggage(): @@ -333,13 +326,7 @@ def get_baggage(): """ Returns Baggage either from the active span or from the scope. """ - current_scope = Scope.get_current_scope() - baggage = current_scope.get_baggage() - - if baggage is None: - isolation_scope = Scope.get_isolation_scope() - baggage = isolation_scope.get_baggage() - + baggage = Scope.get_current_scope().get_baggage() if baggage is not None: return baggage.serialize() diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index efc816b6ba..d8a0820710 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -501,14 +501,16 @@ def get_traceparent(self, *args, **kwargs): if has_tracing_enabled(client.options) and self.span is not None: return self.span.to_traceparent() - if self._propagation_context is None: - return None + # If this scope has a propagation context, return traceparent from there + if self._propagation_context is not None: + traceparent = "%s-%s" % ( + self._propagation_context["trace_id"], + self._propagation_context["span_id"], + ) + return traceparent - traceparent = "%s-%s" % ( - self._propagation_context["trace_id"], - self._propagation_context["span_id"], - ) - return traceparent + # Fall back to isolation scope's traceparent. It always has one + return Scope.get_isolation_scope().get_traceparent() def get_baggage(self, *args, **kwargs): # type: (Any, Any) -> Optional[Baggage] @@ -522,16 +524,18 @@ def get_baggage(self, *args, **kwargs): if has_tracing_enabled(client.options) and self.span is not None: return self.span.to_baggage() - if self._propagation_context is None: - return None + # If this scope has a propagation context, return baggage from there + if self._propagation_context is not None: + dynamic_sampling_context = self._propagation_context.get( + "dynamic_sampling_context" + ) + if dynamic_sampling_context is None: + return Baggage.from_options(self) + else: + return Baggage(dynamic_sampling_context) - dynamic_sampling_context = self._propagation_context.get( - "dynamic_sampling_context" - ) - if dynamic_sampling_context is None: - return Baggage.from_options(self) - else: - return Baggage(dynamic_sampling_context) + # Fall back to isolation scope's baggage. It always has one + return Scope.get_isolation_scope().get_baggage() def get_trace_context(self): # type: () -> Any diff --git a/tests/test_api.py b/tests/test_api.py index 2a69349555..a02d0320fe 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -13,7 +13,6 @@ ) from sentry_sdk.client import Client, NonRecordingClient -from sentry_sdk.hub import Hub from sentry_sdk.scope import Scope @@ -66,7 +65,7 @@ def test_traceparent_with_tracing_enabled(sentry_init): def test_traceparent_with_tracing_disabled(sentry_init): sentry_init() - propagation_context = Hub.current.scope._propagation_context + propagation_context = Scope.get_isolation_scope()._propagation_context expected_traceparent = "%s-%s" % ( propagation_context["trace_id"], propagation_context["span_id"], @@ -77,7 +76,7 @@ def test_traceparent_with_tracing_disabled(sentry_init): @pytest.mark.forked def test_baggage_with_tracing_disabled(sentry_init): sentry_init(release="1.0.0", environment="dev") - propagation_context = Hub.current.scope._propagation_context + propagation_context = Scope.get_isolation_scope()._propagation_context expected_baggage = ( "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0".format( propagation_context["trace_id"] @@ -113,9 +112,7 @@ def test_continue_trace(sentry_init): with start_transaction(transaction): assert transaction.name == "some name" - propagation_context = ( - Hub.current.scope._propagation_context - ) # TODO: because this is the current scope and continue_trace was done on isolation scope. + propagation_context = Scope.get_isolation_scope()._propagation_context assert propagation_context["trace_id"] == transaction.trace_id == trace_id assert propagation_context["parent_span_id"] == parent_span_id assert propagation_context["parent_sampled"] == parent_sampled