23
23
from typing import Any
24
24
from typing import Optional
25
25
from typing import Tuple
26
+ from typing import Dict
26
27
from typing import List
27
28
from typing import Callable
28
29
from typing import Generator
@@ -47,6 +48,24 @@ def overload(x):
47
48
_local = ContextVar ("sentry_current_hub" )
48
49
49
50
51
+ def _update_scope (base , scope_change , scope_kwargs ):
52
+ # type: (Scope, Optional[Any], Dict[str, Any]) -> Scope
53
+ if scope_change and scope_kwargs :
54
+ raise TypeError ("cannot provide scope and kwargs" )
55
+ if scope_change is not None :
56
+ final_scope = copy .copy (base )
57
+ if callable (scope_change ):
58
+ scope_change (final_scope )
59
+ else :
60
+ final_scope .update_from_scope (scope_change )
61
+ elif scope_kwargs :
62
+ final_scope = copy .copy (base )
63
+ final_scope .update_from_kwargs (scope_kwargs )
64
+ else :
65
+ final_scope = base
66
+ return final_scope
67
+
68
+
50
69
def _should_send_default_pii ():
51
70
# type: () -> bool
52
71
client = Hub .current .client
@@ -285,11 +304,14 @@ def capture_event(
285
304
self ,
286
305
event , # type: Event
287
306
hint = None , # type: Optional[Hint]
307
+ scope = None , # type: Optional[Any]
308
+ ** scope_args # type: Dict[str, Any]
288
309
):
289
310
# type: (...) -> Optional[str]
290
311
"""Captures an event. Alias of :py:meth:`sentry_sdk.Client.capture_event`.
291
312
"""
292
- client , scope = self ._stack [- 1 ]
313
+ client , top_scope = self ._stack [- 1 ]
314
+ scope = _update_scope (top_scope , scope , scope_args )
293
315
if client is not None :
294
316
rv = client .capture_event (event , hint , scope )
295
317
if rv is not None :
@@ -301,6 +323,8 @@ def capture_message(
301
323
self ,
302
324
message , # type: str
303
325
level = None , # type: Optional[str]
326
+ scope = None , # type: Optional[Any]
327
+ ** scope_args # type: Dict[str, Any]
304
328
):
305
329
# type: (...) -> Optional[str]
306
330
"""Captures a message. The message is just a string. If no level
@@ -312,10 +336,15 @@ def capture_message(
312
336
return None
313
337
if level is None :
314
338
level = "info"
315
- return self .capture_event ({"message" : message , "level" : level })
339
+ return self .capture_event (
340
+ {"message" : message , "level" : level }, scope = scope , ** scope_args
341
+ )
316
342
317
343
def capture_exception (
318
- self , error = None # type: Optional[Union[BaseException, ExcInfo]]
344
+ self ,
345
+ error = None , # type: Optional[Union[BaseException, ExcInfo]]
346
+ scope = None , # type: Optional[Any]
347
+ ** scope_args # type: Dict[str, Any]
319
348
):
320
349
# type: (...) -> Optional[str]
321
350
"""Captures an exception.
@@ -334,7 +363,7 @@ def capture_exception(
334
363
335
364
event , hint = event_from_exception (exc_info , client_options = client .options )
336
365
try :
337
- return self .capture_event (event , hint = hint )
366
+ return self .capture_event (event , hint = hint , scope = scope , ** scope_args )
338
367
except Exception :
339
368
self ._capture_internal_exception (sys .exc_info ())
340
369
0 commit comments