File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 33from wrapt import wrap_function_wrapper as _w
44
55from .app import patch_app , unpatch_app
6+ from .task import _wrap_shared_task
67from .registry import _wrap_register
78from ...utils .wrappers import unwrap as _u
89
910
1011def patch ():
1112 """Instrument Celery base application and the `TaskRegistry` so
12- that any new registered task is automatically instrumented
13+ that any new registered task is automatically instrumented. In the
14+ case of Django-Celery integration, also the `@shared_task` decorator
15+ must be instrumented because Django doesn't use the Celery registry.
1316 """
1417 setattr (celery , 'Celery' , patch_app (celery .Celery ))
1518 _w ('celery.app.registry' , 'TaskRegistry.register' , _wrap_register )
19+ _w ('celery' , 'shared_task' , _wrap_shared_task )
1620
1721
1822def unpatch ():
1923 """Removes instrumentation from Celery"""
2024 setattr (celery , 'Celery' , unpatch_app (celery .Celery ))
2125 _u (celery .app .registry .TaskRegistry , 'register' )
26+ _u (celery , 'shared_task' )
Original file line number Diff line number Diff line change @@ -79,6 +79,14 @@ def unpatch_task(task):
7979 return task
8080
8181
82+ def _wrap_shared_task (decorator , instance , args , kwargs ):
83+ """Wrapper for Django-Celery shared tasks. `shared_task` is a decorator
84+ that returns a `Task` from the given function.
85+ """
86+ task = decorator (* args , ** kwargs )
87+ return patch_task (task )
88+
89+
8290def _task_init (func , task , args , kwargs ):
8391 func (* args , ** kwargs )
8492
Original file line number Diff line number Diff line change @@ -488,3 +488,19 @@ class CelerySubClass(CelerySuperClass):
488488 t .run ()
489489 spans = self .tracer .writer .pop ()
490490 self .assertEqual (len (spans ), 4 )
491+
492+ def test_celery_shared_task (self ):
493+ @celery .shared_task
494+ def add (x ,y ):
495+ return x + y
496+
497+ res = add .run (2 , 2 )
498+ self .assertEqual (res , 4 )
499+ spans = self .tracer .writer .pop ()
500+ self .assertEqual (len (spans ), 1 )
501+ span = spans [0 ]
502+ self .assertEqual (span .service , 'celery-worker' )
503+ self .assertEqual (span .resource , 'tests.contrib.celery.test_task.add' )
504+ self .assertEqual (span .name , 'celery.run' )
505+ self .assertIsNone (span .parent_id )
506+ self .assertEqual (span .error , 0 )
You can’t perform that action at this time.
0 commit comments