use std::sync::OnceLock;
use prometheus::{Counter, Gauge, HistogramOpts, HistogramVec, Registry};
pub struct Collectors {
pub registry: Registry,
pub restarts_total: Counter,
pub active_services: Gauge,
pub spawned_total: Counter,
pub terminated_total: Counter,
pub circuit_breaker_trips: Counter,
pub uptime_seconds: HistogramVec,
}
static COLLECTORS: OnceLock<Collectors> = OnceLock::new();
pub fn collectors() -> &'static Collectors {
COLLECTORS.get_or_init(Collectors::new)
}
pub fn registry() -> &'static Registry {
&collectors().registry
}
impl Collectors {
fn new() -> Self {
let registry = Registry::new();
let restarts_total = Counter::new(
"rustrade_supervisor_restarts_total",
"Total number of service restarts across all services",
)
.expect("create restarts_total counter");
let active_services = Gauge::new(
"rustrade_supervisor_active_services",
"Number of services currently in a non-terminal phase",
)
.expect("create active_services gauge");
let spawned_total = Counter::new(
"rustrade_supervisor_spawned_total",
"Total number of services ever spawned (including restarts)",
)
.expect("create spawned_total counter");
let terminated_total = Counter::new(
"rustrade_supervisor_terminated_total",
"Total number of services that have terminated",
)
.expect("create terminated_total counter");
let circuit_breaker_trips = Counter::new(
"rustrade_supervisor_circuit_breaker_trips_total",
"Total number of circuit breaker trips across all services",
)
.expect("create circuit_breaker_trips counter");
let uptime_seconds = HistogramVec::new(
HistogramOpts::new(
"rustrade_supervisor_uptime_seconds",
"Cumulative running time of a service at termination, in seconds",
),
&["service"],
)
.expect("create uptime_seconds histogram_vec");
registry
.register(Box::new(restarts_total.clone()))
.expect("register restarts_total");
registry
.register(Box::new(active_services.clone()))
.expect("register active_services");
registry
.register(Box::new(spawned_total.clone()))
.expect("register spawned_total");
registry
.register(Box::new(terminated_total.clone()))
.expect("register terminated_total");
registry
.register(Box::new(circuit_breaker_trips.clone()))
.expect("register circuit_breaker_trips");
registry
.register(Box::new(uptime_seconds.clone()))
.expect("register uptime_seconds");
Self {
registry,
restarts_total,
active_services,
spawned_total,
terminated_total,
circuit_breaker_trips,
uptime_seconds,
}
}
}