sentinel_proxy/agents/
metrics.rs

1//! Agent metrics collection.
2
3use std::sync::atomic::{AtomicU64, Ordering};
4
5/// Agent metrics collector.
6///
7/// Tracks call counts, success/failure rates, and decision distributions.
8#[derive(Default)]
9pub struct AgentMetrics {
10    /// Total calls
11    pub calls_total: AtomicU64,
12    /// Successful calls
13    pub calls_success: AtomicU64,
14    /// Failed calls
15    pub calls_failed: AtomicU64,
16    /// Timeout calls
17    pub calls_timeout: AtomicU64,
18    /// Circuit breaker trips
19    pub circuit_breaker_trips: AtomicU64,
20    /// Total call duration (microseconds)
21    pub duration_total_us: AtomicU64,
22    /// Decisions by type
23    pub decisions_allow: AtomicU64,
24    pub decisions_block: AtomicU64,
25    pub decisions_redirect: AtomicU64,
26    pub decisions_challenge: AtomicU64,
27}
28
29impl AgentMetrics {
30    /// Create a new metrics instance.
31    pub fn new() -> Self {
32        Self::default()
33    }
34
35    /// Record a successful call with duration.
36    pub fn record_success(&self, duration_us: u64) {
37        self.calls_total.fetch_add(1, Ordering::Relaxed);
38        self.calls_success.fetch_add(1, Ordering::Relaxed);
39        self.duration_total_us
40            .fetch_add(duration_us, Ordering::Relaxed);
41    }
42
43    /// Record a failed call.
44    pub fn record_failure(&self) {
45        self.calls_total.fetch_add(1, Ordering::Relaxed);
46        self.calls_failed.fetch_add(1, Ordering::Relaxed);
47    }
48
49    /// Record a timeout.
50    pub fn record_timeout(&self) {
51        self.calls_total.fetch_add(1, Ordering::Relaxed);
52        self.calls_timeout.fetch_add(1, Ordering::Relaxed);
53    }
54
55    /// Get average call duration in microseconds.
56    pub fn average_duration_us(&self) -> f64 {
57        let total = self.duration_total_us.load(Ordering::Relaxed) as f64;
58        let success = self.calls_success.load(Ordering::Relaxed) as f64;
59        if success > 0.0 {
60            total / success
61        } else {
62            0.0
63        }
64    }
65}