# axum-trace-id [Axum] middleware for adding trace ids to requests. # Basic Usage Adding the `SetTraceIdLayer` layer will make `TraceId` available via the request and response extensions. The crate also provides an extractor to access the trace id in a handler. For special use-cases (e.g. lazily generating trace ids only in case of http errors) you can implement `MakeTraceId` on your type. ```rust use axum::{routing::get, Router}; use axum_trace_id::{SetTraceIdLayer, TraceId}; let app: Router = Router::new() .route( "/", get(|trace_id: TraceId| async move { trace_id.to_string() }), ) .layer(SetTraceIdLayer::::new()); ``` # Use with [tracing] To use with [tracing], you can access the requests tracing id via the extensions. ```rust use axum::{http::Request, routing::get, Router}; use axum_trace_id::{SetTraceIdLayer, TraceId}; use tower_http::trace::TraceLayer; use tracing::info_span; let app = Router::new() .route("/", get(|| async { "" })) .layer(TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { let trace_id = request.extensions().get::>().unwrap(); info_span!("http_request", trace_id = trace_id) })); ``` # License This project is licensed under the [MIT license](LICENSE). [axum]: https://crates.io/crates/axum [tracing]: https://crates.io/crates/tracing