Skip to content

Global scope setters #540

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

Merged
merged 14 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
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
55 changes: 55 additions & 0 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

if MYPY:
from typing import Any
from typing import Dict
from typing import Optional
from typing import overload
from typing import Callable
Expand Down Expand Up @@ -36,6 +37,11 @@ def overload(x):
"flush",
"last_event_id",
"start_span",
"set_tag",
"set_context",
"set_extra",
"set_user",
"set_level",
]


Expand All @@ -48,6 +54,15 @@ def hubmethod(f):
return f


def scopemethod(f):
# type: (F) -> F
f.__doc__ = "%s\n\n%s" % (
"Alias for :py:meth:`sentry_sdk.Scope.%s`" % f.__name__,
inspect.getdoc(getattr(Scope, f.__name__)),
)
return f


@hubmethod
def capture_event(
event, # type: Event
Expand Down Expand Up @@ -163,6 +178,46 @@ def inner():
return None


@scopemethod # noqa
def set_tag(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_tag(key, value)


@scopemethod # noqa
def set_context(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_context(key, value)


@scopemethod # noqa
def set_extra(key, value):
# type: (str, Any) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_extra(key, value)


@scopemethod # noqa
def set_user(value):
# type: (Dict[str, Any]) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_user(value)


@scopemethod # noqa
def set_level(value):
# type: (str) -> None
hub = Hub.current
if hub is not None:
hub.scope.set_level(value)


@hubmethod
def flush(
timeout=None, # type: Optional[float]
Expand Down
8 changes: 6 additions & 2 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ def client(self):
"""Returns the current client on the hub."""
return self._stack[-1][0]

@property
def scope(self):
# type: () -> Scope
"""Returns the current scope on the hub."""
return self._stack[-1][1]

def last_event_id(self):
# type: () -> Optional[str]
"""Returns the last event ID."""
Expand Down Expand Up @@ -483,8 +489,6 @@ def push_scope( # noqa

return _ScopeManager(self)

scope = push_scope
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine btw. We never documented this method


def pop_scope_unsafe(self):
# type: () -> Tuple[Optional[Client], Scope]
"""
Expand Down
14 changes: 12 additions & 2 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ def clear(self):
@_attr_setter
def level(self, value):
# type: (Optional[str]) -> None
"""When set this overrides the level."""
"""When set this overrides the level. Deprecated in favor of set_level."""
self._level = value

def set_level(self, value):
# type: (Optional[str]) -> None
"""Sets the level for the scope."""
self._level = value

@_attr_setter
Expand All @@ -141,7 +146,12 @@ def transaction(self, value):
@_attr_setter
def user(self, value):
# type: (Dict[str, Any]) -> None
"""When set a specific user is bound to the scope."""
"""When set a specific user is bound to the scope. Deprecated in favor of set_user."""
self._user = value

def set_user(self, value):
# type: (Dict[str, Any]) -> None
"""Sets a user for the scope."""
self._user = value

@property
Expand Down