use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Instant;
pub static AUTH_TOTAL_REQUESTS: AtomicU64 = AtomicU64::new(0);
pub static AUTH_SUCCESSFUL_REQUESTS: AtomicU64 = AtomicU64::new(0);
pub static AUTH_FAILED_REQUESTS: AtomicU64 = AtomicU64::new(0);
pub static SESSION_ACTIVE_COUNT: AtomicU64 = AtomicU64::new(0);
pub static SESSION_EXPIRED_COUNT: AtomicU64 = AtomicU64::new(0);
pub static SESSION_CREATED_TOTAL: AtomicU64 = AtomicU64::new(0);
pub static TOKEN_CREATION_COUNT: AtomicU64 = AtomicU64::new(0);
pub static TOKEN_VALIDATION_COUNT: AtomicU64 = AtomicU64::new(0);
pub static TOKEN_EXPIRATION_COUNT: AtomicU64 = AtomicU64::new(0);
pub struct AuthMetricsCollector;
pub struct SessionMetricsCollector {
last_created_snapshot: AtomicU64,
last_snapshot_time: std::sync::Mutex<Instant>,
}
pub struct TokenMetricsCollector;
impl AuthMetricsCollector {
pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
let mut metrics = std::collections::HashMap::new();
metrics.insert(
"auth_total_requests".to_string(),
AUTH_TOTAL_REQUESTS.load(Ordering::Relaxed) as f64,
);
metrics.insert(
"auth_successful_requests".to_string(),
AUTH_SUCCESSFUL_REQUESTS.load(Ordering::Relaxed) as f64,
);
metrics.insert(
"auth_failed_requests".to_string(),
AUTH_FAILED_REQUESTS.load(Ordering::Relaxed) as f64,
);
metrics
}
}
impl Default for SessionMetricsCollector {
fn default() -> Self {
Self {
last_created_snapshot: AtomicU64::new(0),
last_snapshot_time: std::sync::Mutex::new(Instant::now()),
}
}
}
impl SessionMetricsCollector {
pub fn new() -> Self {
Self::default()
}
pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
let mut metrics = std::collections::HashMap::new();
metrics.insert(
"session_active_count".to_string(),
SESSION_ACTIVE_COUNT.load(Ordering::Relaxed) as f64,
);
metrics.insert(
"session_expired_count".to_string(),
SESSION_EXPIRED_COUNT.load(Ordering::Relaxed) as f64,
);
let current_total = SESSION_CREATED_TOTAL.load(Ordering::Relaxed);
let previous = self
.last_created_snapshot
.swap(current_total, Ordering::Relaxed);
let delta = current_total.saturating_sub(previous);
let rate = {
let mut last_time = match self.last_snapshot_time.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
let elapsed = last_time.elapsed().as_secs_f64();
*last_time = Instant::now();
if elapsed > 0.0 {
delta as f64 / elapsed
} else {
0.0
}
};
metrics.insert("session_creation_rate".to_string(), rate);
metrics
}
}
impl TokenMetricsCollector {
pub async fn collect(&self) -> std::collections::HashMap<String, f64> {
let mut metrics = std::collections::HashMap::new();
metrics.insert(
"token_creation_count".to_string(),
TOKEN_CREATION_COUNT.load(Ordering::Relaxed) as f64,
);
metrics.insert(
"token_validation_count".to_string(),
TOKEN_VALIDATION_COUNT.load(Ordering::Relaxed) as f64,
);
metrics.insert(
"token_expiration_count".to_string(),
TOKEN_EXPIRATION_COUNT.load(Ordering::Relaxed) as f64,
);
metrics
}
}