Skip to content

Commit 68ddcf4

Browse files
a-d-collinsuntitaker
authored andcommitted
Global scope setters (getsentry#540)
* master: (sentry_sdk/utils.py) add push_scope_decorator * master: (sentry_sdk/utils.py) fix iteritems incompatibility * master: (sentry_sdk/utils.py) reformat push_scope_decorator() to fit linter specs * push-scope-decorator: (utils.py) remove push_scope_decorator, (hub.py) update Hub.push_scope() to allow it to act as both a contentmanager and a decorator, (api.py, utils.py) add new scopemethod api methods {set_tag, set_extra, set_user, set_level} that allow for updating of the current scope * push-scope-decorator: (sentry_sdk/hub.py) have _PushScopeContextDecorator inherit from contextlib.ContextDecorator * push-scope-decorator: (sentry_sdk/hub.py) combine _PushScopeContextDecorator with _ScopeManager * push-scope-decorator: (hub.py) fix push_scope() typing * master: (sentry_sdk/_compat.py, sentry_sdk/hub.py) resolve py2 ContextDecorator compat issue, (sentry_sdk/api.py) update typing to match that of Hub.push_scope() * master: (sentry_sdk/scope.py) remove improper uses of @_attr_setter, (tests/test_basics.py) remove unneeded test_scope_leaks_cleaned_up() * master: (tests/test_basics.py) add test for push_scope as decorator, (sentry_sdk/_compat.py) satisfy linters * global-scope-setters: keep global scope setters and remove push_scope() 'as a decorator' code * global-scope-setters: (api.py) change 'current_scope' property to 'scope' * global-scope-setters: (hub.py) add new lines in docstring to match style in rest of file * global-scope-setters: (api.py) add global for set_context
1 parent 75f09c3 commit 68ddcf4

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

sentry_sdk/api.py

+55
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
if MYPY:
1010
from typing import Any
11+
from typing import Dict
1112
from typing import Optional
1213
from typing import overload
1314
from typing import Callable
@@ -36,6 +37,11 @@ def overload(x):
3637
"flush",
3738
"last_event_id",
3839
"start_span",
40+
"set_tag",
41+
"set_context",
42+
"set_extra",
43+
"set_user",
44+
"set_level",
3945
]
4046

4147

@@ -48,6 +54,15 @@ def hubmethod(f):
4854
return f
4955

5056

57+
def scopemethod(f):
58+
# type: (F) -> F
59+
f.__doc__ = "%s\n\n%s" % (
60+
"Alias for :py:meth:`sentry_sdk.Scope.%s`" % f.__name__,
61+
inspect.getdoc(getattr(Scope, f.__name__)),
62+
)
63+
return f
64+
65+
5166
@hubmethod
5267
def capture_event(
5368
event, # type: Event
@@ -163,6 +178,46 @@ def inner():
163178
return None
164179

165180

181+
@scopemethod # noqa
182+
def set_tag(key, value):
183+
# type: (str, Any) -> None
184+
hub = Hub.current
185+
if hub is not None:
186+
hub.scope.set_tag(key, value)
187+
188+
189+
@scopemethod # noqa
190+
def set_context(key, value):
191+
# type: (str, Any) -> None
192+
hub = Hub.current
193+
if hub is not None:
194+
hub.scope.set_context(key, value)
195+
196+
197+
@scopemethod # noqa
198+
def set_extra(key, value):
199+
# type: (str, Any) -> None
200+
hub = Hub.current
201+
if hub is not None:
202+
hub.scope.set_extra(key, value)
203+
204+
205+
@scopemethod # noqa
206+
def set_user(value):
207+
# type: (Dict[str, Any]) -> None
208+
hub = Hub.current
209+
if hub is not None:
210+
hub.scope.set_user(value)
211+
212+
213+
@scopemethod # noqa
214+
def set_level(value):
215+
# type: (str) -> None
216+
hub = Hub.current
217+
if hub is not None:
218+
hub.scope.set_level(value)
219+
220+
166221
@hubmethod
167222
def flush(
168223
timeout=None, # type: Optional[float]

sentry_sdk/hub.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ def client(self):
263263
"""Returns the current client on the hub."""
264264
return self._stack[-1][0]
265265

266+
@property
267+
def scope(self):
268+
# type: () -> Scope
269+
"""Returns the current scope on the hub."""
270+
return self._stack[-1][1]
271+
266272
def last_event_id(self):
267273
# type: () -> Optional[str]
268274
"""Returns the last event ID."""
@@ -483,8 +489,6 @@ def push_scope( # noqa
483489

484490
return _ScopeManager(self)
485491

486-
scope = push_scope
487-
488492
def pop_scope_unsafe(self):
489493
# type: () -> Tuple[Optional[Client], Scope]
490494
"""

sentry_sdk/scope.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ def clear(self):
120120
@_attr_setter
121121
def level(self, value):
122122
# type: (Optional[str]) -> None
123-
"""When set this overrides the level."""
123+
"""When set this overrides the level. Deprecated in favor of set_level."""
124+
self._level = value
125+
126+
def set_level(self, value):
127+
# type: (Optional[str]) -> None
128+
"""Sets the level for the scope."""
124129
self._level = value
125130

126131
@_attr_setter
@@ -141,7 +146,12 @@ def transaction(self, value):
141146
@_attr_setter
142147
def user(self, value):
143148
# type: (Dict[str, Any]) -> None
144-
"""When set a specific user is bound to the scope."""
149+
"""When set a specific user is bound to the scope. Deprecated in favor of set_user."""
150+
self._user = value
151+
152+
def set_user(self, value):
153+
# type: (Dict[str, Any]) -> None
154+
"""Sets a user for the scope."""
145155
self._user = value
146156

147157
@property

0 commit comments

Comments
 (0)