use std::sync::Arc;
use zerodds_monitor::{Counter, Labels, default_registry};
pub const SAMPLES_FORWARDED_TOTAL: &str = "dds_router_samples_forwarded_total";
pub const SAMPLES_DROPPED_LOOP_TOTAL: &str = "dds_router_samples_dropped_loop_total";
pub const SAMPLES_DROPPED_FILTER_TOTAL: &str = "dds_router_samples_dropped_filter_total";
pub const LIFECYCLE_FORWARDED_TOTAL: &str = "dds_router_lifecycle_forwarded_total";
pub const FORWARD_ERRORS_TOTAL: &str = "dds_router_forward_errors_total";
#[derive(Clone)]
pub struct RouteMetrics {
forwarded: Arc<Counter>,
dropped_loop: Arc<Counter>,
dropped_filter: Arc<Counter>,
lifecycle: Arc<Counter>,
errors: Arc<Counter>,
}
impl RouteMetrics {
#[must_use]
pub fn new(route: &str) -> Self {
let reg = default_registry();
reg.set_help(
SAMPLES_FORWARDED_TOTAL,
"Alive samples forwarded by the router.",
);
reg.set_help(
SAMPLES_DROPPED_LOOP_TOTAL,
"Samples dropped by the router loop guard.",
);
reg.set_help(
SAMPLES_DROPPED_FILTER_TOTAL,
"Samples dropped by the router content filter.",
);
reg.set_help(
LIFECYCLE_FORWARDED_TOTAL,
"Instance lifecycle events forwarded by the router.",
);
reg.set_help(FORWARD_ERRORS_TOTAL, "Router output write failures.");
let lbl = || Labels::new().with("route", route.to_string());
Self {
forwarded: reg.counter(SAMPLES_FORWARDED_TOTAL, lbl()),
dropped_loop: reg.counter(SAMPLES_DROPPED_LOOP_TOTAL, lbl()),
dropped_filter: reg.counter(SAMPLES_DROPPED_FILTER_TOTAL, lbl()),
lifecycle: reg.counter(LIFECYCLE_FORWARDED_TOTAL, lbl()),
errors: reg.counter(FORWARD_ERRORS_TOTAL, lbl()),
}
}
pub fn inc_forwarded(&self) {
self.forwarded.inc();
}
pub fn inc_dropped_loop(&self) {
self.dropped_loop.inc();
}
pub fn inc_dropped_filter(&self) {
self.dropped_filter.inc();
}
pub fn inc_lifecycle(&self) {
self.lifecycle.inc();
}
pub fn inc_errors(&self) {
self.errors.inc();
}
#[must_use]
pub fn snapshot(&self) -> RouteMetricsSnapshot {
RouteMetricsSnapshot {
forwarded: self.forwarded.get(),
dropped_loop: self.dropped_loop.get(),
dropped_filter: self.dropped_filter.get(),
lifecycle: self.lifecycle.get(),
errors: self.errors.get(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RouteMetricsSnapshot {
pub forwarded: u64,
pub dropped_loop: u64,
pub dropped_filter: u64,
pub lifecycle: u64,
pub errors: u64,
}