8000 fix: Ignore Celery's retry exceptions (#253) · etherscan-io/sentry-python@aaacb56 · GitHub
[go: up one dir, main page]

Skip to content

Commit aaacb56

Browse files
authored
fix: Ignore Celery's retry exceptions (getsentry#253)
* fix: Ignore Celery's retry exceptions * fix: Skipping
1 parent 8d2623d commit aaacb56

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

sentry_sdk/integrations/celery.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sys
44

5-
from celery.exceptions import SoftTimeLimitExceeded
5+
from celery.exceptions import SoftTimeLimitExceeded, Retry
66

77
from sentry_sdk.hub import Hub
88
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
@@ -82,6 +82,15 @@ def event_processor(event, hint):
8282
}
8383

8484
if "exc_info" in hint:
85+
with capture_internal_exceptions():
86+
if isinstance(hint["exc_info"][1], Retry):
87+
return None
88+
89+
if hasattr(task, "throws") and isinstance(
90+
hint["exc_info"][1], task.throws
91+
):
92+
return None
93+
8594
with capture_internal_exceptions():
8695
if issubclass(hint["exc_info"][0], SoftTimeLimitExceeded):
8796
event["fingerprint"] = [
@@ -90,12 +99,6 @@ def event_processor(event, hint):
9099
getattr(task, "name", task),
91100
]
92101

93-
with capture_internal_exceptions():
94-
if hasattr(task, "throws") and isinstance(
95-
hint["exc_info"][1], task.throws
96-
):
97-
return None
98-
99102
return event
100103

101104
return event_processor

tests/integrations/celery/test_celery.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,39 @@ def dummy_task(x, y):
103103
assert stack_lengths == [2]
104104
else:
105105
assert stack_lengths == [2, 2]
106+
107+
108+
@pytest.mark.skipif(
109+
(4, 2, 0) <= VERSION < (4, 2, 2),
110+
reason="https://github.com/celery/celery/issues/4661",
111+
)
112+
def test_retry(celery, capture_events):
113+
events = capture_events()
114+
failures = [True, True, False]
115+
runs = []
116+
117+
@celery.task(name="dummy_task", bind=True)
118+
def dummy_task(self):
119+
runs.append(1)
120+
try:
121+
if failures.pop(0):
122+
1 / 0
123+
except Exception as exc:
124+
self.retry(max_retries=2, exc=exc)
125+
126+
dummy_task.delay()
127+
128+
assert len(runs) == 3
129+
assert not events
130+
131+
failures = [True, True, True]
132+
runs = []
133+
134+
dummy_task.delay()
135+
136+
assert len(runs) == 3
137+
event, = events
138+
exceptions = event["exception"]["values"]
139+
140+
for e in exceptions:
141+
assert e["type"] == "ZeroDivisionError"

tox.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ envlist =
2222

2323
py3.7-sanic-0.8
2424

25-
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-celery-4
25+
{pypy,py2.7,py3.5,py3.6,py3.7,py3.8}-celery-{4.1,4.2}
2626
{pypy,py2.7}-celery-3
2727

2828
{py2.7,py3.7}-requests
@@ -65,7 +65,8 @@ deps =
6565
sanic: aiohttp
6666

6767
celery-3: Celery>=3.1,<4.0
68-
celery-4: Celery>=4.0,<5.0
68+
celery-4.1: Celery>=4.1,<4.2
69+
celery-4.2: Celery>=4.2,<4.3
6970

7071
requests: requests>=2.0
7172

0 commit comments

Comments
 (0)
0