Skip to content

Commit

Permalink
feat(apievent): added hs latency to api event (#2734)
Browse files Browse the repository at this point in the history
Co-authored-by: Sampras lopes <[email protected]>
  • Loading branch information
harsh-sharma-juspay and lsampras authored Nov 13, 2023
1 parent 8e538db commit c124511
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/router/src/events/api_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct ApiEvent {
response: Option<serde_json::Value>,
#[serde(flatten)]
event_type: ApiEventsType,
hs_latency: Option<u128>,
}

impl ApiEvent {
Expand All @@ -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,
Expand All @@ -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,
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/router/src/services/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand All @@ -854,6 +868,7 @@ where
status_code,
serialized_request,
serialized_response,
overhead_latency,
auth_type,
event_type.unwrap_or(ApiEventsType::Miscellaneous),
request,
Expand Down

0 comments on commit c124511

Please sign in to comment.