use std::sync::atomic::{AtomicU64, Ordering};
#[derive(Debug, Default)]
pub struct Metrics {
pub active_connections: AtomicU64,
pub connection_open_total: AtomicU64,
pub connection_close_total: AtomicU64,
pub reconnect_total: AtomicU64,
pub resume_success_total: AtomicU64,
pub resume_failed_total: AtomicU64,
pub heartbeat_timeout_total: AtomicU64,
pub messages_in_total: AtomicU64,
pub messages_out_total: AtomicU64,
pub messages_dropped_total: AtomicU64,
pub messages_replayed_total: AtomicU64,
pub messages_expired_total: AtomicU64,
pub ack_timeout_total: AtomicU64,
pub duplicate_total: AtomicU64,
pub send_queue_depth: AtomicU64,
pub recv_queue_depth: AtomicU64,
pub slow_consumer_total: AtomicU64,
pub flow_pause_total: AtomicU64,
pub flow_resume_total: AtomicU64,
pub volatile_drop_total: AtomicU64,
pub state_coalesce_total: AtomicU64,
}
impl Metrics {
pub fn new() -> Self {
Self::default()
}
pub fn inc(&self, counter: &AtomicU64) {
counter.fetch_add(1, Ordering::Relaxed);
}
pub fn add(&self, counter: &AtomicU64, n: u64) {
counter.fetch_add(n, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inc_and_add() {
let m = Metrics::new();
m.inc(&m.connection_open_total);
m.inc(&m.connection_open_total);
m.add(&m.messages_in_total, 7);
assert_eq!(m.connection_open_total.load(Ordering::Relaxed), 2);
assert_eq!(m.messages_in_total.load(Ordering::Relaxed), 7);
}
}