Add request-ID correlation
Nobody has started this yet — be first.
Business impact
Every minute spent by support or on-call figuring out which server-side log line matches this customer's complaint is a minute the customer waits for an answer. Without request-ID correlation, that matching is done by eyeballing timestamps and hoping nothing else happened in the same window -- it gets worse as traffic grows, right when you can least afford slow incident response.
Problem
logRequests logs method, path, status, and duration per request via log/slog, but there is no way to correlate a single client-visible failure with the exact server log line for that request.
Current behavior
If two requests to the same path fail around the same time, the logs are indistinguishable, and the client has no token to hand back to support/on-call.
Expected behavior
Every response carries an X-Request-ID header: echoed back unchanged if the incoming request already had one, generated otherwise. The structured log line for that request includes a matching request_id field, and the ID is available to any handler further down the chain via context.Context.
Steps to reproduce
- Run the server and send a request to any route, e.g.
curl -i localhost:8080/healthz. - Inspect the response headers.
- Observe: no X-Request-ID header, and the corresponding slog line has no way to be tied back to this specific request.
Why this matters
This is the single most common "day one" observability gap in a service: without it, connecting a user's bug report to server-side logs means grepping by timestamp and hoping nothing else happened in that window.
Suggested approach
Add the ID-generation and header-echoing as its own middleware, the same func(http.Handler) http.Handler shape as logRequests, so it composes the same way in Router(). It needs to run before logRequests reads/sets anything it depends on. Use an unexported context-key type (not a bare string) to avoid collisions with other packages' context values.
Acceptance criteria
- Every response from Server.Router() (any route, including /healthz) includes a non-empty X-Request-ID header
- Sending a request with X-Request-ID: my-existing-id gets back the exact same value
- Two different requests without the header get two different generated values
- The slog line for a request includes a request_id field matching the response header's value
Verification
go test ./test/ -run TestTask03 -v
Hints (0/2)
Try it without hints first — the reading is the exercise.
Working on this ticket
Work on a branch named for the ticket — that's what you'll submit.
Branch off your fork
$git checkout -b feat/request-id-correlationFix it and commit
Meet every acceptance criterion, and add a test that would have caught this.
Push the branch
$git push -u origin feat/request-id-correlationSubmit it below
Paste your fork URL and the branch name, with a short write-up of the root cause.
Questions
Stuck on something?
Ask about anything unclear in the ticket — the maintainer and anyone who has solved it can answer. Please don't post full solutions.
Sign in to ask a question or reply.
Sign in