Skip to content

Commit 6e1b1cb

Browse files
authored
Fix initial scope handling (#3837)
1 parent a962fab commit 6e1b1cb

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

sentry_sdk/integrations/opentelemetry/integration.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77
from sentry_sdk.integrations import DidNotEnable, Integration
8+
from sentry_sdk.integrations.opentelemetry.scope import setup_initial_scopes
89
from sentry_sdk.integrations.opentelemetry.propagator import SentryPropagator
910
from sentry_sdk.integrations.opentelemetry.span_processor import (
1011
SentrySpanProcessor,
@@ -74,6 +75,7 @@ def _setup_scope_context_management():
7475
import opentelemetry.context
7576

7677
opentelemetry.context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
78+
setup_initial_scopes()
7779

7880

7981
def _setup_sentry_tracing():

sentry_sdk/integrations/opentelemetry/propagator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def inject(self, carrier, context=None, setter=default_setter):
9999

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

105105
@property

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from typing import cast
22
from contextlib import contextmanager
33

4-
from opentelemetry.context import get_value, set_value, attach, detach, get_current
4+
from opentelemetry.context import (
5+
Context,
6+
get_value,
7+
set_value,
8+
attach,
9+
detach,
10+
get_current,
11+
)
512
from opentelemetry.trace import (
613
SpanContext,
714
NonRecordingSpan,
@@ -136,13 +143,13 @@ def start_span(self, **kwargs):
136143
_INITIAL_ISOLATION_SCOPE = None
137144

138145

139-
def _setup_initial_scopes():
146+
def setup_initial_scopes():
140147
global _INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE
141148
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)
142149
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION)
143150

144-
145-
_setup_initial_scopes()
151+
scopes = (_INITIAL_CURRENT_SCOPE, _INITIAL_ISOLATION_SCOPE)
152+
attach(set_value(SENTRY_SCOPES_KEY, scopes))
146153

147154

148155
@contextmanager

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def clean_scopes():
7474
scope._isolation_scope.set(None)
7575
scope._current_scope.set(None)
7676

77-
potel_scope._setup_initial_scopes()
77+
potel_scope.setup_initial_scopes()
7878

7979

8080
@pytest.fixture(autouse=True)

tests/integrations/stdlib/test_subprocess.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import pytest
99

10-
from sentry_sdk import capture_exception, capture_message, start_transaction
10+
from sentry_sdk import capture_exception, capture_message, start_span
1111
from sentry_sdk.integrations.stdlib import StdlibIntegration
1212
from tests.conftest import ApproxDict
1313

@@ -59,7 +59,7 @@ def test_subprocess_basic(
5959
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
6060
events = capture_events()
6161

62-
with start_transaction(name="foo") as transaction:
62+
with start_span(name="foo") as span:
6363
args = [
6464
sys.executable,
6565
"-c",
@@ -110,7 +110,7 @@ def test_subprocess_basic(
110110

111111
assert os.environ == old_environ
112112

113-
assert transaction.trace_id in str(output)
113+
assert span.trace_id in str(output)
114114

115115
capture_message("hi")
116116

@@ -178,7 +178,7 @@ def test_subprocess_basic(
178178
def test_subprocess_empty_env(sentry_init, monkeypatch):
179179
monkeypatch.setenv("TEST_MARKER", "should_not_be_seen")
180180
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
181-
with start_transaction(name="foo"):
181+
with start_span(name="foo"):
182182
args = [
183183
sys.executable,
184184
"-c",
@@ -201,7 +201,7 @@ def test_subprocess_span_origin(sentry_init, capture_events):
201201
sentry_init(integrations=[StdlibIntegration()], traces_sample_rate=1.0)
202202
events = capture_events()
203203

204-
with start_transaction(name="foo"):
204+
with start_span(name="foo"):
205205
args = [
206206
sys.executable,
207207
"-c",

0 commit comments

Comments
 (0)