11import weakref
22
3+ import sentry_sdk
34from sentry_sdk .consts import OP
45from sentry_sdk .api import continue_trace
5- from sentry_sdk .hub import Hub
66from sentry_sdk .integrations import DidNotEnable , Integration
77from sentry_sdk .integrations .logging import ignore_logger
88from sentry_sdk .tracing import TRANSACTION_SOURCE_TASK
99from sentry_sdk .scope import Scope
1010from sentry_sdk .utils import (
1111 capture_internal_exceptions ,
12+ ensure_integration_enabled ,
1213 event_from_exception ,
1314 format_timestamp ,
1415 parse_version ,
@@ -51,18 +52,10 @@ def setup_once():
5152
5253 old_perform_job = Worker .perform_job
5354
55+ @ensure_integration_enabled (RqIntegration , old_perform_job )
5456 def sentry_patched_perform_job (self , job , * args , ** kwargs ):
5557 # 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 :
6659 scope .clear_breadcrumbs ()
6760 scope .add_event_processor (_make_event_processor (weakref .ref (job )))
6861
@@ -76,7 +69,7 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
7669 with capture_internal_exceptions ():
7770 transaction .name = job .func_name
7871
79- with hub .start_transaction (
72+ with sentry_sdk .start_transaction (
8073 transaction , custom_sampling_context = {"rq_job" : job }
8174 ):
8275 rv = old_perform_job (self , job , * args , ** kwargs )
@@ -85,7 +78,7 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
8578 # We're inside of a forked process and RQ is
8679 # about to call `os._exit`. Make sure that our
8780 # events get sent out.
88- client .flush ()
81+ sentry_sdk . get_client () .flush ()
8982
9083 return rv
9184
@@ -106,15 +99,14 @@ def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
10699
107100 old_enqueue_job = Queue .enqueue_job
108101
102+ @ensure_integration_enabled (RqIntegration , old_enqueue_job )
109103 def sentry_patched_enqueue_job (self , job , ** kwargs ):
110104 # 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+ )
118110
119111 return old_enqueue_job (self , job , ** kwargs )
120112
@@ -158,17 +150,12 @@ def event_processor(event, hint):
158150
159151def _capture_exception (exc_info , ** kwargs ):
160152 # 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 ()
167154
168155 event , hint = event_from_exception (
169156 exc_info ,
170157 client_options = client .options ,
171158 mechanism = {"type" : "rq" , "handled" : False },
172159 )
173160
174- hub .capture_event (event , hint = hint )
161+ sentry_sdk .capture_event (event , hint = hint )
0 commit comments