Skip to content

Commit

Permalink
better logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuv committed Feb 10, 2025
1 parent 22bdd6f commit 6c8bfa5
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where

async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let app_state = AppState::from_ref(state);
let proxy_headers = app_state.config.proxy_headers;
let addr = parts
.extract::<ConnectInfo<SocketAddr>>()
.await
Expand Down Expand Up @@ -52,7 +53,31 @@ where
x_forwarded_for,
remote_client_ip,
);
tracing::info!(?real_client_ip);

let proxy_uri = headers
.get(&proxy_headers.uri)
.map(|x| x.to_str().unwrap_or_default())
.unwrap_or("");
let proxy_method = headers
.get(&proxy_headers.method)
.map(|x| x.to_str().unwrap_or_default())
.unwrap_or("");
let proxy_host = headers
.get(&proxy_headers.host)
.map(|x| x.to_str().unwrap_or_default())
.unwrap_or("");
let x_forwarded_for = headers
.get("x-forwarded-for")
.map(|x| x.to_str().unwrap_or_default())
.unwrap_or("");
tracing::info!(
real_client_ip = real_client_ip.to_string(),
proxy_uri,
proxy_method,
proxy_host,
x_forwarded_for
);

Ok(ExtractRealIp(real_client_ip))
}
}
Expand Down Expand Up @@ -159,33 +184,15 @@ fn api_server_router(state: AppState) -> Router {
.layer(
TraceLayer::new_for_http().make_span_with(move |request: &Request<_>| {
let headers = request.headers();
let proxy_uri = headers.get(&proxy_headers.uri);
let proxy_method = headers.get(&proxy_headers.method);
let proxy_host = headers.get(&proxy_headers.host);
let x_forwarded_for = headers.get("x-forwarded-for");
let uri = request.uri();

match uri.path() {
"/" | "/api/v1/waf" => {
tracing::info_span!(
"proxy_request",
uri = ?request.uri(),
proxy_uri = ?proxy_uri,
proxy_method = ?proxy_method,
proxy_host = ?proxy_host,
x_forwarded_for = ?x_forwarded_for,
)
tracing::info_span!("proxy_request",)
}
"/api/v1/health" => tracing::Span::none(),
_ => {
tracing::info_span!(
"http_request",
uri = ?request.uri(),
proxy_uri = ?proxy_uri,
proxy_method = ?proxy_method,
proxy_host = ?proxy_host,
x_forwarded_for = ?x_forwarded_for,
)
tracing::info_span!("http_request",)
}
}
}),
Expand Down

0 comments on commit 6c8bfa5

Please sign in to comment.