Skip to content

Commit

Permalink
Add additional logic to celery task naming for mapping tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
TimPansino committed Apr 18, 2024
1 parent 034b1bf commit 2827f7b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions newrelic/hooks/application_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@


UNKNOWN_TASK_NAME = "<Unknown Task>"
MAPPING_TASK_NAMES = {"celery.starmap", "celery.map"}


def CeleryTaskWrapper(wrapped, application=None, name=None):
Expand Down Expand Up @@ -175,17 +176,22 @@ def instrument_celery_app_task(module):
# the tracer instead.

def task_name(*args, **kwargs):
if "task" in kwargs:
task = kwargs["task"]
elif args:
if args:
task = args[0]
elif "task" in kwargs:
task = kwargs["task"]
else:
return UNKNOWN_TASK_NAME # Failsafe

if isinstance(task, dict):
return task.get("task", UNKNOWN_TASK_NAME)
else:
return task.name
task_name = getattr(task, "name", None) or task.get("task", UNKNOWN_TASK_NAME)
if task_name in MAPPING_TASK_NAMES:
try:
subtask = kwargs["task"]["task"]
task_name = "/".join((task_name, subtask))
except Exception:
pass

return task_name

if module.BaseTask.__module__ == module.__name__:
module.BaseTask.__call__ = CeleryTaskWrapper(module.BaseTask.__call__, name=task_name)
Expand Down

0 comments on commit 2827f7b

Please sign in to comment.