Request IDs across the proxy and the app: following one request through Docker logs
When a user reports an error, the proxy log has one line for it and the app log has several, often in different containers. A request ID that appears in all of them turns a search through thousands of lines into one grep.
Generate it at the edge
The reverse proxy should set an ID if the request does not already have one, and pass it on as a header.
Caddy:
header_up X-Request-ID {http.request.uuid}
Nginx (1.11 and later):
proxy_set_header X-Request-ID $request_id;
Also log it in the proxy's access log format.
Read it in the app
Add a small middleware that reads X-Request-ID, generates one if missing, stores it in the request context and adds it to every log line. Most logging libraries support a per-request child logger for exactly this.
Return it to the client in a response header too. Support can then ask the user for it.
Pass it to other services
When the app calls another internal service, forward the same header. Workers that process jobs should store the ID with the job and log it when they run it.
Searching
docker logs api 2>&1 | grep 7f3c2a9e
docker logs worker 2>&1 | grep 7f3c2a9e
With a central log store, it is a single query across containers.
Do not log secrets with it
A request ID is safe to show. Keep tokens and cookies out of the log line next to it.
There is more on handling Docker logs in production on the Peon blog. I work on Peon, an open-source self-hosted deployment platform.