/py-pit:request-correlationfrom py-pit
Enforces end-to-end request correlation across HTTP handlers, services, outbound calls, background jobs, and async tasks using contextvars. Ensures correlation IDs propagate across boundaries and exceptions are logged exactly once.
Ships in py-pit. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Every request or job must produce a traceable log story.
A single correlation ID must propagate through:
The correlation ID must appear automatically in logs.
Infrastructure vs. convention: Some rules are wired once at app startup (middleware, logging config, HTTP client factory). Others are conventions followed everywhere (raise don't log in services, never pass correlation as a function argument). Keep them separate in your mental model -- the infrastructure makes the conventions effortless.
Correlation infrastructure must live in predictable modules.
Use:
{pkg_name}/observability/correlation.py
{pkg_name}/observability/logging.py
Do not duplicate correlation logic elsewhere.
1. Wire correlation at entrypoints
Entrypoints include HTTP requests, background job workers, CLI tasks, and async task roots.
For HTTP requests, use a functional middleware to read x-request-id if
present, otherwise generate one, then set it into context:
import uuid
from fastapi import Request
from {pkg_name}.observability.correlation import correlation_id
@app.middleware("http")
async def correlation_middleware(request: Request, call_next):
cid = request.headers.get("x-request-id") or str(uuid.uuid4())
correlation_id.set(cid)
response = await call_next(request)
response.headers["x-request-id"] = cid
return response
Prefer @app.middleware("http") over BaseHTTPMiddleware — the class-based
approach can swallow exceptions and interfere with streaming responses.
For job workers and CLI entrypoints, set correlation_id before executing
the task -- either from a passed value or a freshly generated one.
2. Store correlation in contextvars
Correlation must live in a contextvars.ContextVar.
Never store it on request objects or pass it through every function argument.
Example:
from contextvars import ContextVar
correlation_id: ContextVar[str | None] = ContextVar("correlation_id", default=None)
3. Inject correlation into all log records
Logging configuration must automatically attach the correlation ID.
Example:
class CorrelationFilter(logging.Filter):
def filter(self, record):
record.correlation_id = correlation_id.get()
return True
Log format must include it (use structured/JSON output if the project already does):
%(levelname)s [cid=%(correlation_id)s] %(message)s
4. Log exceptions only at boundaries
Exceptions are logged exactly once at system boundaries:
Service functions should raise errors but not log them. Log calls inside service internals to "track" correlation add noise without value -- the boundary log with the shared correlation ID tells the whole story.
5. Propagate correlation to outbound HTTP calls
All outbound HTTP clients must include the correlation ID.
Example:
headers={"x-request-id": correlation_id.get()}
Centralize HTTP client creation so headers are applied automatically. See the http-client-integration skill for the full outbound client pattern.
6. Propagate correlation to background jobs
If a request schedules a queue-based job, pass the correlation ID explicitly.
Example:
queue.enqueue(task, correlation_id=correlation_id.get())
The worker must restore it before executing the task.
7. Be explicit about context propagation for spawned async tasks
Do not assume task boundaries preserve correlation in every execution
model. Use copy_context() as the safe pattern:
import contextvars
import asyncio
ctx = contextvars.copy_context()
loop.run_in_executor(None, ctx.run, task)
# or for coroutines:
asyncio.get_running_loop().call_soon(ctx.run, task)
This applies the same principle as Rule 6 -- correlation must be deliberately carried across any execution boundary, not assumed to be inherited.
8. Never log secrets
Never log:
A request should produce logs that share one correlation ID:
INFO request.start cid=abc123 path=/orders
INFO http.outbound cid=abc123 service=payments
ERROR request.failed cid=abc123 error=PaymentError
One request → one correlation ID → one coherent trace.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
correlation-id, request-tracing, observability, contextvars, logging
Generated from the plugin's indexed repository (robhowley/py-pit-skills) — no native marketplace selected
npx claudepluginhub p/robhowley-py-pit16 skills in this plugin
/alembic-migrationsProduction-safe Alembic migration workflows for SQLAlchemy 2.x projects. Use this skill when adding Alembic, generating migrations, reviewing autogenerate diffs, fixing broken migrations, or establishing a safe schema evolution workflow in a Python backend./background-jobs-boundariesGuardrails for background work in FastAPI and Python services. Prevent misuse of in-process background execution (e.g. FastAPI BackgroundTasks)./click-cliDesign, generate, and improve Python CLIs using the Click library. Focus on idiomatic architecture, command groups, modular layouts, CLI UX conventions, and avoiding common Click anti-patterns./click-cli-linterAudit and improve existing Python Click CLIs by identifying architecture problems, anti-patterns, and CLI UX issues, then proposing minimal, high-value fixes./code-qualityOpinionated code health stack for Python repos using Ruff, pre-commit, and Markdown linting. Prefers consolidation over redundant tooling, preserves working repos unless migration is requested, and provides a simple adoption path for both new and existing projects./dockerize-serviceGenerate a local-development Docker Compose setup for an existing Python project. Produces a multi-stage Dockerfile, Compose file, env config, and dockerignore based on detected project signals.Other plugins' skills for the same job