diff --git a/src/server.rs b/src/server.rs index 11803d1..c6b5502 100644 --- a/src/server.rs +++ b/src/server.rs @@ -13,10 +13,12 @@ use self::routes::relayer::{ purge_unsent_txs, relayer_rpc, update_relayer, }; use self::routes::transaction::{get_tx, get_txs, send_tx}; +use self::trace_layer::MatchedPathMakeSpan; use crate::app::App; mod middleware; pub mod routes; +mod trace_layer; #[derive(Debug, Error)] pub enum ApiError { @@ -99,7 +101,10 @@ pub async fn spawn_server( let router = Router::new() .nest("/1", v1_routes) .route("/health", get(routes::health)) - .layer(tower_http::trace::TraceLayer::new_for_http()) + .layer( + tower_http::trace::TraceLayer::new_for_http() + .make_span_with(MatchedPathMakeSpan), + ) .layer(axum::middleware::from_fn(middleware::log_response)); let server = axum::Server::bind(&app.config.server.host) diff --git a/src/server/trace_layer.rs b/src/server/trace_layer.rs new file mode 100644 index 0000000..85987b9 --- /dev/null +++ b/src/server/trace_layer.rs @@ -0,0 +1,25 @@ +use axum::extract::MatchedPath; +use hyper::Request; +use tower_http::trace::MakeSpan; +use tracing::{Level, Span}; + +/// MakeSpan to remove api keys from logs +#[derive(Clone)] +pub(crate) struct MatchedPathMakeSpan; + +impl MakeSpan for MatchedPathMakeSpan { + fn make_span(&mut self, request: &Request) -> Span { + let matched_path = request + .extensions() + .get::() + .map(MatchedPath::as_str); + + tracing::span!( + Level::DEBUG, + "request", + method = %request.method(), + matched_path, + version = ?request.version(), + ) + } +}