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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor(middleware): add middleware to record metrics for request count and duration (#7803)
Sanchith Hegde54a1719fe6aa
diff --git a/crates/analytics/src/metrics/request.rs b/crates/analytics/src/metrics/request.rsindex c30e34da8e..7b84347b73 100644--- a/crates/analytics/src/metrics/request.rs+++ b/crates/analytics/src/metrics/request.rs@@ -1,5 +1,3 @@-use std::time;-#[inline]pub async fn record_operation_time<F, R, T>(future: F,@@ -11,7 +9,7 @@ whereF: futures::Future<Output = R>,T: ToString,{- let (result, time) = time_future(future).await;+ let (result, time) = common_utils::metrics::utils::time_future(future).await;let attributes = router_env::metric_attributes!(("metric_name", metric_name.to_string()),("source", source.to_string()),@@ -22,14 +20,3 @@ whererouter_env::logger::debug!("Attributes: {:?}, Time: {}", attributes, value);result}--#[inline]-pub async fn time_future<F, R>(future: F) -> (R, time::Duration)-where- F: futures::Future<Output = R>,-{- let start = time::Instant::now();- let result = future.await;- let time_spent = start.elapsed();- (result, time_spent)-}diff --git a/crates/router/src/compatibility/wrap.rs b/crates/router/src/compatibility/wrap.rsindex 2c1dea5f6d..37fb0e4f0b 100644--- a/crates/router/src/compatibility/wrap.rs+++ b/crates/router/src/compatibility/wrap.rs@@ -10,7 +10,7 @@ use crate::{events::api_logs::ApiEventMetric,routes::{app::{AppStateInfo, ReqState},- metrics, AppState, SessionState,+ AppState, SessionState,},services::{self, api, authentication as auth, logger},};@@ -44,18 +44,15 @@ wherelet start_instant = Instant::now();logger::info!(tag = ?Tag::BeginRequest, payload = ?payload);- let server_wrap_util_res = metrics::request::record_request_time_metric(- api::server_wrap_util(- &flow,- state.clone().into(),- request.headers(),- request,- payload,- func,- api_authentication,- lock_action,- ),+ let server_wrap_util_res = api::server_wrap_util(&flow,+ state.clone().into(),+ request.headers(),+ request,+ payload,+ func,+ api_authentication,+ lock_action,).await.map(|response| {diff --git a/crates/router/src/lib.rs b/crates/router/src/lib.rsindex dc7cb1233f..094b753773 100644--- a/crates/router/src/lib.rs+++ b/crates/router/src/lib.rs@@ -379,6 +379,7 @@ pub fn get_application_builder(// this middleware works only for Http1.1 requests.wrap(middleware::Http400RequestDetailsLogger).wrap(middleware::AddAcceptLanguageHeader)+ .wrap(middleware::RequestResponseMetrics).wrap(middleware::LogSpanInitializer).wrap(router_env::tracing_actix_web::TracingLogger::default())}diff --git a/crates/router/src/middleware.rs b/crates/router/src/middleware.rsindex 0185d8a076..64a2138519 100644--- a/crates/router/src/middleware.rs+++ b/crates/router/src/middleware.rs@@ -5,7 +5,8 @@ use router_env::{tracing::{field::Empty, Instrument},};-use crate::headers;+use crate::{headers, routes::metrics};+/// Middleware to include request ID in response header.pub struct RequestId;@@ -363,6 +364,7 @@ wheretype Future = futures::future::LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;actix_web::dev::forward_ready!(service);+fn call(&self, mut req: actix_web::dev::ServiceRequest) -> Self::Future {let svc = self.service.clone();Box::pin(async move {@@ -396,3 +398,87 @@ where})}}++/// Middleware for recording request-response metrics+pub struct RequestResponseMetrics;++impl<S: 'static, B> actix_web::dev::Transform<S, actix_web::dev::ServiceRequest>+ for RequestResponseMetrics+where+ S: actix_web::dev::Service<+ actix_web::dev::ServiceRequest,+ Response = actix_web::dev::ServiceResponse<B>,+ Error = actix_web::Error,+ >,+ S::Future: 'static,+ B: 'static,+{+ type Response = actix_web::dev::ServiceResponse<B>;+ type Error = actix_web::Error;+ type Transform = RequestResponseMetricsMiddleware<S>;+ type InitError = ();+ type Future = std::future::Ready<Result<Self::Transform, Self::InitError>>;++ fn new_transform(&self, service: S) -> Self::Future {+ std::future::ready(Ok(RequestResponseMetricsMiddleware {+ service: std::rc::Rc::new(service),+ }))+ }+}++pub struct RequestResponseMetricsMiddleware<S> {+ service: std::rc::Rc<S>,+}++impl<S, B> actix_web::dev::Service<actix_web::dev::ServiceRequest>+ for RequestResponseMetricsMiddleware<S>+where+ S: actix_web::dev::Service<+ actix_web::dev::ServiceRequest,+ Response = actix_web::dev::ServiceResponse<B>,+ Error = actix_web::Error,+ > + 'static,+ S::Future: 'static,+ B: 'static,+{+ type Response = actix_web::dev::ServiceResponse<B>;+ type Error = actix_web::Error;+ type Future = futures::future::LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;++ actix_web::dev::forward_ready!(service);++ fn call(&self, req: actix_web::dev::ServiceRequest) -> Self::Future {+ use std::borrow::Cow;++ let svc = self.service.clone();++ let request_path = req+ .match_pattern()+ .map(Cow::<'static, str>::from)+ .unwrap_or_else(|| "UNKNOWN".into());+ let request_method = Cow::<'static, str>::from(req.method().as_str().to_owned());++ Box::pin(async move {+ let mut attributes =+ router_env::metric_attributes!(("path", request_path), ("method", request_method))+ .to_vec();++ let response_fut = svc.call(req);++ metrics::REQUESTS_RECEIVED.add(1, &attributes);++ let (response_result, request_duration) =+ common_utils::metrics::utils::time_future(response_fut).await;+ let response = response_result?;++ attributes.extend_from_slice(router_env::metric_attributes!((+ "status_code",+ i64::from(response.status().as_u16())+ )));++ metrics::REQUEST_TIME.record(request_duration.as_secs_f64(), &attributes);++ Ok(response)+ })+ }+}diff --git a/crates/router/src/routes/metrics.rs b/crates/router/src/routes/metrics.rsindex edb33f4876..63ef0b876f 100644--- a/crates/router/src/routes/metrics.rs+++ b/crates/router/src/routes/metrics.rs@@ -1,6 +1,5 @@pub mod bg_metrics_collector;pub mod request;-pub mod utils;use router_env::{counter_metric, global_meter, histogram_metric_f64};@@ -11,7 +10,6 @@ counter_metric!(KV_MISS, GLOBAL_METER); // No. of KV misses// API Level Metricscounter_metric!(REQUESTS_RECEIVED, GLOBAL_METER);-counter_metric!(REQUEST_STATUS, GLOBAL_METER);histogram_metric_f64!(REQUEST_TIME, GLOBAL_METER);histogram_metric_f64!(EXTERNAL_REQUEST_TIME, GLOBAL_METER);diff --git a/crates/router/src/routes/metrics/request.rs b/crates/router/src/routes/metrics/request.rsindex c2d49cd7ae..4e329005d4 100644--- a/crates/router/src/routes/metrics/request.rs+++ b/crates/router/src/routes/metrics/request.rs@@ -1,38 +1,5 @@-use super::utils as metric_utils;use crate::services::ApplicationResponse;-pub async fn record_request_time_metric<F, R>(- future: F,- flow: &impl router_env::types::FlowMetric,-) -> R-where- F: futures::Future<Output = R>,-{- let key = "request_type";- super::REQUESTS_RECEIVED.add(1, router_env::metric_attributes!((key, flow.to_string())));- let (result, time) = metric_utils::time_future(future).await;- super::REQUEST_TIME.record(- time.as_secs_f64(),- router_env::metric_attributes!((key, flow.to_string())),- );- result-}--pub fn status_code_metrics(- status_code: String,- flow: String,- merchant_id: common_utils::id_type::MerchantId,-) {- super::REQUEST_STATUS.add(- 1,- router_env::metric_attributes!(- ("status_code", status_code),- ("flow", flow),- ("merchant_id", merchant_id.clone()),- ),- )-}-pub fn track_response_status_code<Q>(response: &ApplicationResponse<Q>) -> i64 {match response {ApplicationResponse::Json(_)diff --git a/crates/router/src/routes/metrics/utils.rs b/crates/router/src/routes/metrics/utils.rsdeleted file mode 100644index 28bf2131f9..0000000000--- a/crates/router/src/routes/metrics/utils.rs+++ /dev/null@@ -1,12 +0,0 @@-use std::time;--#[inline]-pub async fn time_future<F, R>(future: F) -> (R, time::Duration)-where- F: futures::Future<Output = R>,-{- let start = time::Instant::now();- let result = future.await;- let time_spent = start.elapsed();- (result, time_spent)-}diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rsindex 5f3ce598ab..ad0b6c4840 100644--- a/crates/router/src/services/api.rs+++ b/crates/router/src/services/api.rs@@ -873,12 +873,6 @@ where);state.event_handler().log_event(&api_event);- metrics::request::status_code_metrics(- status_code.to_string(),- flow.to_string(),- merchant_id.to_owned(),- );-output}@@ -932,18 +926,15 @@ wheretag = ?Tag::BeginRequest, payload = ?payload,headers = ?incoming_header_to_log);- let server_wrap_util_res = metrics::request::record_request_time_metric(- server_wrap_util(- &flow,- state.clone(),- incoming_request_header,- request,- payload,- func,- api_auth,- lock_action,- ),+ let server_wrap_util_res = server_wrap_util(&flow,+ state.clone(),+ incoming_request_header,+ request,+ payload,+ func,+ api_auth,+ lock_action,).await.map(|response| {