Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sentry_sdk/_init_implementation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING

import sentry_sdk
from sentry_sdk.integrations.opentelemetry.scope import setup_scope_context_management

if TYPE_CHECKING:
from typing import Any, Optional
Expand All @@ -24,6 +25,7 @@ def _init(*args, **kwargs):
"""
client = sentry_sdk.Client(*args, **kwargs)
sentry_sdk.get_global_scope().set_client(client)
setup_scope_context_management()
_check_python_deprecations()


Expand Down
13 changes: 0 additions & 13 deletions sentry_sdk/integrations/opentelemetry/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
"""

from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.integrations.opentelemetry.scope import setup_initial_scopes
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator
from sentry_sdk.integrations.opentelemetry.span_processor import (
SentrySpanProcessor,
)
from sentry_sdk.integrations.opentelemetry.contextvars_context import (
SentryContextVarsRuntimeContext,
)
from sentry_sdk.integrations.opentelemetry.sampler import SentrySampler
from sentry_sdk.utils import logger

Expand Down Expand Up @@ -45,7 +41,6 @@ def setup_once():
"Use at your own risk."
)

_setup_scope_context_management()
_setup_sentry_tracing()
_patch_readable_span()
# _setup_instrumentors()
Expand All @@ -70,14 +65,6 @@ def sentry_patched_readable_span(self):
Span._readable_span = sentry_patched_readable_span


def _setup_scope_context_management():
# type: () -> None
import opentelemetry.context

opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
setup_initial_scopes()


def _setup_sentry_tracing():
# type: () -> None
provider = TracerProvider(sampler=SentrySampler())
Expand Down
12 changes: 11 additions & 1 deletion sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from contextlib import contextmanager

from opentelemetry.context import (
Context,
get_value,
set_value,
attach,
Expand All @@ -24,6 +23,9 @@
SENTRY_USE_ISOLATION_SCOPE_KEY,
TRACESTATE_SAMPLED_KEY,
)
from sentry_sdk.integrations.opentelemetry.contextvars_context import (
SentryContextVarsRuntimeContext,
)
from sentry_sdk.integrations.opentelemetry.utils import trace_state_from_baggage
from sentry_sdk.scope import Scope, ScopeType
from sentry_sdk.tracing import POTelSpan
Expand Down Expand Up @@ -152,6 +154,14 @@ def setup_initial_scopes():
attach(set_value(SENTRY_SCOPES_KEY, scopes))


def setup_scope_context_management():
# type: () -> None
import opentelemetry.context

opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
setup_initial_scopes()


@contextmanager
def isolation_scope():
# type: () -> Generator[Scope, None, None]
Expand Down
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def benchmark():


from sentry_sdk import scope
import sentry_sdk.integrations.opentelemetry.scope as potel_scope
from sentry_sdk.integrations.opentelemetry.scope import (
setup_scope_context_management,
setup_initial_scopes,
)


@pytest.fixture(autouse=True)
Expand All @@ -74,7 +77,7 @@ def clean_scopes():
scope._isolation_scope.set(None)
scope._current_scope.set(None)

potel_scope.setup_initial_scopes()
setup_initial_scopes()


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -187,6 +190,7 @@ def inner(*a, **kw):
kw.setdefault("transport", TestTransport())
client = sentry_sdk.Client(*a, **kw)
sentry_sdk.get_global_scope().set_client(client)
setup_scope_context_management()

if request.node.get_closest_marker("forked"):
# Do not run isolation if the test is already running in
Expand Down
6 changes: 2 additions & 4 deletions tests/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@
ScopeType,
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,
setup_scope_context_management,
)


Expand All @@ -31,7 +29,7 @@

@pytest.fixture(autouse=True)
def setup_otel_scope_management():
_setup_scope_context_management()
setup_scope_context_management()


def test_copying():
Expand Down
Loading