1
1
import sys
2
2
3
+ import sentry_sdk
3
4
from sentry_sdk ._types import TYPE_CHECKING
4
- from sentry_sdk import Hub
5
5
from sentry_sdk .consts import OP
6
- from sentry_sdk .hub import _should_send_default_pii
7
6
from sentry_sdk .integrations import DidNotEnable , Integration
8
7
from sentry_sdk .integrations .logging import ignore_logger
9
- from sentry_sdk .scope import Scope
8
+ from sentry_sdk .scope import Scope , should_send_default_pii
10
9
from sentry_sdk .tracing import Transaction , TRANSACTION_SOURCE_TASK
11
10
from sentry_sdk .utils import (
12
11
capture_internal_exceptions ,
12
+ ensure_integration_enabled ,
13
+ ensure_integration_enabled_async ,
13
14
event_from_exception ,
14
15
SENSITIVE_DATA_SUBSTITUTE ,
15
16
parse_version ,
@@ -70,14 +71,10 @@ def patch_enqueue_job():
70
71
# type: () -> None
71
72
old_enqueue_job = ArqRedis .enqueue_job
72
73
74
+ @ensure_integration_enabled_async (ArqIntegration , old_enqueue_job )
73
75
async def _sentry_enqueue_job (self , function , * args , ** kwargs ):
74
76
# type: (ArqRedis, str, *Any, **Any) -> Optional[Job]
75
- hub = Hub .current
76
-
77
- if hub .get_integration (ArqIntegration ) is None :
78
- return await old_enqueue_job (self , function , * args , ** kwargs )
79
-
80
- with hub .start_span (op = OP .QUEUE_SUBMIT_ARQ , description = function ):
77
+ with sentry_sdk .start_span (op = OP .QUEUE_SUBMIT_ARQ , description = function ):
81
78
return await old_enqueue_job (self , function , * args , ** kwargs )
82
79
83
80
ArqRedis .enqueue_job = _sentry_enqueue_job
@@ -87,14 +84,10 @@ def patch_run_job():
87
84
# type: () -> None
88
85
old_run_job = Worker .run_job
89
86
87
+ @ensure_integration_enabled_async (ArqIntegration , old_run_job )
90
88
async def _sentry_run_job (self , job_id , score ):
91
89
# type: (Worker, str, int) -> None
92
- hub = Hub (Hub .current )
93
-
94
- if hub .get_integration (ArqIntegration ) is None :
95
- return await old_run_job (self , job_id , score )
96
-
97
- with hub .push_scope () as scope :
90
+ with sentry_sdk .isolation_scope () as scope :
98
91
scope ._name = "arq"
99
92
scope .clear_breadcrumbs ()
100
93
@@ -105,7 +98,7 @@ async def _sentry_run_job(self, job_id, score):
105
98
source = TRANSACTION_SOURCE_TASK ,
106
99
)
107
100
108
- with hub .start_transaction (transaction ):
101
+ with sentry_sdk .start_transaction (transaction ):
109
102
return await old_run_job (self , job_id , score )
110
103
111
104
Worker .run_job = _sentry_run_job
@@ -127,7 +120,7 @@ def _capture_exception(exc_info):
127
120
client_options = Scope .get_client ().options ,
128
121
mechanism = {"type" : ArqIntegration .identifier , "handled" : False },
129
122
)
130
- scope .capture_event (event , hint = hint )
123
+ sentry_sdk .capture_event (event , hint = hint )
131
124
132
125
133
126
def _make_event_processor (ctx , * args , ** kwargs ):
@@ -148,10 +141,10 @@ def event_processor(event, hint):
148
141
extra ["arq-job" ] = {
149
142
"task" : ctx ["job_name" ],
150
143
"args" : (
151
- args if _should_send_default_pii () else SENSITIVE_DATA_SUBSTITUTE
144
+ args if should_send_default_pii () else SENSITIVE_DATA_SUBSTITUTE
152
145
),
153
146
"kwargs" : (
154
- kwargs if _should_send_default_pii () else SENSITIVE_DATA_SUBSTITUTE
147
+ kwargs if should_send_default_pii () else SENSITIVE_DATA_SUBSTITUTE
155
148
),
156
149
"retry" : ctx ["job_try" ],
157
150
}
@@ -163,13 +156,11 @@ def event_processor(event, hint):
163
156
164
157
def _wrap_coroutine (name , coroutine ):
165
158
# type: (str, WorkerCoroutine) -> WorkerCoroutine
159
+
160
+ @ensure_integration_enabled_async (ArqIntegration , coroutine )
166
161
async def _sentry_coroutine (ctx , * args , ** kwargs ):
167
162
# type: (Dict[Any, Any], *Any, **Any) -> Any
168
- hub = Hub .current
169
- if hub .get_integration (ArqIntegration ) is None :
170
- return await coroutine (ctx , * args , ** kwargs )
171
-
172
- hub .scope .add_event_processor (
163
+ Scope .get_isolation_scope ().add_event_processor (
173
164
_make_event_processor ({** ctx , "job_name" : name }, * args , ** kwargs )
174
165
)
175
166
@@ -189,13 +180,9 @@ def patch_create_worker():
189
180
# type: () -> None
190
181
old_create_worker = arq .worker .create_worker
191
182
183
+ @ensure_integration_enabled (ArqIntegration , old_create_worker )
192
184
def _sentry_create_worker (* args , ** kwargs ):
193
185
# type: (*Any, **Any) -> Worker
194
- hub = Hub .current
195
-
196
- if hub .get_integration (ArqIntegration ) is None :
197
- return old_create_worker (* args , ** kwargs )
198
-
199
186
settings_cls = args [0 ]
200
187
201
188
if hasattr (settings_cls , "functions" ):
0 commit comments