1- from sentry_sdk .hub import Hub
21from sentry_sdk .integrations import Integration
2+ from sentry_sdk .scope import Scope , ScopeType
33
44from spinach import signals
55
@@ -28,18 +28,20 @@ def setup_once():
2828
2929
3030def _job_started (namespace , job , ** kwargs ):
31- hub = Hub .current
3231
33- # Scopes are for error reporting
34- hub .push_scope ()
35- with hub .configure_scope () as scope :
36- scope .transaction = job .task_name
37- scope .clear_breadcrumbs ()
38- for attr in job .__slots__ :
39- scope .set_extra (attr , getattr (job , attr ))
32+ current_scope = Scope (ty = ScopeType .CURRENT )
33+ Scope .set_current_scope (current_scope )
34+
35+ isolation_scope = Scope (ty = ScopeType .ISOLATION )
36+ Scope .set_isolation_scope (isolation_scope )
37+
38+ isolation_scope .transaction = job .task_name
39+ isolation_scope .clear_breadcrumbs ()
40+ for attr in job .__slots__ :
41+ isolation_scope .set_extra (attr , getattr (job , attr ))
4042
4143 # Transactions and spans are for tracing
42- transaction = hub .start_transaction (
44+ transaction = isolation_scope .start_transaction (
4345 op = 'task' ,
4446 name = job .task_name
4547 )
@@ -50,31 +52,32 @@ def _job_started(namespace, job, **kwargs):
5052
5153
5254def _job_finished (namespace , job , ** kwargs ):
53- hub = Hub .current
54- with hub .configure_scope () as scope :
55- for attr in job .__slots__ :
56- scope .set_extra (attr , getattr (job , attr ))
57- hub .scope .transaction .__exit__ (None , None , None )
58- hub .pop_scope_unsafe ()
55+ isolation_scope = Scope .get_isolation_scope ()
56+ for attr in job .__slots__ :
57+ isolation_scope .set_extra (attr , getattr (job , attr ))
58+ transaction = isolation_scope .transaction
59+ if transaction is not None :
60+ transaction .__exit__ (None , None , None )
61+ Scope .set_current_scope (None )
62+ Scope .set_isolation_scope (None )
5963
6064
6165def _job_failed (namespace , job , ** kwargs ):
62- hub = Hub . current
63- with hub . configure_scope () as scope :
64- for attr in job . __slots__ :
65- scope .set_extra ( attr , getattr ( job , attr ) )
66- hub . capture_exception ()
67- hub . scope .transaction .set_status ("internal_error" )
66+ scope = Scope . get_isolation_scope ()
67+ for attr in job . __slots__ :
68+ scope . set_extra ( attr , getattr ( job , attr ))
69+ scope .capture_exception ( )
70+ if scope . transaction is not None :
71+ scope .transaction .set_status ("internal_error" )
6872
6973
7074def _job_schedule_retry (namespace , job , ** kwargs ):
71- hub = Hub .current
72- with hub .configure_scope () as scope :
73- for attr in job .__slots__ :
74- scope .set_extra (attr , getattr (job , attr ))
75- integration = hub .get_integration (SpinachIntegration )
75+ scope = Scope .get_isolation_scope ()
76+ for attr in job .__slots__ :
77+ scope .set_extra (attr , getattr (job , attr ))
78+ integration = scope .get_client ().get_integration (SpinachIntegration )
7679 if integration is None :
7780 return
7881
7982 if integration .send_retries :
80- hub .capture_exception ()
83+ scope .capture_exception ()
0 commit comments