8000 Add context-friendly `use_scope`, `use_isolation_scope` (#3500) · getsentry/sentry-python@2b10d78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b10d78

Browse files
authored
Add context-friendly use_scope, use_isolation_scope (#3500)
1 parent f430c20 commit 2b10d78

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

sentry_sdk/integrations/opentelemetry/consts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# scope management keys
99
SENTRY_SCOPES_KEY = create_key("sentry_scopes")
1010
SENTRY_FORK_ISOLATION_SCOPE_KEY = create_key("sentry_fork_isolation_scope")
11+
SENTRY_USE_CURRENT_SCOPE_KEY = create_key("sentry_use_current_scope")
12+
SENTRY_USE_ISOLATION_SCOPE_KEY = create_key("sentry_use_isolation_scope")
1113

1214
OTEL_SENTRY_CONTEXT = "otel"
1315
SPAN_ORIGIN = "auto.otel"

sentry_sdk/integrations/opentelemetry/contextvars_context.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from sentry_sdk.integrations.opentelemetry.consts import (
66
SENTRY_SCOPES_KEY,
77
SENTRY_FORK_ISOLATION_SCOPE_KEY,
8+
SENTRY_USE_CURRENT_SCOPE_KEY,
9+
SENTRY_USE_ISOLATION_SCOPE_KEY,
810
)
911

1012

@@ -15,17 +17,27 @@ def attach(self, context):
1517
should_fork_isolation_scope = context.pop(
1618
SENTRY_FORK_ISOLATION_SCOPE_KEY, False
1719
)
20+
should_use_isolation_scope = context.pop(SENTRY_USE_ISOLATION_SCOPE_KEY, None)
21+
should_use_current_scope = context.pop(SENTRY_USE_CURRENT_SCOPE_KEY, None)
1822

1923
if scopes and isinstance(scopes, tuple):
2024
(current_scope, isolation_scope) = scopes
2125
else:
2226
current_scope = sentry_sdk.get_current_scope()
2327
isolation_scope = sentry_sdk.get_isolation_scope()
2428

25-
new_scope = current_scope.fork()
26-
new_isolation_scope = (
27-
isolation_scope.fork() if should_fork_isolation_scope else isolation_scope
28-
)
29+
if should_use_current_scope:
30+
new_scope = should_use_current_scope
31+
else:
32+
new_scope = current_scope.fork()
33+
34+
if should_use_isolation_scope:
35+
new_isolation_scope = should_use_isolation_scope
36+
elif should_fork_isolation_scope:
37+
new_isolation_scope = isolation_scope.fork()
38+
else:
39+
new_isolation_scope = isolation_scope
40+
2941
new_scopes = (new_scope, new_isolation_scope)
3042

3143
new_context = set_value(SENTRY_SCOPES_KEY, new_scopes, context)

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from sentry_sdk.integrations.opentelemetry.consts import (
88
SENTRY_SCOPES_KEY,
99
SENTRY_FORK_ISOLATION_SCOPE_KEY,
10+
SENTRY_USE_CURRENT_SCOPE_KEY,
11+
SENTRY_USE_ISOLATION_SCOPE_KEY,
1012
)
1113
from sentry_sdk.scope import Scope, ScopeType
1214
from sentry_sdk.tracing import POTelSpan
@@ -136,3 +138,27 @@ def new_scope():
136138
yield PotelScope.get_current_scope()
137139
finally:
138140
detach(token)
141+
142+
143+
@contextmanager
144+
def use_scope(scope):
145+
# type: (Scope) -> Generator[Scope, None, None]
146+
context = set_value(SENTRY_USE_CURRENT_SCOPE_KEY, scope)
147+
token = attach(context)
148+
149+
try:
150+
yield scope
151+
finally:
152+
detach(token)
153+
154+
155+
@contextmanager
156+
def use_isolation_scope(isolation_scope):
157+
# type: (Scope) -> Generator[Scope, None, None]
158+
context = set_value(SENTRY_USE_ISOLATION_SCOPE_KEY, isolation_scope)
159+
token = attach(context)
160+
161+
try:
162+
yield isolation_scope
163+
finally:
164+
detach(token)

0 commit comments

Comments
 (0)
0