auth_framework/monitoring/
health.rs1use super::{HealthCheckResult, HealthStatus};
4use serde::{Deserialize, Serialize};
5use std::time::{SystemTime, UNIX_EPOCH};
6use tracing::warn;
7
8pub struct HealthChecker;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct HealthCheckConfig {
14 pub enabled: bool,
16 pub timeout_seconds: u64,
18 pub check_interval_seconds: u64,
20}
21
22impl Default for HealthCheckConfig {
23 fn default() -> Self {
24 Self {
25 enabled: true,
26 timeout_seconds: 30,
27 check_interval_seconds: 60,
28 }
29 }
30}
31
32impl Default for HealthChecker {
33 fn default() -> Self {
34 Self::new()
35 }
36}
37
38impl HealthChecker {
39 pub fn new() -> Self {
41 Self
42 }
43
44 pub async fn check_all_components(
46 &self,
47 ) -> std::collections::HashMap<String, HealthCheckResult> {
48 let mut results = std::collections::HashMap::new();
50
51 results.insert("authentication".to_string(), self.check_auth_system().await);
53
54 results.insert("sessions".to_string(), self.check_session_system().await);
56
57 results.insert("tokens".to_string(), self.check_token_system().await);
59
60 results.insert("storage".to_string(), self.check_storage_system().await);
62
63 results.insert("mfa".to_string(), self.check_mfa_system().await);
65
66 results
67 }
68
69 async fn check_auth_system(&self) -> HealthCheckResult {
71 let start_time = SystemTime::now();
72
73 let status = match self.test_auth_system().await {
75 Ok(()) => HealthStatus::Healthy,
76 Err(e) => {
77 warn!("Authentication system health check failed: {}", e);
78 HealthStatus::Critical
79 }
80 };
81
82 let message = match status {
83 HealthStatus::Healthy => "Authentication system operational".to_string(),
84 HealthStatus::Critical => "Authentication system has critical issues".to_string(),
85 _ => "Authentication system status unknown".to_string(),
86 };
87
88 HealthCheckResult {
89 component: "authentication".to_string(),
90 status,
91 message,
92 timestamp: current_timestamp(),
93 response_time: start_time.elapsed().unwrap_or_default().as_millis() as u64,
94 }
95 }
96
97 async fn test_auth_system(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
99 tokio::time::sleep(std::time::Duration::from_millis(5)).await;
119 Ok(())
120 }
121
122 async fn check_session_system(&self) -> HealthCheckResult {
124 let start_time = SystemTime::now();
125
126 let status = HealthStatus::Healthy;
127 let message = "Session management operational".to_string();
128
129 HealthCheckResult {
130 component: "sessions".to_string(),
131 status,
132 message,
133 timestamp: current_timestamp(),
134 response_time: start_time.elapsed().unwrap_or_default().as_millis() as u64,
135 }
136 }
137
138 async fn check_token_system(&self) -> HealthCheckResult {
140 let start_time = SystemTime::now();
141
142 let status = HealthStatus::Healthy;
143 let message = "Token management operational".to_string();
144
145 HealthCheckResult {
146 component: "tokens".to_string(),
147 status,
148 message,
149 timestamp: current_timestamp(),
150 response_time: start_time.elapsed().unwrap_or_default().as_millis() as u64,
151 }
152 }
153
154 async fn check_storage_system(&self) -> HealthCheckResult {
156 let start_time = SystemTime::now();
157
158 let status = HealthStatus::Healthy;
159 let message = "Storage system operational".to_string();
160
161 HealthCheckResult {
162 component: "storage".to_string(),
163 status,
164 message,
165 timestamp: current_timestamp(),
166 response_time: start_time.elapsed().unwrap_or_default().as_millis() as u64,
167 }
168 }
169
170 async fn check_mfa_system(&self) -> HealthCheckResult {
172 let start_time = SystemTime::now();
173
174 let status = HealthStatus::Healthy;
175 let message = "MFA system operational".to_string();
176
177 HealthCheckResult {
178 component: "mfa".to_string(),
179 status,
180 message,
181 timestamp: current_timestamp(),
182 response_time: start_time.elapsed().unwrap_or_default().as_millis() as u64,
183 }
184 }
185}
186
187fn current_timestamp() -> u64 {
189 SystemTime::now()
190 .duration_since(UNIX_EPOCH)
191 .unwrap_or_default()
192 .as_secs()
193}