-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
680cdcb
commit ca632b9
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>") | ||
} |