8000 Add simple scope management whenever a context is attached · getsentry/sentry-python@e7cbb59 · GitHub
[go: up one dir, main page]

Skip to content

Commit e7cbb59

Browse files
committed
Add simple scope management whenever a context is attached
* create a new otel context `_SCOPES_KEY` that will hold a tuple of `(curent_scope, isolation_scope)` * the `current_scope` will always be forked (like on every span creation/context update in practice) * note that this is on `attach`, so not on all copy-on-write context object creation but only on apis such as [`trace.use_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L547) or [`tracer.start_as_current_span`](https://github.com/open-telemetry/opentelemetry-python/blob/ba22b165471bde2037620f2c850ab648a849fbc0/opentelemetry-api/src/opentelemetry/trace/__init__.py#L329) * basically every otel `context` fork corresponds to our `current_scope` fork * the `isolation_scope` currently will not be forked * these will later be updated, for instance when we update our top level scope apis that fork isolation scope, that will also have a corresponding change in this `attach` function
1 parent 28effd6 commit e7cbb59

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
from opentelemetry.context.context import Context # type: ignore
1+
from opentelemetry.context import Context, create_key, get_value, set_value
22
from opentelemetry.context.contextvars_context import ContextVarsRuntimeContext # type: ignore
33

4+
from sentry_sdk.scope import Scope
5+
6+
7+
_SCOPES_KEY = create_key("sentry_scopes")
8+
49

510
class SentryContextVarsRuntimeContext(ContextVarsRuntimeContext): # type: ignore
611
def attach(self, context):
712
# type: (Context) -> object
8-
# TODO-neel-potel do scope management
9-
return super().attach(context)
13+
scopes = get_value(_SCOPES_KEY, context)
14+
15+
if scopes and isinstance(scopes, tuple):
16+
(current_scope, isolation_scope) = scopes
17+
else:
18+
current_scope = Scope.get_current_scope()
19+
isolation_scope = Scope.get_isolation_scope()
20+
21+
# TODO-neel-potel fork isolation_scope too like JS
22+
# once we setup our own apis to pass through to otel
23+
new_scopes = (current_scope.fork(), isolation_scope)
24+
new_context = set_value(_SCOPES_KEY, new_scopes, context)
1025

11-
def detach(self, token):
12-
# type: (object) -> None
13-
# TODO-neel-potel not sure if we need anything here, see later
14-
super().detach(token)
26+
return super().attach(new_context)

sentry_sdk/integrations/opentelemetry/integration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def _import_by_path(path):
172172
def _setup_sentry_tracing():
173173
# type: () -> None
174174

175-
# TODO-neel-potel make sure lifecycle is correct
176175
# TODO-neel-potel contribute upstream so this is not necessary
177176
context._RUNTIME_CONTEXT = SentryContextVarsRuntimeContext()
178177

0 commit comments

Comments
 (0)
0