Skip to content

Commit 3023caa

Browse files
author
Emanuele Palazzetti
authored
[celery] patch TaskRegistry to support old-style task with ddtrace-run (#484)
1 parent 8620442 commit 3023caa

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

ddtrace/contrib/celery/patch.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# Third party
21
import celery
32

4-
# Project
3+
from wrapt import wrap_function_wrapper as _w
4+
55
from .app import patch_app, unpatch_app
6+
from .registry import _wrap_register
7+
from ...utils.wrappers import unwrap as _u
68

79

810
def patch():
9-
""" patch will add all available tracing to the celery library """
11+
"""Instrument Celery base application and the `TaskRegistry` so
12+
that any new registered task is automatically instrumented
13+
"""
1014
setattr(celery, 'Celery', patch_app(celery.Celery))
15+
_w('celery.app.registry', 'TaskRegistry.register', _wrap_register)
1116

1217

1318
def unpatch():
14-
""" unpatch will remove tracing from the celery library """
19+
"""Removes instrumentation from Celery"""
1520
setattr(celery, 'Celery', unpatch_app(celery.Celery))
21+
_u(celery.app.registry.TaskRegistry, 'register')

ddtrace/contrib/celery/registry.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .task import patch_task
2+
3+
4+
def _wrap_register(func, instance, args, kwargs):
5+
"""Wraps the `TaskRegistry.register` function so that everytime
6+
a `Task` is registered it is properly instrumented. This wrapper
7+
is required because in old-style tasks (Celery 1.0+) we cannot
8+
instrument the base class, otherwise a `Strategy` `KeyError`
9+
exception is raised.
10+
"""
11+
# the original signature requires one positional argument so the
12+
# first and only parameter is the `Task` that must be instrumented
13+
task = args[0]
14+
patch_task(task)
15+
func(*args, **kwargs)

tox.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ deps =
128128
celery31: celery>=3.1,<3.2
129129
celery40: celery>=4.0,<4.1
130130
celery41: celery>=4.1,<4.2
131-
# TODO[manu] update to a stable version of Celery
132-
celery42: celery==4.2.0rc3
131+
celery42: celery>=4.2,<4.3
133132
ddtracerun: redis
134133
ddtracerun: celery
135134
elasticsearch16: elasticsearch>=1.6,<1.7

0 commit comments

Comments
 (0)