-
Notifications
You must be signed in to change notification settings - Fork 520
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
New Scope implementation based on OTel Context #3389
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
25307db
WIP tag tests
sl0thentr0py 934f404
wip scope overhaul
sl0thentr0py 95de20b
wip scope overhaul
sl0thentr0py 52cf4fa
Fix setup after rebase
sl0thentr0py 4505cb8
More scope stuff wip
sl0thentr0py bbbef71
Update tests/integrations/opentelemetry/test_potel.py
sl0thentr0py File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from sentry_sdk.integrations.opentelemetry.span_processor import ( # noqa: F401 | ||
SentrySpanProcessor, | ||
) | ||
# TODO-neel-potel fix circular imports | ||
# from sentry_sdk.integrations.opentelemetry.span_processor import ( # noqa: F401 | ||
# SentrySpanProcessor, | ||
# ) | ||
|
||
from sentry_sdk.integrations.opentelemetry.propagator import ( # noqa: F401 | ||
SentryPropagator, | ||
) | ||
# from sentry_sdk.integrations.opentelemetry.propagator import ( # noqa: F401 | ||
# SentryPropagator, | ||
# ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 18 additions & 12 deletions
30
sentry_sdk/integrations/opentelemetry/contextvars_context.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,32 @@ | ||
from opentelemetry.context import Context, create_key, get_value, set_value | ||
from opentelemetry.context import Context, get_value, set_value | ||
from opentelemetry.context.contextvars_context import ContextVarsRuntimeContext | ||
|
||
from sentry_sdk.scope import Scope | ||
|
||
|
||
_SCOPES_KEY = create_key("sentry_scopes") | ||
import sentry_sdk | ||
from sentry_sdk.integrations.opentelemetry.consts import ( | ||
SENTRY_SCOPES_KEY, | ||
SENTRY_FORK_ISOLATION_SCOPE_KEY, | ||
) | ||
|
||
|
||
class SentryContextVarsRuntimeContext(ContextVarsRuntimeContext): | ||
def attach(self, context): | ||
# type: (Context) -> object | ||
scopes = get_value(_SCOPES_KEY, context) | ||
scopes = get_value(SENTRY_SCOPES_KEY, context) | ||
should_fork_isolation_scope = context.pop( | ||
SENTRY_FORK_ISOLATION_SCOPE_KEY, False | ||
) | ||
|
||
if scopes and isinstance(scopes, tuple): | ||
(current_scope, isolation_scope) = scopes | ||
else: | ||
current_scope = Scope.get_current_scope() | ||
isolation_scope = Scope.get_isolation_scope() | ||
current_scope = sentry_sdk.get_current_scope() | ||
isolation_scope = sentry_sdk.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) | ||
new_scope = current_scope.fork() | ||
new_isolation_scope = ( | ||
isolation_scope.fork() if should_fork_isolation_scope else isolation_scope | ||
) | ||
new_scopes = (new_scope, new_isolation_scope) | ||
|
||
new_context = set_value(SENTRY_SCOPES_KEY, new_scopes, context) | ||
return super().attach(new_context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from typing import cast | ||
from contextlib import contextmanager | ||
|
||
from opentelemetry.context import get_value, set_value, attach, detach, get_current | ||
|
||
from sentry_sdk.scope import Scope, ScopeType | ||
from sentry_sdk.integrations.opentelemetry.consts import ( | ||
SENTRY_SCOPES_KEY, | ||
SENTRY_FORK_ISOLATION_SCOPE_KEY, | ||
) | ||
|
||
from sentry_sdk._types import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Tuple, Optional, Generator | ||
|
||
|
||
class PotelScope(Scope): | ||
@classmethod | ||
def _get_scopes(cls): | ||
# type: () -> Optional[Tuple[Scope, Scope]] | ||
""" | ||
Returns the current scopes tuple on the otel context. Internal use only. | ||
""" | ||
return cast("Optional[Tuple[Scope, Scope]]", get_value(SENTRY_SCOPES_KEY)) | ||
|
||
@classmethod | ||
def get_current_scope(cls): | ||
# type: () -> Scope | ||
""" | ||
Returns the current scope. | ||
""" | ||
return cls._get_current_scope() or _INITIAL_CURRENT_SCOPE | ||
|
||
@classmethod | ||
def _get_current_scope(cls): | ||
# type: () -> Optional[Scope] | ||
""" | ||
Returns the current scope without creating a new one. Internal use only. | ||
""" | ||
scopes = cls._get_scopes() | ||
return scopes[0] if scopes else None | ||
|
||
@classmethod | ||
def get_isolation_scope(cls): | ||
""" | ||
Returns the isolation scope. | ||
""" | ||
# type: () -> Scope | ||
return cls._get_isolation_scope() or _INITIAL_ISOLATION_SCOPE | ||
|
||
@classmethod | ||
def _get_isolation_scope(cls): | ||
# type: () -> Optional[Scope] | ||
""" | ||
Returns the isolation scope without creating a new one. Internal use only. | ||
""" | ||
scopes = cls._get_scopes() | ||
return scopes[1] if scopes else None | ||
|
||
|
||
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT) | ||
_INITIAL_ISOLATION_SCOPE = PotelScope(ty=ScopeType.ISOLATION) | ||
|
||
|
||
@contextmanager | ||
def isolation_scope(): | ||
# type: () -> Generator[Scope, None, None] | ||
context = set_value(SENTRY_FORK_ISOLATION_SCOPE_KEY, True) | ||
token = attach(context) | ||
try: | ||
yield PotelScope.get_isolation_scope() | ||
finally: | ||
detach(token) | ||
|
||
|
||
@contextmanager | ||
def new_scope(): | ||
# type: () -> Generator[Scope, None, None] | ||
token = attach(get_current()) | ||
try: | ||
yield PotelScope.get_current_scope() | ||
finally: | ||
detach(token) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we plan to include
Potel
in the final name of this class before it goes out to users? I am guessing our users will not know what "Potel" isThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're dropping the old span processor we can eventually just go with
SentrySpanProcessor
.