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
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ ignore_missing_imports = True
[mypy-sentry_sdk._queue]
ignore_missing_imports = True
disallow_untyped_defs = False
[mypy-celery.app.trace]
ignore_missing_imports = True
11 changes: 7 additions & 4 deletions sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Ignore,
Reject,
)
from celery.app.trace import task_has_custom
except ImportError:
raise DidNotEnable("Celery not installed")

Expand Down Expand Up @@ -57,10 +58,12 @@ def setup_once():
def sentry_build_tracer(name, task, *args, **kwargs):
# type: (Any, Any, *Any, **Any) -> Any
if not getattr(task, "_sentry_is_patched", False):
# Need to patch both methods because older celery sometimes
# short-circuits to task.run if it thinks it's safe.
task.__call__ = _wrap_task_call(task, task.__call__)
task.run = _wrap_task_call(task, task.run)
# determine whether Celery will use __call__ or run and patch
# accordingly
if task_has_custom(task, "__call__"):
type(task).__call__ = _wrap_task_call(task, type(task).__call__)
else:
task.run = _wrap_task_call(task, task.run)

# `build_tracer` is apparently called for every task
# invocation. Can't wrap every celery task for every invocation
Expand Down
22 changes: 22 additions & 0 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,25 @@ def walk_dogs(x, y):
# passed as args or as kwargs, so make this generic
DictionaryContaining({"celery_job": dict(task="dog_walk", **args_kwargs)})
)


def test_abstract_task(capture_events, celery, celery_invocation):
events = capture_events()

class AbstractTask(celery.Task):
abstract = True

def __call__(self, *args, **kwargs):
try:
return self.run(*args, **kwargs)
except ZeroDivisionError:
return None

@celery.task(name="dummy_task", base=AbstractTask)
def dummy_task(x, y):
return x / y

with start_transaction():
celery_invocation(dummy_task, 1, 0)

assert not events