Skip to content

Lesson 4 of 14 · 20m

You Can't Attach a Debugger to Production

Production debugging is reading, not stepping — structured logs with request IDs are the reproduction you get.

Production breaks at 3am and there is no breakpoint, no REPL, no git bisect for a live request. The only thing between you and the bug is whatever the system emitted before it fell over. Debugging production is reading — so the quality of that reading decides whether the incident lasts ten minutes or three days.

The wrong mental model is "logs are where errors go." A log line is evidence for a request you can't replay, and most of what a busy system emits is useless for that job because it can't be tied back to the request that produced it.

The one field that makes logs debuggable

A single request through a real stack touches the proxy, the app, two services, and a database — and the server is handling a thousand others concurrently. Without a shared identifier, every request's lines are interleaved into one stream and none of them can be connected. The fix is a correlation ID: generate one at the edge (X-Request-ID, or reuse the client's), thread it through every downstream call, and put it on every log line. Then grep requestId=abc123 reconstructs the whole story — which service saw it, what it asked for, where it died. Cloudflare's CF-RAY header is this idea at CDN scale; OpenTelemetry's traceparent header standardizes it across services.

Structured logs, not sentences

Loggers like pino and winston emit JSON lines:

{"level":"error","msg":"charge failed","requestId":"abc123","userId":"u_9f2","ms":812}

JSON exists so the aggregator can query it — journalctl -u app -f, kubectl logs, Grafana Loki, or whatever ships them off the box. Fields are the point: a message string you have to regex is a message you can't filter at 3am. Log the event and the context — request ID, actor, latency, what upstream call failed.

Levels mean who should care

debug is for developers reproducing locally. info is for expected events (request completed, job finished). warn is degradation that doesn't need a human yet. error means a human should look — and an error nobody acts on is a flake in human form: it trains on-call to ignore the channel, exactly like a flaky test trains a team to ignore red.

Logs, metrics, traces — different instruments

They aren't interchangeable, and treating them as one soup is what makes dashboards useless. Metrics are aggregates — the RED trio (rate, errors, duration — Tom Wilkie's naming) that alerts read; they tell you that something is wrong at a glance. Logs are events — they tell you what a specific request did. Traces — OpenTelemetry spans stitched together by the traceparent header — tell you where the time went across hops: which service burned the 800ms. The flow on a bad night is: an alert fires on metrics, you find the failing requests in logs, you find the slow or broken hop in a trace.

One warning that bites everyone once: unbounded values — user IDs, request IDs, emails — belong in logs, never in metric labels. A time-series store indexes every distinct label combination, so http_requests{user="u_9f2"} mints a new series per user; cardinality explosions take down the monitoring stack on the night you need it.

Never log secrets

logger.info("login", { email, password }) writes credentials to a file that outlives the request, gets shipped to third-party aggregators, and lands in backups. GitHub disclosed in 2018 that plaintext passwords had ended up in internal logs; Twitter disclosed the same class of bug the same year. Redaction is a logging feature — build the filter into the logger, don't make it a code-review habit.

Try it yourself — correlation in JS

const log = (fields) => console.log(JSON.stringify({ ts: Date.now(), ...fields }));
const requestId = "req_7a2";
log({ level: "info", requestId, msg: "request start" });
log({ level: "error", requestId, msg: "db timeout", ms: 5000 });
// Every line for one request shares an ID — grep can rebuild the story.

The practice

Before you ship a path, ask: if this fails in production at 3am, does the log contain enough to act on — which request, which user, what it was doing, what failed? If the answer is "something went wrong," the observability work isn't done, even if the code is.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.