Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import sys

from celery.exceptions import SoftTimeLimitExceeded
from celery.exceptions import SoftTimeLimitExceeded, Retry

from sentry_sdk.hub import Hub
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
Expand Down Expand Up @@ -82,6 +82,15 @@ def event_processor(event, hint):
}

if "exc_info" in hint:
with capture_internal_exceptions():
if isinstance(hint["exc_info"][1], Retry):
return None

if hasattr(task, "throws") and isinstance(
hint["exc_info"][1], task.throws
):
return None

with capture_internal_exceptions():
if issubclass(hint["exc_info"][0], SoftTimeLimitExceeded):
event["fingerprint"] = [
Expand All @@ -90,12 +99,6 @@ def event_processor(event, hint):
getattr(task, "name", task),
]

with capture_internal_exceptions():
if hasattr(task, "throws") and isinstance(
hint["exc_info"][1], task.throws
):
return None

return event

return event_processor
Expand Down
36 changes: 36 additions & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,39 @@ def dummy_task(x, y):
assert stack_lengths == [2]
else:
assert stack_lengths == [2, 2]


@pytest.mark.skipif(
(4, 2, 0) <= VERSION < (4, 2, 2),
reason="https://github.com/celery/celery/issues/4661",
)
def test_retry(celery, capture_events):
events = capture_events()
failures = [True, True, False]
runs = []

@celery.task(name="dummy_task", bind=True)
def dummy_task(self):
runs.append(1)
try:
if failures.pop(0):
1 / 0
except Exception as exc:
self.retry(max_retries=2, exc=exc)

dummy_task.delay()

assert len(runs) == 3
assert not events

failures = [True, True, True]
runs = []

dummy_task.delay()

assert len(runs) == 3
event, = events
exceptions = event["exception"]["values"]

for e in exceptions:
assert e["type"] == "ZeroDivisionError"
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ envlist =

py3.7-sanic-0.8

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

{py2.7,py3.7}-requests
Expand Down Expand Up @@ -65,7 +65,8 @@ deps =
sanic: aiohttp

celery-3: Celery>=3.1,<4.0
celery-4: Celery>=4.0,<5.0
celery-4.1: Celery>=4.1,<4.2
celery-4.2: Celery>=4.2,<4.3

requests: requests>=2.0

Expand Down