Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 9 commits into from
Nov 13, 2023
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 @@ -13,6 +13,7 @@ pub struct ApiEvent {
status_code: i64,
request: serde_json::Value,
response: Option<serde_json::Value>,
hs_latency: Option<u128>,
}

impl ApiEvent {
Expand All @@ -23,6 +24,7 @@ impl ApiEvent {
status_code: i64,
request: serde_json::Value,
response: Option<serde_json::Value>,
hs_latency: Option<u128>,
) -> Self {
Self {
api_flow: api_flow.to_string(),
Expand All @@ -32,6 +34,7 @@ impl ApiEvent {
status_code,
request,
response,
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 @@ -818,6 +818,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 @@ -827,6 +828,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(request_duration - external_latency);
}
}
}

metrics::request::track_response_status_code(res)
Expand All @@ -841,6 +855,7 @@ where
status_code,
serialized_request,
serialized_response,
overhead_latency,
);
match api_event.clone().try_into() {
Ok(event) => {
Expand Down
Loading