Skip to content

Commit 6fbbba4

Browse files
authored
Merge pull request #26 from taskbadger/include-exclude-auto-track-tasks
allow specifying task names to include / exclude from tracking
2 parents 05d6ffb + 2707d7d commit 6fbbba4

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

taskbadger/celery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def task_publish_handler(sender=None, headers=None, body=None, **kwargs):
144144
return
145145

146146
celery_system = Badger.current.settings.get_system_by_id("celery")
147-
auto_track = celery_system and celery_system.auto_track_tasks
147+
auto_track = celery_system and celery_system.track_task(sender)
148148
manual_track = headers.get("taskbadger_track")
149149
if not manual_track and not auto_track:
150150
return

taskbadger/systems/celery.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
import re
2+
13
from taskbadger.systems import System
24

35

46
class CelerySystemIntegration(System):
57
identifier = "celery"
68

7-
def __init__(self, auto_track_tasks=True):
9+
def __init__(self, auto_track_tasks=True, includes=None, excludes=None):
810
"""
911
Args:
1012
auto_track_tasks: Automatically track all Celery tasks regardless of whether they are using the
1113
`taskbadger.celery.Task` base class.
14+
includes: A list of task names to include in tracking. These can be either the full task name
15+
(e.g. `myapp.tasks.export_data`) or a regular expression (e.g. `export_.*`). If a task name
16+
matches both an include and an exclude, it will be excluded.
17+
excludes: A list of task names to exclude from tracking. As with `includes`, these can be either
18+
the full task name or a regular expression. Exclusions take precedence over inclusions.
1219
"""
1320
self.auto_track_tasks = auto_track_tasks
14-
if auto_track_tasks:
15-
# Importing this here ensures that the Celery signal handlers are registered
16-
import taskbadger.celery # noqa
21+
self.includes = includes
22+
self.excludes = excludes
23+
24+
def track_task(self, task_name):
25+
if not self.auto_track_tasks:
26+
return False
27+
28+
if self.excludes:
29+
for exclude in self.excludes:
30+
if re.fullmatch(exclude, task_name):
31+
return False
32+
33+
if self.includes:
34+
for include in self.includes:
35+
if re.fullmatch(include, task_name):
36+
break
37+
else:
38+
return False
39+
40+
return True

tests/test_celery_system_integration.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,20 @@ def add_normal(self, a, b):
6060
assert get_task.call_count == 1
6161
assert update.call_count == 2
6262
assert Badger.current.session().client is None
63+
64+
65+
@pytest.mark.parametrize(
66+
"include,exclude,expected",
67+
[
68+
(None, None, True),
69+
(["myapp.tasks.export_data"], None, True),
70+
([".*export_data"], [], True),
71+
([".*export_da"], [], False),
72+
(["myapp.tasks.export_data"], ["myapp.tasks.export_data"], False),
73+
([".*"], ["myapp.tasks.export_data"], False),
74+
([".*"], [".*tasks.*"], False),
75+
],
76+
)
77+
def test_task_name_matching(include, exclude, expected: bool):
78+
integration = CelerySystemIntegration(includes=include, excludes=exclude)
79+
assert integration.track_task("myapp.tasks.export_data") is expected

0 commit comments

Comments
 (0)