Skip to content

Commit b6a6231

Browse files
sl0thentr0pyszokeasaurusrex
authored andcommitted
Add simple scope management whenever a context is attached (#3159)
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 5bad7f1 commit b6a6231

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
from opentelemetry.context.context import Context
1+
from opentelemetry.context import Context, create_key, get_value, set_value
22
from opentelemetry.context.contextvars_context import ContextVarsRuntimeContext
33

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

510
class SentryContextVarsRuntimeContext(ContextVarsRuntimeContext):
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)

0 commit comments

Comments
 (0)