Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple scope management whenever a context is attached #3159

Merged
merged 3 commits into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions sentry_sdk/integrations/opentelemetry/contextvars_context.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
from opentelemetry.context.context import Context
from opentelemetry.context import Context, create_key, get_value, set_value
from opentelemetry.context.contextvars_context import ContextVarsRuntimeContext

from sentry_sdk.scope import Scope


_SCOPES_KEY = create_key("sentry_scopes")


class SentryContextVarsRuntimeContext(ContextVarsRuntimeContext):
def attach(self, context):
# type: (Context) -> object
# TODO-neel-potel do scope management
return super().attach(context)
scopes = get_value(_SCOPES_KEY, context)

if scopes and isinstance(scopes, tuple):
(current_scope, isolation_scope) = scopes
else:
current_scope = Scope.get_current_scope()
isolation_scope = Scope.get_isolation_scope()

# TODO-neel-potel fork isolation_scope too like JS
# once we setup our own apis to pass through to otel
new_scopes = (current_scope.fork(), isolation_scope)
new_context = set_value(_SCOPES_KEY, new_scopes, context)

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