Skip to main content

Module request_id

Module request_id 

Source
Expand description

Request-ID middleware.

Assigns a UUID to x-request-id on the way in (unless the caller already supplied one) and copies it back onto the response on the way out. Compose both layers with tower::ServiceBuilder: set outermost (so it runs first on the way in) and propagate innermost (so it runs last on the way out, after the response has been fully built, and so survives handler and error-mapping layers placed between them):

use tower::ServiceBuilder;
use soma_http_server::middleware::request_id;

let _middleware = ServiceBuilder::new()
    .layer(request_id::set_request_id_layer())
    .layer(request_id::propagate_request_id_layer());

Note: crate::middleware::tracing::trace_layer uses tower_http’s default HTTP span, which does not automatically include the request ID. A product wanting the ID inside its trace spans needs a custom tower_http::trace::TraceLayer::new_for_http().make_span_with(..) that reads the x-request-id header itself — layering trace_layer() between set/propagate alone does not achieve that.

Structs§

MakeRequestUuid
A [MakeRequestId] that generates UUIDs.
PropagateRequestIdLayer
Propagate request ids from requests to responses.
SetRequestIdLayer
Set request id headers and extensions on requests.

Constants§

REQUEST_ID_HEADER
Default request-ID header name shared across Soma HTTP surfaces.

Functions§

propagate_request_id_layer
Layer that copies the request-scoped ID from the request onto the response headers.
set_request_id_layer
Layer that assigns a random UUID request ID when the incoming request does not already carry one.