Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix initial scope handling #3837

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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/integrations/opentelemetry/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

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,
Expand Down Expand Up @@ -74,6 +75,7 @@ def _setup_scope_context_management():
import opentelemetry.context

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


def _setup_sentry_tracing():
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/opentelemetry/propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def inject(self, carrier, context=None, setter=default_setter):

# TODO-neel-potel check trace_propagation_targets
# TODO-neel-potel test propagator works with twp
for (key, value) in current_scope.iter_trace_propagation_headers():
for key, value in current_scope.iter_trace_propagation_headers():
setter.set(carrier, key, value)

@property
Expand Down
15 changes: 11 additions & 4 deletions sentry_sdk/integrations/opentelemetry/scope.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from typing import cast
from contextlib import contextmanager

from opentelemetry.context import get_value, set_value, attach, detach, get_current
from opentelemetry.context import (
Context,
get_value,
set_value,
attach,
detach,
get_current,
)
from opentelemetry.trace import (
SpanContext,
NonRecordingSpan,
Expand Down Expand Up @@ -136,13 +143,13 @@ def start_span(self, **kwargs):
_INITIAL_ISOLATION_SCOPE = None


def _setup_initial_scopes():
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()
scopes = (_INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE)
attach(set_value(SENTRY_SCOPES_KEY, scopes))


@contextmanager
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def clean_scopes():
scope._isolation_scope.set(None)
scope._current_scope.set(None)

potel_scope._setup_initial_scopes()
potel_scope.setup_initial_scopes()


@pytest.fixture(autouse=True)
Expand Down
10 changes: 5 additions & 5 deletions tests/integrations/stdlib/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from sentry_sdk import capture_exception, capture_message, start_transaction
from sentry_sdk import capture_exception, capture_message, start_span
from sentry_sdk.integrations.stdlib import StdlibIntegration
from tests.conftest import ApproxDict

Expand Down Expand Up @@ -59,7 +59,7 @@ def test_subprocess_basic(
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
events = capture_events()

with start_transaction(name="foo") as transaction:
with start_span(name="foo") as span:
args = [
sys.executable,
"-c",
Expand Down Expand Up @@ -110,7 +110,7 @@ def test_subprocess_basic(

assert os.environ == old_environ

assert transaction.trace_id in str(output)
assert span.trace_id in str(output)

capture_message("hi")

Expand Down Expand Up @@ -178,7 +178,7 @@ def test_subprocess_basic(
def test_subprocess_empty_env(sentry_init, monkeypatch):
monkeypatch.setenv("TEST_MARKER", "should_not_be_seen")
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
with start_transaction(name="foo"):
with start_span(name="foo"):
args = [
sys.executable,
"-c",
Expand All @@ -201,7 +201,7 @@ def test_subprocess_span_origin(sentry_init, capture_events):
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
events = capture_events()

with start_transaction(name="foo"):
with start_span(name="foo"):
args = [
sys.executable,
"-c",
Expand Down
Loading