Skip to content

feat(background_tasks): propagate task_id + job_id into worker log context - #166

Merged
antosubash merged 1 commit into
mainfrom
worktree-bg-tasks-log-context
May 21, 2026
Merged

feat(background_tasks): propagate task_id + job_id into worker log context#166
antosubash merged 1 commit into
mainfrom
worktree-bg-tasks-log-context

Conversation

@antosubash

Copy link
Copy Markdown
Owner

Summary

  • Adds bind_task_context(**ids) — a context manager that layers arbitrary domain identifiers (e.g. job_id) onto a contextvars-backed log context for the wrapping scope.
  • Celery task_prerun / task_postrun signals now bind task_id + task_name automatically for the task's duration; a LogContextFilter copies the bindings onto every LogRecord.
  • Hosting JsonFormatter emits task_id / task_name out of the box; arbitrary bind_task_context keys flow into stdlib record.__dict__ and into structlog via merge_contextvars.

Closes #165.

Verification

  • make lint — clean (ruff format-check, ruff, ty, biome, per-workspace tsc, 300-line cap, metadata, README, hardcoded-string scans).
  • make test1198 passed, 1 skipped, 2 deselected (Python) + 16 passed (JS).
  • 13 new tests under modules/background_tasks/tests/test_log_context.py cover: nested + restorable bindings, raise-on-LogRecord-attribute-collision, filter injection + explicit-extra= precedence, idempotent install, signal prerun/postrun pairing, layered domain bindings on top of signal-bound task_id/task_name.
  • Browser stages skipped (--skip-browser): feature is a backend Celery library with no UI / route / e2e surface.

Test plan

  • Reviewer confirms the public API (bind_task_context, get_log_context, install_log_filter) is what's wanted; non-public surface (LogContextFilter, signal_task_started/finished, _signal_tokens) is intentionally not re-exported from the package root.
  • CI green.

…ntext

Adds `bind_task_context()` plus a `task_prerun` / `task_postrun` pair that
binds Celery's `task_id` / `task_name` into a contextvars-backed log
context. A `LogContextFilter` copies the bindings onto every LogRecord;
hosting's `JsonFormatter` already emits the keys via `_EXTRA_KEYS`. App
code wraps task bodies in `bind_task_context(job_id=...)` to attach
domain identifiers. Closes #165.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying simple-module-python with  Cloudflare Pages  Cloudflare Pages

Latest commit: e82721f
Status: ✅  Deploy successful!
Preview URL: https://13479aa7.simple-module-python.pages.dev
Branch Preview URL: https://worktree-bg-tasks-log-contex.simple-module-python.pages.dev

View logs

@antosubash
antosubash merged commit b03cd54 into main May 21, 2026
12 checks passed
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.

simple_module_background_tasks: propagate task_id + job_id into log context for Celery workers

1 participant