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(metrics): refine API latency histogram buckets (#14569)
Kashifd108440e4362
diff --git a/crates/router/src/routes/metrics.rs b/crates/router/src/routes/metrics.rsindex 7f9876be44..39620b3393 100644--- a/crates/router/src/routes/metrics.rs+++ b/crates/router/src/routes/metrics.rs@@ -13,7 +13,11 @@ counter_metric!(PREFERRED_CONNECTORS_UPDATE_FAILURES, GLOBAL_METER);// API Level Metricscounter_metric!(REQUESTS_RECEIVED, GLOBAL_METER);-histogram_metric_f64!(REQUEST_TIME, GLOBAL_METER);+histogram_metric_f64!(+ REQUEST_TIME,+ GLOBAL_METER,+ boundaries: router_env::metrics::exponential_histogram_buckets(),+);histogram_metric_f64!(PAYMENT_OPERATION_DURATION,diff --git a/crates/router_env/src/metrics.rs b/crates/router_env/src/metrics.rsindex 4fe6c18ca2..5de49d41ba 100644--- a/crates/router_env/src/metrics.rs+++ b/crates/router_env/src/metrics.rs@@ -59,6 +59,16 @@ macro_rules! counter_metric {/// [Meter]: opentelemetry::metrics::Meter#[macro_export]macro_rules! histogram_metric_f64 {+ ($name:ident, $meter:ident, boundaries: $boundaries:expr $(,)?) => {+ pub(crate) static $name: ::std::sync::LazyLock<+ $crate::opentelemetry::metrics::Histogram<f64>,+ > = ::std::sync::LazyLock::new(|| {+ $meter+ .f64_histogram(stringify!($name))+ .with_boundaries($boundaries)+ .build()+ });+ };($name:ident, $meter:ident) => {pub(crate) static $name: ::std::sync::LazyLock<$crate::opentelemetry::metrics::Histogram<f64>,@@ -158,9 +168,17 @@ macro_rules! metric_attributes {};}-pub use helpers::f64_histogram_buckets;+pub use helpers::{exponential_histogram_buckets, f64_histogram_buckets};mod helpers {+ /// 134 latency boundaries in seconds: 100us to 52.4288s, seven intervals per doubling.+ #[inline(always)]+ pub fn exponential_histogram_buckets() -> Vec<f64> {+ (0..134)+ .map(|index| 0.000_1 * 2_f64.powf(f64::from(index) / 7.0))+ .collect()+ }+/// Returns the buckets to be used for a f64 histogram#[inline(always)]pub fn f64_histogram_buckets() -> Vec<f64> {@@ -174,4 +192,30 @@ mod helpers {Vec::from(buckets)}++ #[cfg(test)]+ mod tests {+ use super::{exponential_histogram_buckets, f64_histogram_buckets};++ #[test]+ fn histogram_boundaries() {+ let buckets = exponential_histogram_buckets();+ assert_eq!(buckets.len(), 134);+ assert!(buckets+ .first()+ .is_some_and(|&value| (value - 0.000_1).abs() < 1e-12));+ assert!(buckets+ .last()+ .is_some_and(|&value| (value - 52.4288).abs() < 1e-9));+ for (&lower, &upper) in buckets.iter().zip(buckets.iter().skip(1)) {+ assert!(upper > lower);+ assert!(upper / lower < 1.105);+ }+ for (&lower, &upper) in buckets.iter().zip(buckets.iter().skip(7)) {+ assert!((upper / lower - 2.0).abs() < 1e-12);+ }+ let expected: Vec<f64> = (0..30).map(|index| 0.000_001 * 2_f64.powi(index)).collect();+ assert_eq!(f64_histogram_buckets(), expected);+ }+ }}