Skip to main content

camel_api/
health.rs

1//! Health monitoring types for rust-camel.
2//!
3//! This module provides types for tracking and reporting the health status
4//! of services in a rust-camel application.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10use crate::lifecycle::{HealthStatus, ServiceStatus};
11
12/// System-wide health report containing aggregated status of all services.
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct HealthReport {
15    pub status: HealthStatus,
16    pub services: Vec<ServiceHealth>,
17    pub timestamp: DateTime<Utc>,
18}
19
20impl Default for HealthReport {
21    fn default() -> Self {
22        Self {
23            status: HealthStatus::Healthy,
24            services: Vec::new(),
25            timestamp: Utc::now(),
26        }
27    }
28}
29
30/// Health status of an individual service.
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct ServiceHealth {
33    pub name: String,
34    pub status: ServiceStatus,
35}
36
37pub type HealthChecker = Arc<dyn Fn() -> HealthReport + Send + Sync>;
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn test_health_report_serialization() {
45        let report = HealthReport {
46            status: HealthStatus::Healthy,
47            services: vec![ServiceHealth {
48                name: "prometheus".to_string(),
49                status: ServiceStatus::Started,
50            }],
51            timestamp: chrono::Utc::now(),
52        };
53
54        let json = serde_json::to_string(&report).unwrap();
55        assert!(json.contains("Healthy"));
56        assert!(json.contains("prometheus"));
57        assert!(json.contains("Started"));
58        assert!(json.contains("timestamp"));
59    }
60
61    #[test]
62    fn test_health_report_default() {
63        let report = HealthReport::default();
64        assert_eq!(report.status, HealthStatus::Healthy);
65        assert!(report.services.is_empty());
66        assert!(report.timestamp <= chrono::Utc::now());
67    }
68}