From e68e00b90c4b0aa9fda0ea6ce309fa144002ba15 Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Mon, 18 Nov 2024 18:45:43 +0100 Subject: [PATCH] Fix test scopes --- .../integrations/opentelemetry/integration.py | 6 +++++- sentry_sdk/scope.py | 4 ++-- tests/test_scope.py | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/sentry_sdk/integrations/opentelemetry/integration.py b/sentry_sdk/integrations/opentelemetry/integration.py index 944326a124..016ce91b52 100644 --- a/sentry_sdk/integrations/opentelemetry/integration.py +++ b/sentry_sdk/integrations/opentelemetry/integration.py @@ -44,6 +44,7 @@ def setup_once(): "Use at your own risk." ) + _setup_scope_context_management() _setup_sentry_tracing() _patch_readable_span() # _setup_instrumentors() @@ -68,12 +69,15 @@ def sentry_patched_readable_span(self): Span._readable_span = sentry_patched_readable_span -def _setup_sentry_tracing(): +def _setup_scope_context_management(): # type: () -> None import opentelemetry.context opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext() + +def _setup_sentry_tracing(): + # type: () -> None provider = TracerProvider(sampler=SentrySampler()) provider.add_span_processor(PotelSentrySpanProcessor()) trace.set_tracer_provider(provider) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 12b6c5aed6..54e6fc8928 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -376,7 +376,7 @@ def get_client(cls): This checks the current scope, the isolation scope and the global scope for a client. If no client is available a :py:class:`sentry_sdk.client.NonRecordingClient` is returned. """ - current_scope = cls._get_current_scope() + current_scope = cls.get_current_scope() try: client = current_scope.client except AttributeError: @@ -385,7 +385,7 @@ def get_client(cls): if client is not None and client.is_active(): return client - isolation_scope = cls._get_isolation_scope() + isolation_scope = cls.get_isolation_scope() try: client = isolation_scope.client except AttributeError: diff --git a/tests/test_scope.py b/tests/test_scope.py index 0dfa155d11..8b89d755b8 100644 --- a/tests/test_scope.py +++ b/tests/test_scope.py @@ -11,18 +11,29 @@ ) from sentry_sdk.client import Client, NonRecordingClient from sentry_sdk.scope import ( - Scope, + Scope as BaseScope, ScopeType, - use_isolation_scope, - use_scope, should_send_default_pii, ) +from sentry_sdk.integrations.opentelemetry.integration import ( + _setup_scope_context_management, +) +from sentry_sdk.integrations.opentelemetry.scope import ( + PotelScope as Scope, + use_scope, + use_isolation_scope, +) SLOTS_NOT_COPIED = {"client"} """__slots__ that are not copied when copying a Scope object.""" +@pytest.fixture(autouse=True) +def setup_otel_scope_management(): + _setup_scope_context_management() + + def test_copying(): s1 = Scope() s1.fingerprint = {} @@ -212,7 +223,7 @@ def test_get_isolation_scope(): def test_get_global_scope(): scope = Scope.get_global_scope() assert scope is not None - assert scope.__class__ == Scope + assert scope.__class__ == BaseScope assert scope._type == ScopeType.GLOBAL