From dbfe1d8a5347f1792b6476e9c7a7527ef5896c9b Mon Sep 17 00:00:00 2001 From: Apisit Ritreungroj Date: Thu, 16 Jan 2025 20:52:56 +0700 Subject: [PATCH] feat: envar interval config --- .../bin/src/metrics/chain_follower.rs | 15 ++++--- .../bin/src/metrics/chain_indexer.rs | 44 ------------------- catalyst-gateway/bin/src/metrics/memory.rs | 2 +- catalyst-gateway/bin/src/metrics/mod.rs | 5 +-- catalyst-gateway/bin/src/settings/mod.rs | 15 +++++++ 5 files changed, 27 insertions(+), 54 deletions(-) diff --git a/catalyst-gateway/bin/src/metrics/chain_follower.rs b/catalyst-gateway/bin/src/metrics/chain_follower.rs index fe40137f34d..8d44c95fcff 100644 --- a/catalyst-gateway/bin/src/metrics/chain_follower.rs +++ b/catalyst-gateway/bin/src/metrics/chain_follower.rs @@ -5,36 +5,39 @@ use std::{ thread, }; +use cardano_chain_follower::Statistics; + use crate::settings::Settings; /// This is to prevent the init function from accidentally being called multiple times. static IS_INITIALIZED: AtomicBool = AtomicBool::new(false); -/// Starts a background thread to periodically update memory metrics. +/// Starts a background thread to periodically update Chain Follower metrics. /// /// This function spawns a thread that updates the memory metrics /// at regular intervals defined by the `METRICS_MEMORY_INTERVAL` envar. -pub(crate) fn init_metrics_updater() { +pub(crate) fn init_metrics_reporter() { if IS_INITIALIZED.swap(true, Ordering::SeqCst) { return; } let api_host_names = Settings::api_host_names().join(","); let service_id = Settings::service_id(); + let chain = Settings::cardano_network(); thread::spawn(move || { loop { { - + let chain_stats = Statistics::new(chain); } - thread::sleep(Settings::metrics_memory_interval()); + thread::sleep(Settings::metrics_follower_interval()); } }); } -/// All the related memory reporting metrics to the Prometheus service are inside this -/// module. +/// All the related Chain Follower reporting metrics to the Prometheus service are inside +/// this module. mod reporter { use std::sync::LazyLock; diff --git a/catalyst-gateway/bin/src/metrics/chain_indexer.rs b/catalyst-gateway/bin/src/metrics/chain_indexer.rs index 9f174c668c4..ee8728166ab 100644 --- a/catalyst-gateway/bin/src/metrics/chain_indexer.rs +++ b/catalyst-gateway/bin/src/metrics/chain_indexer.rs @@ -1,45 +1 @@ //! Metrics related to Chain Indexer analytics. - -use std::{ - sync::atomic::{AtomicBool, Ordering}, - thread, -}; - -use crate::settings::Settings; - -/// This is to prevent the init function from accidentally being called multiple times. -static IS_INITIALIZED: AtomicBool = AtomicBool::new(false); - -/// Starts a background thread to periodically update memory metrics. -/// -/// This function spawns a thread that updates the memory metrics -/// at regular intervals defined by the `METRICS_MEMORY_INTERVAL` envar. -pub(crate) fn init_metrics_updater() { - if IS_INITIALIZED.swap(true, Ordering::SeqCst) { - return; - } - - let api_host_names = Settings::api_host_names().join(","); - let service_id = Settings::service_id(); - - thread::spawn(move || { - loop { - { - - } - - thread::sleep(Settings::metrics_memory_interval()); - } - }); -} - -/// All the related memory reporting metrics to the Prometheus service are inside this -/// module. -mod reporter { - use std::sync::LazyLock; - - use prometheus::{register_int_gauge_vec, IntGaugeVec}; - - /// Labels for the chain follower metrics - const MEMORY_METRIC_LABELS: [&str; 2] = ["api_host_names", "service_id"]; -} diff --git a/catalyst-gateway/bin/src/metrics/memory.rs b/catalyst-gateway/bin/src/metrics/memory.rs index d8801a5f1f0..3c2d783a396 100644 --- a/catalyst-gateway/bin/src/metrics/memory.rs +++ b/catalyst-gateway/bin/src/metrics/memory.rs @@ -24,7 +24,7 @@ static IS_INITIALIZED: AtomicBool = AtomicBool::new(false); /// /// This function spawns a thread that updates the memory metrics /// at regular intervals defined by the `METRICS_MEMORY_INTERVAL` envar. -pub(crate) fn init_metrics_updater() { +pub(crate) fn init_metrics_reporter() { if IS_INITIALIZED.swap(true, Ordering::SeqCst) { return; } diff --git a/catalyst-gateway/bin/src/metrics/mod.rs b/catalyst-gateway/bin/src/metrics/mod.rs index bcf5b986fe2..c9870134ca1 100644 --- a/catalyst-gateway/bin/src/metrics/mod.rs +++ b/catalyst-gateway/bin/src/metrics/mod.rs @@ -14,9 +14,8 @@ pub(crate) mod memory; /// Returns the default prometheus registry. #[must_use] pub(crate) fn init_prometheus() -> Registry { - chain_follower::init_metrics_updater(); - chain_indexer::init_metrics_updater(); - memory::init_metrics_updater(); + chain_follower::init_metrics_reporter(); + memory::init_metrics_reporter(); default_registry().clone() } diff --git a/catalyst-gateway/bin/src/settings/mod.rs b/catalyst-gateway/bin/src/settings/mod.rs index d3702d39373..8cd0cc85fae 100644 --- a/catalyst-gateway/bin/src/settings/mod.rs +++ b/catalyst-gateway/bin/src/settings/mod.rs @@ -54,6 +54,9 @@ const CHECK_CONFIG_TICK_DEFAULT: &str = "5s"; /// Default `METRICS_MEMORY_INTERVAL`. const METRICS_MEMORY_INTERVAL_DEFAULT: &str = "1s"; +/// Default `METRICS_FOLLOWER_INTERVAL`. +const METRICS_FOLLOWER_INTERVAL_DEFAULT: &str = "1s"; + /// Default Event DB URL. const EVENT_DB_URL_DEFAULT: &str = "postgresql://postgres:postgres@localhost/catalyst_events?sslmode=disable"; @@ -149,6 +152,9 @@ struct EnvVars { /// Interval for updating and sending memory metrics. metrics_memory_interval: Duration, + + /// Interval for updating and sending Chain Follower metrics. + metrics_follower_interval: Duration, } // Lazy initialization of all env vars which are not command line parameters. @@ -194,6 +200,10 @@ static ENV_VARS: LazyLock = LazyLock::new(|| { "METRICS_MEMORY_INTERVAL", METRICS_MEMORY_INTERVAL_DEFAULT, ), + metrics_follower_interval: StringEnvVar::new_as_duration( + "METRICS_FOLLOWER_INTERVAL", + METRICS_FOLLOWER_INTERVAL_DEFAULT, + ), } }); @@ -292,6 +302,11 @@ impl Settings { ENV_VARS.metrics_memory_interval } + /// The Chain Follower metrics interval + pub(crate) fn metrics_follower_interval() -> Duration { + ENV_VARS.metrics_follower_interval + } + /// Get a list of all host names to serve the API on. /// /// Used by the `OpenAPI` Documentation to point to the correct backend.