feat(background_tasks): propagate task_id + job_id into worker log context - #166
Merged
Conversation
…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.
Deploying simple-module-python with
|
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
bind_task_context(**ids)— a context manager that layers arbitrary domain identifiers (e.g.job_id) onto acontextvars-backed log context for the wrapping scope.task_prerun/task_postrunsignals now bindtask_id+task_nameautomatically for the task's duration; aLogContextFiltercopies the bindings onto everyLogRecord.JsonFormatteremitstask_id/task_nameout of the box; arbitrarybind_task_contextkeys flow into stdlibrecord.__dict__and into structlog viamerge_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 test— 1198 passed, 1 skipped, 2 deselected (Python) + 16 passed (JS).modules/background_tasks/tests/test_log_context.pycover: 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-boundtask_id/task_name.--skip-browser): feature is a backend Celery library with no UI / route / e2e surface.Test plan
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.