Skip to content

Commit

Permalink
redact api key (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xForerunner authored Jan 23, 2024
1 parent 18da023 commit 8c5b2a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/server/trace_layer.rs
Original file line number Diff line number Diff line change
@@ -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<B> MakeSpan<B> for MatchedPathMakeSpan {
fn make_span(&mut self, request: &Request<B>) -> Span {
let matched_path = request
.extensions()
.get::<MatchedPath>()
.map(MatchedPath::as_str);

tracing::span!(
Level::DEBUG,
"request",
method = %request.method(),
matched_path,
version = ?request.version(),
)
}
}

0 comments on commit 8c5b2a4

Please sign in to comment.