Skip to main content

Module request_id

Module request_id 

Source
Expand description

Request ID / correlation ID middleware.

Distributed tracing (crate::otel) creates a span per request but doesn’t give handlers a simple, stable identifier to put in their own log lines — and doesn’t propagate one across service boundaries unless the caller already sends a W3C traceparent. RequestIdLayer fills that gap: it’s a plain string ID, present on both the request (so your handler can read and log it) and the response (so the caller can log the same value), that survives even in builds/setups without OpenTelemetry wired up.

§Example

use rust_web_server::app::App;
use rust_web_server::core::New;
use rust_web_server::request_id::RequestIdLayer;

let app = App::new().wrap(RequestIdLayer::new());

Reading it in a handler — the header is just a normal request header, visible through any of the usual ways to read one:

use rust_web_server::request::Request;
use rust_web_server::request_id::DEFAULT_HEADER;

fn handler(request: &Request) {
    let id = request.get_header(DEFAULT_HEADER.to_string()).map(|h| h.value.as_str()).unwrap_or("");
    println!("[{}] handling request", id);
}

Structs§

RequestIdLayer
Middleware that ensures every request/response pair carries a stable correlation ID.

Constants§

DEFAULT_HEADER
Default header name used for the request ID, both incoming and outgoing.

Functions§

generate_request_id
Generates a UUID-v4-shaped identifier (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).