pub struct RequestIdGenerator;Expand description
Request ID generator for distributed tracing and request correlation.
This generator implements the MakeRequestId trait from tower-http to either:
- Preserve an existing
x-request-idheader from the incoming request, or - Generate a new UUIDv7 if no request ID is present
Using UUIDv7 provides several benefits:
- Time-ordered: IDs are sortable by creation time
- Unique: Collision-resistant across distributed systems
- Traceable: Can correlate requests across multiple services
§Request ID Flow
Client Request
│
├─ Has x-request-id header? ─> Preserve it
│
└─ No header? ─> Generate new UUIDv7§Examples
use axum_conf::RequestIdGenerator;
use tower_http::request_id::SetRequestIdLayer;
// Add to your Axum router
let layer = SetRequestIdLayer::x_request_id(RequestIdGenerator);§Use Cases
- Distributed Tracing: Track a request across multiple microservices
- Debugging: Correlate logs from different components of a request
- Auditing: Track the lifecycle of a request for compliance
- Monitoring: Measure end-to-end request latency
Trait Implementations§
Source§impl Clone for RequestIdGenerator
impl Clone for RequestIdGenerator
Source§fn clone(&self) -> RequestIdGenerator
fn clone(&self) -> RequestIdGenerator
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for RequestIdGenerator
impl Debug for RequestIdGenerator
Source§impl MakeRequestId for RequestIdGenerator
impl MakeRequestId for RequestIdGenerator
Source§fn make_request_id<B>(&mut self, req: &Request<B>) -> Option<RequestId>
fn make_request_id<B>(&mut self, req: &Request<B>) -> Option<RequestId>
Generates or extracts a request ID from an HTTP request.
If the request already has an x-request-id header, that value is preserved.
Otherwise, a new UUIDv7 is generated with high-precision timestamp context.
§Arguments
req- The HTTP request to process
§Returns
An Option<RequestId> containing either the existing or newly generated ID.
Returns None only if UUID generation or header value creation fails
(which is extremely rare in practice).
impl Copy for RequestIdGenerator
Auto Trait Implementations§
impl Freeze for RequestIdGenerator
impl RefUnwindSafe for RequestIdGenerator
impl Send for RequestIdGenerator
impl Sync for RequestIdGenerator
impl Unpin for RequestIdGenerator
impl UnwindSafe for RequestIdGenerator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more