|
| 1 | +//! Monitoring and health check endpoints for projections |
| 2 | +//! |
| 3 | +//! This module provides HTTP-friendly monitoring data structures |
| 4 | +//! that can be easily serialized for health check endpoints. |
| 5 | +
|
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use std::collections::HashMap; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +use super::runner::{HealthStatus, ProjectionHealth}; |
| 11 | + |
| 12 | +/// Health check response for projection system |
| 13 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 14 | +pub struct ProjectionSystemHealth { |
| 15 | + /// Overall system status |
| 16 | + pub status: SystemStatus, |
| 17 | + /// Individual projection health |
| 18 | + pub projections: Vec<ProjectionHealthDto>, |
| 19 | + /// Summary statistics |
| 20 | + pub summary: HealthSummary, |
| 21 | + /// Timestamp of the health check |
| 22 | + pub checked_at: chrono::DateTime<chrono::Utc>, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 26 | +#[serde(rename_all = "UPPERCASE")] |
| 27 | +pub enum SystemStatus { |
| 28 | + /// All projections healthy |
| 29 | + Healthy, |
| 30 | + /// One or more projections lagging but operational |
| 31 | + Degraded, |
| 32 | + /// One or more projections failed |
| 33 | + Unhealthy, |
| 34 | +} |
| 35 | + |
| 36 | +/// DTO for projection health suitable for JSON serialization |
| 37 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 38 | +pub struct ProjectionHealthDto { |
| 39 | + pub name: String, |
| 40 | + pub status: String, |
| 41 | + pub last_checkpoint: Option<String>, |
| 42 | + pub events_processed: u64, |
| 43 | + pub last_error: Option<String>, |
| 44 | + pub lag_seconds: Option<u64>, |
| 45 | +} |
| 46 | + |
| 47 | +/// Summary of projection system health |
| 48 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 49 | +pub struct HealthSummary { |
| 50 | + pub total_projections: usize, |
| 51 | + pub healthy_projections: usize, |
| 52 | + pub lagging_projections: usize, |
| 53 | + pub failed_projections: usize, |
| 54 | + pub total_events_processed: u64, |
| 55 | +} |
| 56 | + |
| 57 | +impl From<Vec<ProjectionHealth>> for ProjectionSystemHealth { |
| 58 | + fn from(projections: Vec<ProjectionHealth>) -> Self { |
| 59 | + let mut healthy_count = 0; |
| 60 | + let mut lagging_count = 0; |
| 61 | + let mut failed_count = 0; |
| 62 | + let mut total_events = 0; |
| 63 | + |
| 64 | + let projection_dtos: Vec<ProjectionHealthDto> = projections |
| 65 | + .iter() |
| 66 | + .map(|p| { |
| 67 | + match p.status { |
| 68 | + HealthStatus::Healthy => healthy_count += 1, |
| 69 | + HealthStatus::Lagging => lagging_count += 1, |
| 70 | + HealthStatus::Failed => failed_count += 1, |
| 71 | + HealthStatus::Rebuilding => {} // Don't count as failed |
| 72 | + } |
| 73 | + |
| 74 | + total_events += p.events_processed; |
| 75 | + |
| 76 | + ProjectionHealthDto { |
| 77 | + name: p.name.clone(), |
| 78 | + status: format!("{:?}", p.status), |
| 79 | + last_checkpoint: p.last_checkpoint.map(|_| { |
| 80 | + // EventCore timestamps don't expose internal representation |
| 81 | + chrono::Utc::now().to_rfc3339() |
| 82 | + }), |
| 83 | + events_processed: p.events_processed, |
| 84 | + last_error: p.last_error.clone(), |
| 85 | + lag_seconds: p.lag.as_ref().map(|d| d.as_secs()), |
| 86 | + } |
| 87 | + }) |
| 88 | + .collect(); |
| 89 | + |
| 90 | + let status = if failed_count > 0 { |
| 91 | + SystemStatus::Unhealthy |
| 92 | + } else if lagging_count > 0 { |
| 93 | + SystemStatus::Degraded |
| 94 | + } else { |
| 95 | + SystemStatus::Healthy |
| 96 | + }; |
| 97 | + |
| 98 | + Self { |
| 99 | + status, |
| 100 | + projections: projection_dtos, |
| 101 | + summary: HealthSummary { |
| 102 | + total_projections: projections.len(), |
| 103 | + healthy_projections: healthy_count, |
| 104 | + lagging_projections: lagging_count, |
| 105 | + failed_projections: failed_count, |
| 106 | + total_events_processed: total_events, |
| 107 | + }, |
| 108 | + checked_at: chrono::Utc::now(), |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Metrics for Prometheus/OpenTelemetry export |
| 114 | +#[derive(Debug, Clone)] |
| 115 | +pub struct ProjectionMetrics { |
| 116 | + /// Counter: Total events processed by projection |
| 117 | + pub events_processed: HashMap<String, u64>, |
| 118 | + /// Gauge: Current lag in seconds by projection |
| 119 | + pub lag_seconds: HashMap<String, f64>, |
| 120 | + /// Gauge: Projection status (1 = healthy, 0 = unhealthy) |
| 121 | + pub health_status: HashMap<String, f64>, |
| 122 | + /// Counter: Total errors by projection |
| 123 | + pub error_count: HashMap<String, u64>, |
| 124 | +} |
| 125 | + |
| 126 | +impl From<Vec<ProjectionHealth>> for ProjectionMetrics { |
| 127 | + fn from(projections: Vec<ProjectionHealth>) -> Self { |
| 128 | + let mut metrics = ProjectionMetrics { |
| 129 | + events_processed: HashMap::new(), |
| 130 | + lag_seconds: HashMap::new(), |
| 131 | + health_status: HashMap::new(), |
| 132 | + error_count: HashMap::new(), |
| 133 | + }; |
| 134 | + |
| 135 | + for projection in projections { |
| 136 | + metrics |
| 137 | + .events_processed |
| 138 | + .insert(projection.name.clone(), projection.events_processed); |
| 139 | + |
| 140 | + if let Some(lag) = projection.lag { |
| 141 | + metrics |
| 142 | + .lag_seconds |
| 143 | + .insert(projection.name.clone(), lag.as_secs_f64()); |
| 144 | + } |
| 145 | + |
| 146 | + let health_value = match projection.status { |
| 147 | + HealthStatus::Healthy => 1.0, |
| 148 | + _ => 0.0, |
| 149 | + }; |
| 150 | + metrics |
| 151 | + .health_status |
| 152 | + .insert(projection.name.clone(), health_value); |
| 153 | + |
| 154 | + if projection.last_error.is_some() { |
| 155 | + // In production, you'd track error counts properly |
| 156 | + metrics.error_count.insert(projection.name.clone(), 1); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + metrics |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Configuration for alerting thresholds |
| 165 | +#[derive(Debug, Clone)] |
| 166 | +pub struct AlertThresholds { |
| 167 | + /// Maximum acceptable lag before alerting |
| 168 | + pub max_lag: Duration, |
| 169 | + /// Maximum consecutive errors before alerting |
| 170 | + pub max_consecutive_errors: u32, |
| 171 | + /// Minimum events per minute (for liveness check) |
| 172 | + pub min_events_per_minute: Option<u64>, |
| 173 | +} |
| 174 | + |
| 175 | +impl Default for AlertThresholds { |
| 176 | + fn default() -> Self { |
| 177 | + Self { |
| 178 | + max_lag: Duration::from_secs(300), // 5 minutes |
| 179 | + max_consecutive_errors: 3, |
| 180 | + min_events_per_minute: None, // Disabled by default |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +/// Check if projections meet alert thresholds |
| 186 | +pub fn check_alerts(health: &[ProjectionHealth], thresholds: &AlertThresholds) -> Vec<Alert> { |
| 187 | + let mut alerts = Vec::new(); |
| 188 | + |
| 189 | + for projection in health { |
| 190 | + // Check lag threshold |
| 191 | + if let Some(lag) = &projection.lag { |
| 192 | + if lag > &thresholds.max_lag { |
| 193 | + alerts.push(Alert { |
| 194 | + projection: projection.name.clone(), |
| 195 | + severity: AlertSeverity::Warning, |
| 196 | + message: format!( |
| 197 | + "Projection lag ({:?}) exceeds threshold ({:?})", |
| 198 | + lag, thresholds.max_lag |
| 199 | + ), |
| 200 | + }); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // Check failure status |
| 205 | + if matches!(projection.status, HealthStatus::Failed) { |
| 206 | + alerts.push(Alert { |
| 207 | + projection: projection.name.clone(), |
| 208 | + severity: AlertSeverity::Critical, |
| 209 | + message: format!( |
| 210 | + "Projection failed: {}", |
| 211 | + projection |
| 212 | + .last_error |
| 213 | + .as_ref() |
| 214 | + .unwrap_or(&"Unknown error".to_string()) |
| 215 | + ), |
| 216 | + }); |
| 217 | + } |
| 218 | + |
| 219 | + // TODO: Implement events per minute check when we have time-series data |
| 220 | + } |
| 221 | + |
| 222 | + alerts |
| 223 | +} |
| 224 | + |
| 225 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 226 | +pub struct Alert { |
| 227 | + pub projection: String, |
| 228 | + pub severity: AlertSeverity, |
| 229 | + pub message: String, |
| 230 | +} |
| 231 | + |
| 232 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 233 | +#[serde(rename_all = "UPPERCASE")] |
| 234 | +pub enum AlertSeverity { |
| 235 | + Info, |
| 236 | + Warning, |
| 237 | + Critical, |
| 238 | +} |
| 239 | + |
| 240 | +#[cfg(test)] |
| 241 | +mod tests { |
| 242 | + use super::*; |
| 243 | + use crate::infrastructure::eventcore::projections::runner::HealthStatus; |
| 244 | + |
| 245 | + #[test] |
| 246 | + fn test_system_health_conversion() { |
| 247 | + let projections = vec![ |
| 248 | + ProjectionHealth { |
| 249 | + name: "test1".to_string(), |
| 250 | + status: HealthStatus::Healthy, |
| 251 | + last_checkpoint: None, |
| 252 | + events_processed: 100, |
| 253 | + last_error: None, |
| 254 | + lag: None, |
| 255 | + }, |
| 256 | + ProjectionHealth { |
| 257 | + name: "test2".to_string(), |
| 258 | + status: HealthStatus::Lagging, |
| 259 | + last_checkpoint: None, |
| 260 | + events_processed: 50, |
| 261 | + last_error: None, |
| 262 | + lag: Some(Duration::from_secs(120)), |
| 263 | + }, |
| 264 | + ]; |
| 265 | + |
| 266 | + let health = ProjectionSystemHealth::from(projections); |
| 267 | + assert_eq!(health.status, SystemStatus::Degraded); |
| 268 | + assert_eq!(health.summary.healthy_projections, 1); |
| 269 | + assert_eq!(health.summary.lagging_projections, 1); |
| 270 | + assert_eq!(health.summary.total_events_processed, 150); |
| 271 | + } |
| 272 | + |
| 273 | + #[test] |
| 274 | + fn test_alert_generation() { |
| 275 | + let projections = vec![ |
| 276 | + ProjectionHealth { |
| 277 | + name: "test1".to_string(), |
| 278 | + status: HealthStatus::Failed, |
| 279 | + last_checkpoint: None, |
| 280 | + events_processed: 0, |
| 281 | + last_error: Some("Connection failed".to_string()), |
| 282 | + lag: None, |
| 283 | + }, |
| 284 | + ProjectionHealth { |
| 285 | + name: "test2".to_string(), |
| 286 | + status: HealthStatus::Lagging, |
| 287 | + last_checkpoint: None, |
| 288 | + events_processed: 50, |
| 289 | + last_error: None, |
| 290 | + lag: Some(Duration::from_secs(400)), |
| 291 | + }, |
| 292 | + ]; |
| 293 | + |
| 294 | + let thresholds = AlertThresholds::default(); |
| 295 | + let alerts = check_alerts(&projections, &thresholds); |
| 296 | + |
| 297 | + assert_eq!(alerts.len(), 2); |
| 298 | + assert_eq!(alerts[0].severity, AlertSeverity::Critical); |
| 299 | + assert_eq!(alerts[1].severity, AlertSeverity::Warning); |
| 300 | + } |
| 301 | +} |
0 commit comments