Documentation
¶
Overview ¶
Request IDs: one identifier per request, in the response header and the logs.
requestid.New() installs the middleware. The default generator reuses a client-supplied X-Request-Id header and only mints a UUIDv4 when it is absent - convenient behind a trusted proxy, unsafe when arbitrary clients can choose the value that lands in your logs. requestid.ServerOnlyGenerator is the hardened variant: it ignores the inbound header and always generates the ID server-side.
Read the ID with requestid.Get(ctx) (a shortcut for ctx.ID().(string)).
Learn: install requestid, read the ID with requestid.Get, and pick ServerOnlyGenerator when clients must not choose the value. Before: ../rate-limit
Run:
go run main.go
Try:
curl -i http://localhost:8080/trusted # 200, a generated X-Request-Id curl -i -H "X-Request-Id: chosen-by-client" http://localhost:8080/trusted # 200, echoes chosen-by-client curl -i -H "X-Request-Id: chosen-by-client" http://localhost:8080/hardened # 200, ignores it and generates its own
Next: ../accesslog