auth_framework/monitoring/
collectors.rs1use std::sync::atomic::{AtomicU64, Ordering};
7use std::time::Instant;
8
9pub static AUTH_TOTAL_REQUESTS: AtomicU64 = AtomicU64::new(0);
13pub static AUTH_SUCCESSFUL_REQUESTS: AtomicU64 = AtomicU64::new(0);
15pub static AUTH_FAILED_REQUESTS: AtomicU64 = AtomicU64::new(0);
17
18pub static SESSION_ACTIVE_COUNT: AtomicU64 = AtomicU64::new(0);
20pub static SESSION_EXPIRED_COUNT: AtomicU64 = AtomicU64::new(0);
22pub static SESSION_CREATED_TOTAL: AtomicU64 = AtomicU64::new(0);
24
25pub static TOKEN_CREATION_COUNT: AtomicU64 = AtomicU64::new(0);
27pub static TOKEN_VALIDATION_COUNT: AtomicU64 = AtomicU64::new(0);
29pub static TOKEN_EXPIRATION_COUNT: AtomicU64 = AtomicU64::new(0);
31
32pub struct AuthMetricsCollector;
36
37pub struct SessionMetricsCollector {
42 last_created_snapshot: AtomicU64,
43 last_snapshot_time: std::sync::Mutex<Instant>,
44}
45
46pub struct TokenMetricsCollector;
48
49impl AuthMetricsCollector {
50 pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
52 let mut metrics = std::collections::HashMap::new();
53 metrics.insert(
54 "auth_total_requests".to_string(),
55 AUTH_TOTAL_REQUESTS.load(Ordering::Relaxed) as f64,
56 );
57 metrics.insert(
58 "auth_successful_requests".to_string(),
59 AUTH_SUCCESSFUL_REQUESTS.load(Ordering::Relaxed) as f64,
60 );
61 metrics.insert(
62 "auth_failed_requests".to_string(),
63 AUTH_FAILED_REQUESTS.load(Ordering::Relaxed) as f64,
64 );
65 metrics
66 }
67}
68
69impl Default for SessionMetricsCollector {
70 fn default() -> Self {
71 Self {
72 last_created_snapshot: AtomicU64::new(0),
73 last_snapshot_time: std::sync::Mutex::new(Instant::now()),
74 }
75 }
76}
77
78impl SessionMetricsCollector {
79 pub fn new() -> Self {
81 Self::default()
82 }
83
84 pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
90 let mut metrics = std::collections::HashMap::new();
91 metrics.insert(
92 "session_active_count".to_string(),
93 SESSION_ACTIVE_COUNT.load(Ordering::Relaxed) as f64,
94 );
95 metrics.insert(
96 "session_expired_count".to_string(),
97 SESSION_EXPIRED_COUNT.load(Ordering::Relaxed) as f64,
98 );
99
100 let current_total = SESSION_CREATED_TOTAL.load(Ordering::Relaxed);
102 let previous = self
103 .last_created_snapshot
104 .swap(current_total, Ordering::Relaxed);
105 let delta = current_total.saturating_sub(previous);
106
107 let rate = {
108 let mut last_time = match self.last_snapshot_time.lock() {
109 Ok(guard) => guard,
110 Err(poisoned) => poisoned.into_inner(),
111 };
112 let elapsed = last_time.elapsed().as_secs_f64();
113 *last_time = Instant::now();
114 if elapsed > 0.0 {
115 delta as f64 / elapsed
116 } else {
117 0.0
118 }
119 };
120 metrics.insert("session_creation_rate".to_string(), rate);
121
122 metrics
123 }
124}
125
126impl TokenMetricsCollector {
127 pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
129 let mut metrics = std::collections::HashMap::new();
130 metrics.insert(
131 "token_creation_count".to_string(),
132 TOKEN_CREATION_COUNT.load(Ordering::Relaxed) as f64,
133 );
134 metrics.insert(
135 "token_validation_count".to_string(),
136 TOKEN_VALIDATION_COUNT.load(Ordering::Relaxed) as f64,
137 );
138 metrics.insert(
139 "token_expiration_count".to_string(),
140 TOKEN_EXPIRATION_COUNT.load(Ordering::Relaxed) as f64,
141 );
142 metrics
143 }
144}