Skip to content

Commit

Permalink
Configured TraceLayer in web server to log info about request/response
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Apr 11, 2024
1 parent 1bf4dbb commit c700129
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion rust/agama-server/src/web/service.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use super::{auth::TokenClaims, config::ServiceConfig, state::ServiceState, EventsSender};
use axum::{
body::Body,
extract::Request,
middleware,
response::IntoResponse,
response::Response,
routing::{get, post},
Router,
};
use std::convert::Infallible;
use std::time::Duration;
use tower::Service;
use tower_http::{compression::CompressionLayer, trace::TraceLayer};
use tracing::Span;

pub struct MainServiceBuilder {
config: ServiceConfig,
Expand Down Expand Up @@ -55,7 +59,14 @@ impl MainServiceBuilder {
))
.route("/ping", get(super::http::ping))
.route("/authenticate", post(super::http::authenticate))
.layer(TraceLayer::new_for_http())
.layer(TraceLayer::new_for_http()
.on_request(|request: &Request<Body>, _span: &Span| {
tracing::info!("request: {} {}", request.method(), request.uri().path())
})
.on_response(|response: &Response<Body>, latency: Duration, _span: &Span| {
tracing::info!("response: {} {:?}", response.status(), latency)
})
)
.layer(CompressionLayer::new().br(true))
.with_state(state)
}
Expand Down

0 comments on commit c700129

Please sign in to comment.