Skip to content

Celery integration: Add task_id to task_failed log - #1043

Open
ento wants to merge 2 commits into
jrobichaud:mainfrom
ento:add-task-id-to-task-failed-log
Open

Celery integration: Add task_id to task_failed log#1043
ento wants to merge 2 commits into
jrobichaud:mainfrom
ento:add-task-id-to-task-failed-log

Conversation

@ento

@ento ento commented Jul 31, 2026

Copy link
Copy Markdown

This is helpful when a worker crashes and the task_failure receiver gets called with a WorkerLostError exception. Because the receiver gets called on the master Celery process, context vars that were bound by previous receivers are not there, and there's barely any information that helps quickly pinpoint which task the worker was processing.

Example event without task_id:

{
  "event": "task_failed",
  "level": "error",
  "logger": "django_structlog.celery.receivers",
  "error": "Worker exited prematurely: signal 9 (SIGKILL) Job: 1012.",
  "exception": [
    {
      "exc_type": "WorkerLostError",
      "exc_value": "Worker exited prematurely: signal 9 (SIGKILL) Job: 1012.",
      "frames": [
        {
          "filename": "[...]/site-packages/billiard/pool.py",
          "line": "",
          "lineno": 1265,
          "locals": {
            "exitcode": "-9",
            "job": "<%s: 1012 ack:True ready:True>",
            "self": "<celery.concurrency.asynpool.AsynPool object at 0xffff677a24a0>"
          },
          "name": "mark_as_worker_lost"
        }
      ],
      "is_cause": false
    }
  ]
}

The line in Celery that sends the task_failure signal when a worker is lost: https://github.com/celery/celery/blob/v5.6.3/celery/worker/request.py#L645

Confirmed that the task_id in this case matches the one included in the task_started event using a local project:

worker | 2026-07-31T12:50:23.455490 [info ] task_started [django_structlog.celery.receivers] task_id=dd46cf66-3c4d-47a5-a298-506345ed61e5
...
worker | 2026-07-31T12:50:23.514780 [error ] task_failed [django_structlog.celery.receivers] error=Worker exited prematurely: exitcode 1 Job: 0. task_id=dd46cf66-3c4d-47a5-a298-506345ed61e5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant