From 3e2885322a633398d62e8f1dae6315eefec35a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Figea?= <59359380+malkovro@users.noreply.github.com> Date: Tue, 19 Nov 2024 16:51:29 +0100 Subject: [PATCH] fix(integrations): Check retries_left before capturing exception (#3803) Since rq/rq#1964 the job status is set to Failed before the handler decides whether to capture or not the exception while handle_job_failure has not yet been called so the job is not yet re-scheduled leading to all exceptions getting captured in RQ version >= 2.0. Related to #1076 Fixes #3707 --- sentry_sdk/integrations/rq.py | 10 +++++++--- tests/integrations/rq/test_rq.py | 5 ----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sentry_sdk/integrations/rq.py b/sentry_sdk/integrations/rq.py index c0df1c5e53..462f3ad30a 100644 --- a/sentry_sdk/integrations/rq.py +++ b/sentry_sdk/integrations/rq.py @@ -90,9 +90,13 @@ def sentry_patched_perform_job(self, job, *args, **kwargs): def sentry_patched_handle_exception(self, job, *exc_info, **kwargs): # type: (Worker, Any, *Any, **Any) -> Any - # Note, the order of the `or` here is important, - # because calling `job.is_failed` will change `_status`. - if job._status == JobStatus.FAILED or job.is_failed: + retry = ( + hasattr(job, "retries_left") + and job.retries_left + and job.retries_left > 0 + ) + failed = job._status == JobStatus.FAILED or job.is_failed + if failed and not retry: _capture_exception(exc_info) return old_handle_exception(self, job, *exc_info, **kwargs) diff --git a/tests/integrations/rq/test_rq.py b/tests/integrations/rq/test_rq.py index ffd6f458e1..e445b588be 100644 --- a/tests/integrations/rq/test_rq.py +++ b/tests/integrations/rq/test_rq.py @@ -254,11 +254,6 @@ def test_traces_sampler_gets_correct_values_in_sampling_context( @pytest.mark.skipif( parse_version(rq.__version__) < (1, 5), reason="At least rq-1.5 required" ) -@pytest.mark.skipif( - parse_version(rq.__version__) >= (2,), - reason="Test broke in RQ 2.0. Investigate and fix. " - "See https://github.com/getsentry/sentry-python/issues/3707.", -) def test_job_with_retries(sentry_init, capture_events): sentry_init(integrations=[RqIntegration()]) events = capture_events()