Skip to content

Commit

Permalink
Add request id example
Browse files Browse the repository at this point in the history
  • Loading branch information
conways-glider committed Sep 14, 2024
1 parent 680cdcb commit ca632b9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
13 changes: 13 additions & 0 deletions examples/request-id/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "example-request-id"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
axum = { path = "../../axum" }
tokio = { version = "1.0", features = ["full"] }
tower = "0.5"
tower-http = { version = "0.5", features = ["request-id", "trace"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
81 changes: 81 additions & 0 deletions examples/request-id/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Run with
//!
//! ```not_rust
//! cargo run -p example-request-id
//! ```

use axum::{
http::{HeaderName, Request},
response::Html,
routing::get,
Router,
};
use tower::ServiceBuilder;
use tower_http::{
request_id::{MakeRequestUuid, PropagateRequestIdLayer, SetRequestIdLayer},
trace::TraceLayer,
};
use tracing::info_span;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

const REQUEST_ID_HEADER: &str = "x-request-id";

#[tokio::main]
async fn main() {
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
// axum logs rejections from built-in extractors with the `axum::rejection`
// target, at `TRACE` level. `axum::rejection=trace` enables showing those events
format!(
"{}=debug,tower_http=debug,axum::rejection=trace",
env!("CARGO_CRATE_NAME")
)
.into()
}),
)
.with(tracing_subscriber::fmt::layer())
.init();

let x_request_id = HeaderName::from_static(REQUEST_ID_HEADER);

let middleware = ServiceBuilder::new()
.layer(SetRequestIdLayer::new(
x_request_id.clone(),
MakeRequestUuid,
))
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
// log the request id
let request_id = request.headers().get(REQUEST_ID_HEADER);

match request_id {
Some(request_id) => info_span!(
"http_request",
method = ?request.method(),
request_id = ?request_id,
),
None => info_span!(
"http_request",
method = ?request.method(),
),
}
}),
)
// send headers from request to response headers
.layer(PropagateRequestIdLayer::new(x_request_id));

// build our application with a route
let app = Router::new().route("/", get(handler)).layer(middleware);

// run it
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}

async fn handler() -> Html<&'static str> {
Html("<h1>Hello, World!</h1>")
}

0 comments on commit ca632b9

Please sign in to comment.