1
1
import weakref
2
2
3
+ import sentry_sdk
3
4
from sentry_sdk .consts import OP
4
5
from sentry_sdk .api import continue_trace
5
- from sentry_sdk .hub import Hub
6
6
from sentry_sdk .integrations import DidNotEnable , Integration
7
7
from sentry_sdk .integrations .logging import ignore_logger
8
8
from sentry_sdk .tracing import TRANSACTION_SOURCE_TASK
9
9
from sentry_sdk .scope import Scope
10
10
from sentry_sdk .utils import (
11
11
capture_internal_exceptions ,
12
+ ensure_integration_enabled ,
12
13
event_from_exception ,
13
14
format_timestamp ,
14
15
parse_version ,
@@ -51,18 +52,10 @@ def setup_once():
51
52
52
53
old_perform_job = Worker .perform_job
53
54
55
+ @ensure_integration_enabled (RqIntegration , old_perform_job )
54
56
def sentry_patched_perform_job (self , job , * args , ** kwargs ):
55
57
# type: (Any, Job, *Queue, **Any) -> bool
56
- hub = Hub .current
57
- integration = hub .get_integration (RqIntegration )
58
-
59
- if integration is None :
60
- return old_perform_job (self , job , * args , ** kwargs )
61
-
62
- client = hub .client
63
- assert client is not None
64
-
65
- with hub .push_scope () as scope :
58
+ with sentry_sdk .new_scope () as scope :
66
59
scope .clear_breadcrumbs ()
67
60
scope .add_event_processor (_make_event_processor (weakref .ref (job )))
68
61
@@ -76,7 +69,7 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
76
69
with capture_internal_exceptions ():
77
70
transaction .name = job .func_name
78
71
79
- with hub .start_transaction (
72
+ with sentry_sdk .start_transaction (
80
73
transaction , custom_sampling_context = {"rq_job" : job }
81
74
):
82
75
rv = old_perform_job (self , job , * args , ** kwargs )
@@ -85,7 +78,7 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
85
78
# We're inside of a forked process and RQ is
86
79
# about to call `os._exit`. Make sure that our
87
80
# events get sent out.
88
- client .flush ()
81
+ sentry_sdk . get_client () .flush ()
89
82
90
83
return rv
91
84
@@ -106,15 +99,14 @@ def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
106
99
107
100
old_enqueue_job = Queue .enqueue_job
108
101
102
+ @ensure_integration_enabled (RqIntegration , old_enqueue_job )
109
103
def sentry_patched_enqueue_job (self , job , ** kwargs ):
110
104
# type: (Queue, Any, **Any) -> Any
111
- hub = Hub .current
112
- if hub .get_integration (RqIntegration ) is not None :
113
- scope = Scope .get_current_scope ()
114
- if scope .span is not None :
115
- job .meta ["_sentry_trace_headers" ] = dict (
116
- scope .iter_trace_propagation_headers ()
117
- )
105
+ scope = Scope .get_current_scope ()
106
+ if scope .span is not None :
107
+ job .meta ["_sentry_trace_headers" ] = dict (
108
+ scope .iter_trace_propagation_headers ()
109
+ )
118
110
119
111
return old_enqueue_job (self , job , ** kwargs )
120
112
@@ -158,17 +150,12 @@ def event_processor(event, hint):
158
150
159
151
def _capture_exception (exc_info , ** kwargs ):
160
152
# type: (ExcInfo, **Any) -> None
161
- hub = Hub .current
162
- if hub .get_integration (RqIntegration ) is None :
163
- return
164
-
165
- # If an integration is there, a client has to be there.
166
- client = hub .client # type: Any
153
+ client = sentry_sdk .get_client ()
167
154
168
155
event , hint = event_from_exception (
169
156
exc_info ,
170
157
client_options = client .options ,
171
158
mechanism = {"type" : "rq" , "handled" : False },
172
159
)
173
160
174
- hub .capture_event (event , hint = hint )
161
+ sentry_sdk .capture_event (event , hint = hint )
0 commit comments