Add request id example (#2912)

This commit is contained in:
Nia 2024-09-28 03:25:05 -06:00 committed by GitHub
parent 5d8541de6e
commit 2542acb243
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 94 additions and 0 deletions

View file

@ -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"] }

View file

@ -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::{error, info, 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 as generated.
let request_id = request.headers().get(REQUEST_ID_HEADER);
match request_id {
Some(request_id) => info_span!(
"http_request",
request_id = ?request_id,
),
None => {
error!("could not extract request_id");
info_span!("http_request")
}
}
}),
)
// 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> {
info!("Hello world!");
Html("<h1>Hello, World!</h1>")
}