From 164d1c66fbcb84104db07412496114db2f8c5c0c Mon Sep 17 00:00:00 2001 From: Sampras Lopes Date: Wed, 8 Nov 2023 15:59:13 +0530 Subject: [PATCH] feat(events): add request details to api events (#2769) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- crates/router/src/events/api_logs.rs | 15 +++++++++++++++ crates/router/src/services/api.rs | 1 + 2 files changed, 16 insertions(+) diff --git a/crates/router/src/events/api_logs.rs b/crates/router/src/events/api_logs.rs index 35eaf1edae7f..5a66ba3e0bf9 100644 --- a/crates/router/src/events/api_logs.rs +++ b/crates/router/src/events/api_logs.rs @@ -1,3 +1,4 @@ +use actix_web::HttpRequest; use router_env::{tracing_actix_web::RequestId, types::FlowMetric}; use serde::Serialize; use time::OffsetDateTime; @@ -15,10 +16,14 @@ pub struct ApiEvent { #[serde(flatten)] auth_type: AuthenticationType, request: serde_json::Value, + user_agent: Option, + ip_addr: Option, + url_path: String, response: Option, } impl ApiEvent { + #[allow(clippy::too_many_arguments)] pub fn new( api_flow: &impl FlowMetric, request_id: &RequestId, @@ -27,6 +32,7 @@ impl ApiEvent { request: serde_json::Value, response: Option, auth_type: AuthenticationType, + http_req: &HttpRequest, ) -> Self { Self { api_flow: api_flow.to_string(), @@ -37,6 +43,15 @@ impl ApiEvent { request, response, auth_type, + ip_addr: http_req + .connection_info() + .realip_remote_addr() + .map(ToOwned::to_owned), + user_agent: http_req + .headers() + .get("user-agent") + .and_then(|user_agent_value| user_agent_value.to_str().ok().map(ToOwned::to_owned)), + url_path: http_req.path().to_string(), } } } diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs index c942f09f96aa..362644906971 100644 --- a/crates/router/src/services/api.rs +++ b/crates/router/src/services/api.rs @@ -852,6 +852,7 @@ where serialized_request, serialized_response, auth_type, + request, ); match api_event.clone().try_into() { Ok(event) => {