{"success":true,"name":"thread-one-request-id-through-every-layer-so-a-failure-tells","description":"Debugging across frontend, API, services, and jobs with disconnected logs","category":"ai-brimind","path":"ai-brimind/thread-one-request-id-through-every-layer-so-a-failure-tells/SKILL.md","file":"SKILL.md","content":"---\nname: thread-one-request-id-through-every-layer-so-a-failure-tells\ndescription: \"Debugging across frontend, API, services, and jobs with disconnected logs\"\n---\n\n# Thread One Request ID Through Every Layer So a Failure Tells You Its Whole Story\n\n## When to use\nA single user action triggers work in several layers, but each layer logs in isolation — nothing links the frontend call, the API handler, the service logic, and the DB error into one traceable story.\n\n## Example request\nThe log shows 40 'payment failed' lines from a busy hour → which belongs to the user who complained? Without an ID tying logs to a specific request, production logs are confetti, not evidence. A requestId generated at the API entry, attached to every log line in that request's path → grep one ID, read the whole story in order: entry, service call, DB error, response sent; correlation is what turns logs into narratives. The frontend shows the user 'Error (ref: ab12cd)' from the response → the support ticket now contains the exact thread to pull; the ID crossing BACK over the API boundary is what connects a human complaint to a machine trace. A background job processing a record touched by an earlier request → carrying the originating ID (or its own runId) into the job's logs extends the story across time; async work is where stories usually go dark.\n\n## Instructions\nAct as the detective who will investigate next month's production incident, and plant the evidence trail now — because in a layered app one user click fans out through middleware, handler, services, DB calls, and maybe a queued job, and when something fails at layer four, isolated log lines from each layer are forty puzzle pieces from different puzzles; the request ID is the thread that makes them ONE story, and it costs almost nothing if woven in from the start. Do this: (1) Mint the ID at the outermost entry: a middleware at the top of every API request generates a short unique id (crypto.randomUUID or nanoid — existing tools, per your reuse discipline) or adopts one from an incoming header (x-request-id) if a proxy already provides it — check what your platform sends before generating, the header convention exists precisely for this. (2) Carry it through the layers the way your architecture already flows: attach it to the request context, and pass it into service functions as part of their context/argument — your logic-out-of-routes discipline makes this clean, since services take plain arguments; every log line those functions emit includes the id (a small logger helper that prefixes it beats remembering to concatenate it forty times — one shared mechanism, per your one-owner instincts). (3) Let it cross boundaries in BOTH directions: include it in error responses per your error-contract discipline ('ref: ab12cd') so the frontend can show it and a user's screenshot becomes a grep command; and when work goes async — a queued email, a background job touched off by this request — write the originating id into the job record so the story survives the time gap, which is exactly where your fire-and-forget and scheduled-job logging disciplines need the connective tissue. (4) Log the story's chapters at the layer transitions, not every line: request in (method, path, id), service decision points with intent per your log-with-intent discipline, DB/external failures with the id, response out (status, duration, id) — enough that grepping one id reconstructs the sequence without drowning in noise. (5) Verify with the incident drill: cause a deep failure on purpose (break a DB call), take the ref from the error response, grep it — you should read the entire journey in order, across files, in one search. Only call observability done when one ID retrieves one story — logs that cannot answer 'what happened to THIS request' are not logs, they are a diary written by forty people who never met.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #244\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}