Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(apievent): added hs latency to api event (#2734)

harsh-sharma-juspayc124511052ed
diff --git a/crates/router/src/events/api_logs.rs b/crates/router/src/events/api_logs.rs
index 873102e81e..27a90028ba 100644
--- a/crates/router/src/events/api_logs.rs
+++ b/crates/router/src/events/api_logs.rs
@@ -38,6 +38,7 @@ pub struct ApiEvent {
response: Option<serde_json::Value>,
#[serde(flatten)]
event_type: ApiEventsType,
+ hs_latency: Option<u128>,
}
impl ApiEvent {
@@ -49,6 +50,7 @@ impl ApiEvent {
status_code: i64,
request: serde_json::Value,
response: Option<serde_json::Value>,
+ hs_latency: Option<u128>,
auth_type: AuthenticationType,
event_type: ApiEventsType,
http_req: &HttpRequest,
@@ -72,6 +74,7 @@ impl ApiEvent {
.and_then(|user_agent_value| user_agent_value.to_str().ok().map(ToOwned::to_owned)),
url_path: http_req.path().to_string(),
event_type,
+ hs_latency,
}
}
}
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index bb0e70b4b2..321bf909ea 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -830,6 +830,7 @@ where
.as_millis();
let mut serialized_response = None;
+ let mut overhead_latency = None;
let status_code = match output.as_ref() {
Ok(res) => {
if let ApplicationResponse::Json(data) = res {
@@ -839,6 +840,19 @@ where
.attach_printable("Failed to serialize json response")
.change_context(errors::ApiErrorResponse::InternalServerError.switch())?,
);
+ } else if let ApplicationResponse::JsonWithHeaders((data, headers)) = res {
+ serialized_response.replace(
+ masking::masked_serialize(&data)
+ .into_report()
+ .attach_printable("Failed to serialize json response")
+ .change_context(errors::ApiErrorResponse::InternalServerError.switch())?,
+ );
+
+ if let Some((_, value)) = headers.iter().find(|(key, _)| key == X_HS_LATENCY) {
+ if let Ok(external_latency) = value.parse::<u128>() {
+ overhead_latency.replace(external_latency);
+ }
+ }
}
event_type = res.get_api_event_type().or(event_type);
@@ -854,6 +868,7 @@ where
status_code,
serialized_request,
serialized_response,
+ overhead_latency,
auth_type,
event_type.unwrap_or(ApiEventsType::Miscellaneous),
request,