fuel_core_metrics/
futures.rsuse crate::global_registry;
use prometheus_client::{
encoding::text::encode,
metrics::counter::Counter,
};
use std::ops::Deref;
pub mod future_tracker;
pub mod metered_future;
#[derive(Default, Debug, Clone)]
pub struct FuturesMetrics {
pub busy: Counter,
pub idle: Counter,
}
impl FuturesMetrics {
pub fn obtain_futures_metrics(futures_name: &str) -> FuturesMetrics {
let reg =
regex::Regex::new("^[a-zA-Z_:][a-zA-Z0-9_:]*$").expect("It is a valid Regex");
if !reg.is_match(futures_name) {
panic!("The futures metric {} has incorrect name.", futures_name);
}
let lifecycle = FuturesMetrics::default();
let mut lock = global_registry().registry.lock();
let mut encoded_bytes = String::new();
encode(&mut encoded_bytes, lock.deref())
.expect("Unable to decode futures metrics");
let reg = regex::Regex::new(format!("\\b{}\\b", futures_name).as_str())
.expect("It is a valid Regex");
if reg.is_match(encoded_bytes.as_str()) {
tracing::warn!(
"Futures metrics with '{}' name is already registered",
futures_name
);
}
lock.register(
format!("{}_idle_ns", futures_name),
format!("The idle time of the {} future", futures_name),
lifecycle.idle.clone(),
);
lock.register(
format!("{}_busy_ns", futures_name),
format!("The busy time of the {} future", futures_name),
lifecycle.busy.clone(),
);
lifecycle
}
}
#[test]
fn register_success() {
FuturesMetrics::obtain_futures_metrics("Foo");
FuturesMetrics::obtain_futures_metrics("Bar");
}