feat(metrics): add support for gauge metrics and include IMC metrics (#4939)

Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
This commit is contained in:
Chethan Rao
2024-06-11 20:22:20 +05:30
committed by GitHub
parent a949676f8b
commit 42cd769407
10 changed files with 95 additions and 4 deletions

View File

@ -2,6 +2,7 @@ use router::{
configs::settings::{CmdLineConf, Settings},
core::errors::{ApplicationError, ApplicationResult},
logger,
routes::metrics,
};
#[tokio::main]
@ -27,6 +28,11 @@ async fn main() -> ApplicationResult<()> {
logger::info!("Application started [{:?}] [{:?}]", conf.server, conf.log);
// Spawn a thread for collecting metrics at fixed intervals
metrics::bg_metrics_collector::spawn_metrics_collector(
&conf.log.telemetry.bg_metrics_collection_interval_in_secs,
);
#[allow(clippy::expect_used)]
let server = Box::pin(router::start_server(conf))
.await

View File

@ -1,4 +1,8 @@
use router_env::{counter_metric, global_meter, histogram_metric, metrics_context};
pub mod bg_metrics_collector;
pub mod request;
pub mod utils;
use router_env::{counter_metric, gauge_metric, global_meter, histogram_metric, metrics_context};
metrics_context!(CONTEXT);
global_meter!(GLOBAL_METER, "ROUTER_API");
@ -133,5 +137,5 @@ counter_metric!(ACCESS_TOKEN_CACHE_HIT, GLOBAL_METER);
// A counter to indicate the access token cache miss
counter_metric!(ACCESS_TOKEN_CACHE_MISS, GLOBAL_METER);
pub mod request;
pub mod utils;
// Metrics for In-memory cache
gauge_metric!(CACHE_ENTRY_COUNT, GLOBAL_METER);

View File

@ -0,0 +1,46 @@
use storage_impl::redis::cache;
const DEFAULT_BG_METRICS_COLLECTION_INTERVAL_IN_SECS: u16 = 15;
macro_rules! gauge_metrics_for_imc {
($($cache:ident),*) => {
$(
{
cache::$cache.run_pending_tasks().await;
super::CACHE_ENTRY_COUNT.observe(
&super::CONTEXT,
cache::$cache.get_entry_count(),
&[super::request::add_attributes(
"cache_type",
stringify!($cache),
)],
);
}
)*
};
}
pub fn spawn_metrics_collector(metrics_collection_interval_in_secs: &Option<u16>) {
let metrics_collection_interval = metrics_collection_interval_in_secs
.unwrap_or(DEFAULT_BG_METRICS_COLLECTION_INTERVAL_IN_SECS);
tokio::spawn(async move {
loop {
gauge_metrics_for_imc!(
CONFIG_CACHE,
ACCOUNTS_CACHE,
ROUTING_CACHE,
CGRAPH_CACHE,
PM_FILTERS_CGRAPH_CACHE,
DECISION_MANAGER_CACHE,
SURCHARGE_CACHE
);
tokio::time::sleep(std::time::Duration::from_secs(
metrics_collection_interval.into(),
))
.await
}
});
}