|
1 |
| -from opentelemetry.context.context import Context |
| 1 | +from opentelemetry.context import Context, create_key, get_value, set_value |
2 | 2 | from opentelemetry.context.contextvars_context import ContextVarsRuntimeContext
|
3 | 3 |
|
| 4 | +from sentry_sdk.scope import Scope |
| 5 | + |
| 6 | + |
| 7 | +_SCOPES_KEY = create_key("sentry_scopes") |
| 8 | + |
4 | 9 |
|
5 | 10 | class SentryContextVarsRuntimeContext(ContextVarsRuntimeContext):
|
6 | 11 | def attach(self, context):
|
7 | 12 | # 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) |
10 | 25 |
|
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