Skip to content

Add root_id & parent_id for tasks executed with ".apply()" #9738

Closed

Description

@jayjoshi64

Checklist

  • I have checked the issues list
    for similar or identical enhancement to an existing feature.
  • I have checked the pull requests list
    for existing proposed enhancements.
  • I have checked the commit log
    to find out if the same enhancement was already implemented in the
    main branch.
  • I have included all related issues and possible duplicate issues in this issue
    (If there are none, check this box anyway).

Related Issues and Possible Duplicates

Related Issues

  • None

Possible Duplicates

  • None

Brief Summary

When a task is called with apply_async, the request has root_id & parent_id which helps in tracing the logs.
But when a task is executed with apply, the root_id & parent_id are missing from the request.
We are using the root_id to club logs of a root-task. but for applied tasks, its not working.

I understand, we are not adding a task to queue when it's called with apply, because we run locally... but adding the root-id & parent-id will help regardless.

Details

We are using the following formatter:

(
            '{"ts": "%(asctime)s", "log_level": "%(levelname)s", "process_name":'
            ' "%(processName)s", "file": "%(filename)s:%(lineno)d", "task_name":'
            ' "%(task_name)s", "task_id": "%(task_id)s", "root_id":'
            ' "%(root_id)s", "parent_id": "%(parent_id)s", "msg": "%(message)s"}'
)

This works, because we add the properties before formatting the logs

class CustomFormatter(TaskFormatter):
    def format(self, record):
        task = get_current_task()
        if task and task.request:
            record.__dict__.update(
                task_id=task.request.id,
                task_name=task.name,
                parent_id=task.request.parent_id or "",
                root_id=task.request.root_id or "",
            )
        else:
            record.__dict__.setdefault("task_name", "")
            record.__dict__.setdefault("task_id", "")
            record.__dict__.setdefault("parent_id", "")
            record.__dict__.setdefault("root_id", "")
        return super().format(record)

Activity

  1. added a commit that references this issue on Jun 27, 2025
  2. added this to the 5.6.0 milestone on Jul 2, 2025
  3. added a commit that references this issue on Jul 3, 2025
    a83070e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions