Skip to content

Commit c124511

Browse files
feat(apievent): added hs latency to api event (#2734)
Co-authored-by: Sampras lopes <[email protected]>
1 parent 8e538db commit c124511

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

crates/router/src/events/api_logs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub struct ApiEvent {
3838
response: Option<serde_json::Value>,
3939
#[serde(flatten)]
4040
event_type: ApiEventsType,
41+
hs_latency: Option<u128>,
4142
}
4243

4344
impl ApiEvent {
@@ -49,6 +50,7 @@ impl ApiEvent {
4950
status_code: i64,
5051
request: serde_json::Value,
5152
response: Option<serde_json::Value>,
53+
hs_latency: Option<u128>,
5254
auth_type: AuthenticationType,
5355
event_type: ApiEventsType,
5456
http_req: &HttpRequest,
@@ -72,6 +74,7 @@ impl ApiEvent {
7274
.and_then(|user_agent_value| user_agent_value.to_str().ok().map(ToOwned::to_owned)),
7375
url_path: http_req.path().to_string(),
7476
event_type,
77+
hs_latency,
7578
}
7679
}
7780
}

crates/router/src/services/api.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ where
830830
.as_millis();
831831

832832
let mut serialized_response = None;
833+
let mut overhead_latency = None;
833834
let status_code = match output.as_ref() {
834835
Ok(res) => {
835836
if let ApplicationResponse::Json(data) = res {
@@ -839,6 +840,19 @@ where
839840
.attach_printable("Failed to serialize json response")
840841
.change_context(errors::ApiErrorResponse::InternalServerError.switch())?,
841842
);
843+
} else if let ApplicationResponse::JsonWithHeaders((data, headers)) = res {
844+
serialized_response.replace(
845+
masking::masked_serialize(&data)
846+
.into_report()
847+
.attach_printable("Failed to serialize json response")
848+
.change_context(errors::ApiErrorResponse::InternalServerError.switch())?,
849+
);
850+
851+
if let Some((_, value)) = headers.iter().find(|(key, _)| key == X_HS_LATENCY) {
852+
if let Ok(external_latency) = value.parse::<u128>() {
853+
overhead_latency.replace(external_latency);
854+
}
855+
}
842856
}
843857
event_type = res.get_api_event_type().or(event_type);
844858

@@ -854,6 +868,7 @@ where
854868
status_code,
855869
serialized_request,
856870
serialized_response,
871+
overhead_latency,
857872
auth_type,
858873
event_type.unwrap_or(ApiEventsType::Miscellaneous),
859874
request,

0 commit comments

Comments
 (0)