Skip to content

Commit

Permalink
feat: envar interval config
Browse files Browse the repository at this point in the history
  • Loading branch information
apskhem committed Jan 16, 2025
1 parent 11e4144 commit dbfe1d8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 54 deletions.
15 changes: 9 additions & 6 deletions catalyst-gateway/bin/src/metrics/chain_follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
44 changes: 0 additions & 44 deletions catalyst-gateway/bin/src/metrics/chain_indexer.rs
Original file line number Diff line number Diff line change
@@ -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"];
}
2 changes: 1 addition & 1 deletion catalyst-gateway/bin/src/metrics/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 2 additions & 3 deletions catalyst-gateway/bin/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
15 changes: 15 additions & 0 deletions catalyst-gateway/bin/src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -194,6 +200,10 @@ static ENV_VARS: LazyLock<EnvVars> = LazyLock::new(|| {
"METRICS_MEMORY_INTERVAL",
METRICS_MEMORY_INTERVAL_DEFAULT,
),
metrics_follower_interval: StringEnvVar::new_as_duration(
"METRICS_FOLLOWER_INTERVAL",
METRICS_FOLLOWER_INTERVAL_DEFAULT,
),
}
});

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dbfe1d8

Please sign in to comment.