Expand description
One identifier per request, from the first log line to the response header.
When a user reports “it failed around 3pm”, the only thing that connects
their screenshot to a line in the log is an identifier that appeared in
both. This middleware assigns one — or keeps the one a load balancer already
attached — puts it on the request for handlers to read, on the response for
the client to quote, and on the http.request instrumentation event for
Telescope, the debug bar and OTLP traces.
App::new()?.middleware(RequestId::default())
// In a handler:
let id = req.request_id().unwrap_or("-");
// Anywhere inside the request, without a `Request` in hand:
if let Some(id) = request_id::current() { … }Incoming identifiers are trusted by default, because the useful case — an edge proxy that stamps every request and logs it — is far more common than the harmful one, and a forged id can do nothing except confuse the forger’s own log search. Anything that does not look like an identifier (too long, not printable ASCII) is replaced rather than passed on.
Structs§
Constants§
- HEADER
- The header the identifier travels in, unless configured otherwise.