Skip to content

Commit 76aa189

Browse files
author
BobReid
authored
fix(rq): Only capture exception if RQ job has failed (ignore retries) (#1076)
1 parent d7cf16c commit 76aa189

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

sentry_sdk/integrations/rq.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
import weakref
44

55
from sentry_sdk.hub import Hub
6-
from sentry_sdk.integrations import Integration, DidNotEnable
6+
from sentry_sdk.integrations import DidNotEnable, Integration
7+
from sentry_sdk.integrations.logging import ignore_logger
78
from sentry_sdk.tracing import Transaction
89
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
910

10-
1111
try:
12-
from rq.version import VERSION as RQ_VERSION
12+
from rq.queue import Queue
1313
from rq.timeouts import JobTimeoutException
14+
from rq.version import VERSION as RQ_VERSION
1415
from rq.worker import Worker
15-
from rq.queue import Queue
1616
except ImportError:
1717
raise DidNotEnable("RQ not installed")
1818

1919
from sentry_sdk._types import MYPY
2020

2121
if MYPY:
22-
from typing import Any
23-
from typing import Dict
24-
from typing import Callable
25-
26-
from rq.job import Job
22+
from typing import Any, Callable, Dict
2723

28-
from sentry_sdk.utils import ExcInfo
2924
from sentry_sdk._types import EventProcessor
25+
from sentry_sdk.utils import ExcInfo
26+
27+
from rq.job import Job
3028

3129

3230
class RqIntegration(Integration):
@@ -89,7 +87,9 @@ def sentry_patched_perform_job(self, job, *args, **kwargs):
8987

9088
def sentry_patched_handle_exception(self, job, *exc_info, **kwargs):
9189
# type: (Worker, Any, *Any, **Any) -> Any
92-
_capture_exception(exc_info) # type: ignore
90+
if job.is_failed:
91+
_capture_exception(exc_info) # type: ignore
92+
9393
return old_handle_exception(self, job, *exc_info, **kwargs)
9494

9595
Worker.handle_exception = sentry_patched_handle_exception
@@ -108,6 +108,8 @@ def sentry_patched_enqueue_job(self, job, **kwargs):
108108

109109
Queue.enqueue_job = sentry_patched_enqueue_job
110110

111+
ignore_logger("rq.worker")
112+
111113

112114
def _make_event_processor(weak_job):
113115
# type: (Callable[[], Job]) -> EventProcessor

tests/integrations/rq/test_rq.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from sentry_sdk.integrations.rq import RqIntegration
2-
31
import pytest
4-
52
from fakeredis import FakeStrictRedis
3+
from sentry_sdk.integrations.rq import RqIntegration
4+
65
import rq
76

87
try:
@@ -177,3 +176,19 @@ def test_traces_sampler_gets_correct_values_in_sampling_context(
177176
}
178177
)
179178
)
179+
180+
181+
@pytest.mark.skipif(
182+
rq.__version__.split(".") < ["1", "5"], reason="At least rq-1.5 required"
183+
)
184+
def test_job_with_retries(sentry_init, capture_events):
185+
sentry_init(integrations=[RqIntegration()])
186+
events = capture_events()
187+
188+
queue = rq.Queue(connection=FakeStrictRedis())
189+
worker = rq.SimpleWorker([queue], connection=queue.connection)
190+
191+
queue.enqueue(crashing_job, foo=42, retry=rq.Retry(max=1))
192+
worker.work(burst=True)
193+
194+
assert len(events) == 1

0 commit comments

Comments
 (0)