From 4badba9eb4620f71496756dd0585d7a78580c713 Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 11 Apr 2024 07:48:02 +0200 Subject: [PATCH 1/3] Configured TraceLayer in web server to log info about request/response --- rust/agama-server/src/web/service.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/rust/agama-server/src/web/service.rs b/rust/agama-server/src/web/service.rs index 04300da332..a5eeb93179 100644 --- a/rust/agama-server/src/web/service.rs +++ b/rust/agama-server/src/web/service.rs @@ -1,9 +1,11 @@ use super::http::{login, login_from_query, logout, session}; use super::{auth::TokenClaims, config::ServiceConfig, state::ServiceState, EventsSender}; use axum::{ + body::Body, extract::Request, middleware, response::IntoResponse, + response::Response, routing::{get, post}, Router, }; @@ -11,8 +13,10 @@ use std::{ convert::Infallible, path::{Path, PathBuf}, }; +use std::time::Duration; use tower::Service; use tower_http::{compression::CompressionLayer, services::ServeDir, trace::TraceLayer}; +use tracing::Span; /// Builder for Agama main service. /// @@ -93,7 +97,14 @@ impl MainServiceBuilder { .route("/login", get(login_from_query)) .route("/po.js", get(super::http::po)) .nest("/api", api_router) - .layer(TraceLayer::new_for_http()) + .layer(TraceLayer::new_for_http() + .on_request(|request: &Request, _span: &Span| { + tracing::info!("request: {} {}", request.method(), request.uri().path()) + }) + .on_response(|response: &Response, latency: Duration, _span: &Span| { + tracing::info!("response: {} {:?}", response.status(), latency) + }) + ) .layer(CompressionLayer::new().br(true)) .with_state(state) } From b819396c01d2f5676186b771b941c774a74ec64f Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 11 Apr 2024 08:03:56 +0200 Subject: [PATCH 2/3] Formatting --- rust/agama-server/src/web/service.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/rust/agama-server/src/web/service.rs b/rust/agama-server/src/web/service.rs index a5eeb93179..fcf9dff091 100644 --- a/rust/agama-server/src/web/service.rs +++ b/rust/agama-server/src/web/service.rs @@ -97,13 +97,16 @@ impl MainServiceBuilder { .route("/login", get(login_from_query)) .route("/po.js", get(super::http::po)) .nest("/api", api_router) - .layer(TraceLayer::new_for_http() - .on_request(|request: &Request, _span: &Span| { - tracing::info!("request: {} {}", request.method(), request.uri().path()) - }) - .on_response(|response: &Response, latency: Duration, _span: &Span| { - tracing::info!("response: {} {:?}", response.status(), latency) - }) + .layer( + TraceLayer::new_for_http() + .on_request(|request: &Request, _span: &Span| { + tracing::info!("request: {} {}", request.method(), request.uri().path()) + }) + .on_response( + |response: &Response, latency: Duration, _span: &Span| { + tracing::info!("response: {} {:?}", response.status(), latency) + }, + ), ) .layer(CompressionLayer::new().br(true)) .with_state(state) From 7f9f06c7b964a58953e5182d559ad0a4d0a5136e Mon Sep 17 00:00:00 2001 From: Michal Filka Date: Thu, 11 Apr 2024 11:14:49 +0200 Subject: [PATCH 3/3] Minor tweaks --- rust/agama-server/src/web/service.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/agama-server/src/web/service.rs b/rust/agama-server/src/web/service.rs index fcf9dff091..31af807388 100644 --- a/rust/agama-server/src/web/service.rs +++ b/rust/agama-server/src/web/service.rs @@ -4,16 +4,15 @@ use axum::{ body::Body, extract::Request, middleware, - response::IntoResponse, - response::Response, + response::{IntoResponse, Response}, routing::{get, post}, Router, }; +use std::time::Duration; use std::{ convert::Infallible, path::{Path, PathBuf}, }; -use std::time::Duration; use tower::Service; use tower_http::{compression::CompressionLayer, services::ServeDir, trace::TraceLayer}; use tracing::Span; @@ -101,7 +100,7 @@ impl MainServiceBuilder { TraceLayer::new_for_http() .on_request(|request: &Request, _span: &Span| { tracing::info!("request: {} {}", request.method(), request.uri().path()) - }) + }) .on_response( |response: &Response, latency: Duration, _span: &Span| { tracing::info!("response: {} {:?}", response.status(), latency)