Skip to content

Commit f416845

Browse files
authored
ref(rq): Use new scopes API (#2881)
1 parent f228f70 commit f416845

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

sentry_sdk/integrations/rq.py

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import weakref
22

3+
import sentry_sdk
34
from sentry_sdk.consts import OP
45
from sentry_sdk.api import continue_trace
5-
from sentry_sdk.hub import Hub
66
from sentry_sdk.integrations import DidNotEnable, Integration
77
from sentry_sdk.integrations.logging import ignore_logger
88
from sentry_sdk.tracing import TRANSACTION_SOURCE_TASK
99
from sentry_sdk.scope import Scope
1010
from 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

159151
def _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)

tests/integrations/rq/test_rq.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import rq
55
from fakeredis import FakeStrictRedis
66

7-
from sentry_sdk import configure_scope, start_transaction
7+
from sentry_sdk import start_transaction
88
from sentry_sdk.integrations.rq import RqIntegration
9+
from sentry_sdk.scope import Scope
910
from sentry_sdk.utils import parse_version
1011

1112

@@ -178,19 +179,17 @@ def test_tracing_disabled(
178179
queue = rq.Queue(connection=FakeStrictRedis())
179180
worker = rq.SimpleWorker([queue], connection=queue.connection)
180181

181-
with configure_scope() as scope:
182-
queue.enqueue(crashing_job, foo=None)
183-
worker.work(burst=True)
182+
scope = Scope.get_isolation_scope()
183+
queue.enqueue(crashing_job, foo=None)
184+
worker.work(burst=True)
184185

185-
(error_event,) = events
186+
(error_event,) = events
186187

187-
assert (
188-
error_event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
189-
)
190-
assert (
191-
error_event["contexts"]["trace"]["trace_id"]
192-
== scope._propagation_context["trace_id"]
193-
)
188+
assert error_event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
189+
assert (
190+
error_event["contexts"]["trace"]["trace_id"]
191+
== scope._propagation_context["trace_id"]
192+
)
194193

195194

196195
def test_transaction_no_error(

0 commit comments

Comments
 (0)