Skip to main content

auth_framework/deployment/
monitoring.rs

1// Monitoring integration for production deployment
2// Comprehensive monitoring with Prometheus, Grafana, and custom metrics
3
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6use std::time::{Duration, SystemTime, UNIX_EPOCH};
7use thiserror::Error;
8use zeroize::Zeroizing;
9
10#[derive(Debug, Error)]
11pub enum MonitoringError {
12    #[error("Metric collection error: {0}")]
13    MetricCollection(String),
14    #[error("Exporter error: {0}")]
15    Exporter(String),
16    #[error("Alert manager error: {0}")]
17    AlertManager(String),
18    #[error("Dashboard error: {0}")]
19    Dashboard(String),
20    #[error("Configuration error: {0}")]
21    Configuration(String),
22    #[error("Network error: {0}")]
23    Network(String),
24    #[error("IO error: {0}")]
25    Io(#[from] std::io::Error),
26    #[error("Serialization error: {0}")]
27    Serialization(#[from] serde_json::Error),
28}
29
30/// Metric types supported by the monitoring system
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub enum MetricType {
33    Counter,
34    Gauge,
35    Histogram,
36    Summary,
37}
38
39/// Metric value with timestamp
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct MetricValue {
42    pub value: f64,
43    pub timestamp: u64,
44    pub labels: HashMap<String, String>,
45}
46
47/// Metric definition
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct Metric {
50    pub name: String,
51    pub metric_type: MetricType,
52    pub description: String,
53    pub unit: String,
54    pub values: Vec<MetricValue>,
55    pub retention: Duration,
56}
57
58/// Alert rule configuration
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct AlertRule {
61    pub name: String,
62    pub metric_name: String,
63    pub condition: AlertCondition,
64    pub threshold: f64,
65    pub duration: Duration,
66    pub severity: AlertSeverity,
67    pub enabled: bool,
68    pub labels: HashMap<String, String>,
69    pub annotations: HashMap<String, String>,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub enum AlertCondition {
74    GreaterThan,
75    LessThan,
76    Equal,
77    NotEqual,
78    GreaterThanOrEqual,
79    LessThanOrEqual,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub enum AlertSeverity {
84    Critical,
85    Warning,
86    Info,
87}
88
89/// Alert instance
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct Alert {
92    pub rule_name: String,
93    pub metric_name: String,
94    pub current_value: f64,
95    pub threshold: f64,
96    pub severity: AlertSeverity,
97    pub message: String,
98    pub started_at: u64,
99    pub resolved_at: Option<u64>,
100    pub labels: HashMap<String, String>,
101}
102
103/// Dashboard configuration
104#[derive(Debug, Clone, Serialize, Deserialize)]
105pub struct Dashboard {
106    pub id: String,
107    pub title: String,
108    pub description: String,
109    pub panels: Vec<DashboardPanel>,
110    pub refresh_interval: Duration,
111    pub time_range: TimeRange,
112    pub variables: HashMap<String, String>,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct DashboardPanel {
117    pub id: String,
118    pub title: String,
119    pub panel_type: PanelType,
120    pub metric_queries: Vec<String>,
121    pub position: PanelPosition,
122    pub options: HashMap<String, serde_json::Value>,
123}
124
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub enum PanelType {
127    LineGraph,
128    BarChart,
129    Gauge,
130    SingleStat,
131    Table,
132    HeatMap,
133    Alert,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct PanelPosition {
138    pub x: u32,
139    pub y: u32,
140    pub width: u32,
141    pub height: u32,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct TimeRange {
146    pub from: String,
147    pub to: String,
148}
149
150/// Monitoring configuration
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct MonitoringConfig {
153    pub enabled: bool,
154    pub prometheus: PrometheusConfig,
155    pub grafana: GrafanaConfig,
156    pub alertmanager: AlertManagerConfig,
157    pub custom_exporters: Vec<ExporterConfig>,
158    pub dashboards: Vec<Dashboard>,
159    pub retention_policy: RetentionPolicy,
160}
161
162#[derive(Debug, Clone, Serialize, Deserialize)]
163pub struct PrometheusConfig {
164    pub endpoint: String,
165    pub scrape_interval: Duration,
166    pub evaluation_interval: Duration,
167    pub external_labels: HashMap<String, String>,
168    pub rule_files: Vec<String>,
169}
170
171#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct GrafanaConfig {
173    pub endpoint: String,
174    pub api_key: Zeroizing<String>,
175    pub organization: String,
176    pub datasource: String,
177    pub auto_provision: bool,
178}
179
180#[derive(Debug, Clone, Serialize, Deserialize)]
181pub struct AlertManagerConfig {
182    pub endpoint: String,
183    pub receivers: Vec<AlertReceiver>,
184    pub routing: AlertRouting,
185    pub inhibit_rules: Vec<InhibitRule>,
186}
187
188#[derive(Debug, Clone, Serialize, Deserialize)]
189pub struct AlertReceiver {
190    pub name: String,
191    pub webhook_configs: Vec<WebhookConfig>,
192    pub email_configs: Vec<EmailConfig>,
193    pub slack_configs: Vec<SlackConfig>,
194}
195
196#[derive(Clone, Serialize, Deserialize)]
197pub struct WebhookConfig {
198    pub url: String,
199    pub http_config: Option<HttpConfig>,
200}
201
202impl std::fmt::Debug for WebhookConfig {
203    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204        let mut d = f.debug_struct("WebhookConfig");
205        d.field("url", &crate::utils::redact::redacted(&self.url));
206        d.field("http_config", &self.http_config);
207        d.finish()
208    }
209}
210
211#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct EmailConfig {
213    pub to: Vec<String>,
214    pub from: String,
215    pub subject: String,
216    pub body: String,
217}
218
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct SlackConfig {
221    pub api_url: String,
222    pub channel: String,
223    pub username: String,
224    pub title: String,
225    pub text: String,
226}
227
228#[derive(Clone, Serialize, Deserialize)]
229pub struct HttpConfig {
230    pub basic_auth: Option<BasicAuth>,
231    pub bearer_token: Option<String>,
232    pub tls_config: Option<TlsConfig>,
233}
234
235impl std::fmt::Debug for HttpConfig {
236    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
237        let mut d = f.debug_struct("HttpConfig");
238        d.field("basic_auth", &self.basic_auth);
239        d.field(
240            "bearer_token",
241            &crate::utils::redact::redacted(&self.bearer_token),
242        );
243        d.field("tls_config", &self.tls_config);
244        d.finish()
245    }
246}
247
248#[derive(Clone, Serialize, Deserialize)]
249pub struct BasicAuth {
250    pub username: String,
251    pub password: String,
252}
253
254impl std::fmt::Debug for BasicAuth {
255    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
256        let mut d = f.debug_struct("BasicAuth");
257        d.field("username", &self.username);
258        d.field("password", &crate::utils::redact::redacted(&self.password));
259        d.finish()
260    }
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize)]
264pub struct TlsConfig {
265    pub ca_file: Option<String>,
266    pub cert_file: Option<String>,
267    pub key_file: Option<String>,
268    pub insecure_skip_verify: bool,
269}
270
271#[derive(Debug, Clone, Serialize, Deserialize)]
272pub struct AlertRouting {
273    pub group_by: Vec<String>,
274    pub group_wait: Duration,
275    pub group_interval: Duration,
276    pub repeat_interval: Duration,
277    pub receiver: String,
278    pub routes: Vec<Route>,
279}
280
281#[derive(Debug, Clone, Serialize, Deserialize)]
282pub struct Route {
283    pub matchers: HashMap<String, String>,
284    pub receiver: String,
285    pub group_by: Vec<String>,
286    pub continue_route: bool,
287}
288
289#[derive(Debug, Clone, Serialize, Deserialize)]
290pub struct InhibitRule {
291    pub source_matchers: HashMap<String, String>,
292    pub target_matchers: HashMap<String, String>,
293    pub equal: Vec<String>,
294}
295
296#[derive(Debug, Clone, Serialize, Deserialize)]
297pub struct ExporterConfig {
298    pub name: String,
299    pub endpoint: String,
300    pub interval: Duration,
301    pub timeout: Duration,
302    pub labels: HashMap<String, String>,
303}
304
305#[derive(Debug, Clone, Serialize, Deserialize)]
306pub struct RetentionPolicy {
307    pub default_retention: Duration,
308    pub metric_retentions: HashMap<String, Duration>,
309    pub downsampling_rules: Vec<DownsamplingRule>,
310}
311
312#[derive(Debug, Clone, Serialize, Deserialize)]
313pub struct DownsamplingRule {
314    pub resolution: Duration,
315    pub retention: Duration,
316    pub aggregation: AggregationType,
317}
318
319#[derive(Debug, Clone, Serialize, Deserialize)]
320pub enum AggregationType {
321    Average,
322    Sum,
323    Min,
324    Max,
325    Count,
326}
327
328/// Monitoring system manager
329pub struct MonitoringSystem {
330    config: MonitoringConfig,
331    metrics: HashMap<String, Metric>,
332    alert_rules: Vec<AlertRule>,
333    active_alerts: Vec<Alert>,
334    dashboards: Vec<Dashboard>,
335}
336
337impl MonitoringSystem {
338    /// Create new monitoring system
339    pub fn new(config: MonitoringConfig) -> Self {
340        Self {
341            config,
342            metrics: HashMap::new(),
343            alert_rules: Vec::new(),
344            active_alerts: Vec::new(),
345            dashboards: Vec::new(),
346        }
347    }
348
349    /// Initialize monitoring system
350    pub async fn initialize(&mut self) -> Result<(), MonitoringError> {
351        if !self.config.enabled {
352            return Ok(());
353        }
354
355        // Initialize Prometheus
356        self.initialize_prometheus().await?;
357
358        // Initialize Grafana
359        self.initialize_grafana().await?;
360
361        // Initialize AlertManager
362        self.initialize_alertmanager().await?;
363
364        // Setup default metrics
365        self.setup_default_metrics().await?;
366
367        // Load dashboards
368        self.load_dashboards().await?;
369
370        Ok(())
371    }
372
373    /// Initialize Prometheus integration
374    async fn initialize_prometheus(&self) -> Result<(), MonitoringError> {
375        // Configure Prometheus scraping
376        tracing::info!(
377            endpoint = %self.config.prometheus.endpoint,
378            "Initializing Prometheus"
379        );
380        Ok(())
381    }
382
383    /// Initialize Grafana integration
384    async fn initialize_grafana(&self) -> Result<(), MonitoringError> {
385        if self.config.grafana.auto_provision {
386            // Auto-provision dashboards
387            self.provision_grafana_dashboards().await?;
388        }
389        Ok(())
390    }
391
392    /// Initialize AlertManager integration
393    async fn initialize_alertmanager(&self) -> Result<(), MonitoringError> {
394        // Configure alert routing
395        tracing::info!(
396            endpoint = %self.config.alertmanager.endpoint,
397            "Initializing AlertManager"
398        );
399        Ok(())
400    }
401
402    /// Setup default RBAC metrics
403    async fn setup_default_metrics(&mut self) -> Result<(), MonitoringError> {
404        // Authentication metrics
405        self.register_metric(Metric {
406            name: "auth_requests_total".to_string(),
407            metric_type: MetricType::Counter,
408            description: "Total number of authentication requests".to_string(),
409            unit: "requests".to_string(),
410            values: Vec::new(),
411            retention: Duration::from_secs(30 * 24 * 3600), // 30 days
412        });
413
414        self.register_metric(Metric {
415            name: "auth_success_total".to_string(),
416            metric_type: MetricType::Counter,
417            description: "Total number of successful authentications".to_string(),
418            unit: "requests".to_string(),
419            values: Vec::new(),
420            retention: Duration::from_secs(30 * 24 * 3600),
421        });
422
423        self.register_metric(Metric {
424            name: "auth_failures_total".to_string(),
425            metric_type: MetricType::Counter,
426            description: "Total number of failed authentications".to_string(),
427            unit: "requests".to_string(),
428            values: Vec::new(),
429            retention: Duration::from_secs(30 * 24 * 3600),
430        });
431
432        // Authorization metrics
433        self.register_metric(Metric {
434            name: "authz_checks_total".to_string(),
435            metric_type: MetricType::Counter,
436            description: "Total number of authorization checks".to_string(),
437            unit: "checks".to_string(),
438            values: Vec::new(),
439            retention: Duration::from_secs(30 * 24 * 3600),
440        });
441
442        self.register_metric(Metric {
443            name: "authz_denied_total".to_string(),
444            metric_type: MetricType::Counter,
445            description: "Total number of denied authorization checks".to_string(),
446            unit: "checks".to_string(),
447            values: Vec::new(),
448            retention: Duration::from_secs(30 * 24 * 3600),
449        });
450
451        // Performance metrics
452        self.register_metric(Metric {
453            name: "request_duration_seconds".to_string(),
454            metric_type: MetricType::Histogram,
455            description: "Request duration in seconds".to_string(),
456            unit: "seconds".to_string(),
457            values: Vec::new(),
458            retention: Duration::from_secs(7 * 24 * 3600), // 7 days
459        });
460
461        self.register_metric(Metric {
462            name: "active_sessions".to_string(),
463            metric_type: MetricType::Gauge,
464            description: "Number of active user sessions".to_string(),
465            unit: "sessions".to_string(),
466            values: Vec::new(),
467            retention: Duration::from_secs(24 * 3600), // 1 day
468        });
469
470        Ok(())
471    }
472
473    /// Register a new metric
474    pub fn register_metric(&mut self, metric: Metric) {
475        self.metrics.insert(metric.name.clone(), metric);
476    }
477
478    /// Record metric value
479    pub fn record_metric(
480        &mut self,
481        name: &str,
482        value: f64,
483        labels: HashMap<String, String>,
484    ) -> Result<(), MonitoringError> {
485        if let Some(metric) = self.metrics.get_mut(name) {
486            let now = SystemTime::now()
487                .duration_since(UNIX_EPOCH)
488                .unwrap_or_default();
489
490            let metric_value = MetricValue {
491                value,
492                timestamp: now.as_secs(),
493                labels,
494            };
495
496            metric.values.push(metric_value);
497
498            // Clean up old values based on retention policy
499            let cutoff = now.as_secs() - metric.retention.as_secs();
500            metric.values.retain(|v| v.timestamp > cutoff);
501
502            Ok(())
503        } else {
504            Err(MonitoringError::MetricCollection(format!(
505                "Metric not found: {}",
506                name
507            )))
508        }
509    }
510
511    /// Add alert rule
512    pub fn add_alert_rule(&mut self, rule: AlertRule) {
513        self.alert_rules.push(rule);
514    }
515
516    /// Evaluate alert rules
517    pub async fn evaluate_alerts(&mut self) -> Result<(), MonitoringError> {
518        let now = SystemTime::now()
519            .duration_since(UNIX_EPOCH)
520            .unwrap_or_default();
521
522        for rule in &self.alert_rules {
523            if !rule.enabled {
524                continue;
525            }
526
527            if let Some(metric) = self.metrics.get(&rule.metric_name)
528                && let Some(latest_value) = metric.values.last()
529            {
530                let should_alert = match rule.condition {
531                    AlertCondition::GreaterThan => latest_value.value > rule.threshold,
532                    AlertCondition::LessThan => latest_value.value < rule.threshold,
533                    AlertCondition::Equal => {
534                        (latest_value.value - rule.threshold).abs() < f64::EPSILON
535                    }
536                    AlertCondition::NotEqual => {
537                        (latest_value.value - rule.threshold).abs() > f64::EPSILON
538                    }
539                    AlertCondition::GreaterThanOrEqual => latest_value.value >= rule.threshold,
540                    AlertCondition::LessThanOrEqual => latest_value.value <= rule.threshold,
541                };
542
543                if should_alert {
544                    // Check if alert already exists
545                    let existing_alert = self
546                        .active_alerts
547                        .iter()
548                        .find(|alert| alert.rule_name == rule.name && alert.resolved_at.is_none());
549
550                    if existing_alert.is_none() {
551                        let alert = Alert {
552                            rule_name: rule.name.clone(),
553                            metric_name: rule.metric_name.clone(),
554                            current_value: latest_value.value,
555                            threshold: rule.threshold,
556                            severity: rule.severity.clone(),
557                            message: format!(
558                                "Alert: {} - Current value: {}, Threshold: {}",
559                                rule.name, latest_value.value, rule.threshold
560                            ),
561                            started_at: now.as_secs(),
562                            resolved_at: None,
563                            labels: rule.labels.clone(),
564                        };
565
566                        self.active_alerts.push(alert.clone());
567                        self.send_alert(&alert).await?;
568                    }
569                } else {
570                    // Resolve existing alerts
571                    let mut alerts_to_resolve = Vec::new();
572                    for alert in &mut self.active_alerts {
573                        if alert.rule_name == rule.name && alert.resolved_at.is_none() {
574                            alert.resolved_at = Some(now.as_secs());
575                            alerts_to_resolve.push(alert.clone());
576                        }
577                    }
578
579                    // Send resolution notifications
580                    for alert in &alerts_to_resolve {
581                        self.send_alert_resolution(alert).await?;
582                    }
583                }
584            }
585        }
586        Ok(())
587    }
588
589    /// Send alert notification
590    async fn send_alert(&self, alert: &Alert) -> Result<(), MonitoringError> {
591        // Send to AlertManager
592        tracing::warn!(rule = %alert.rule_name, message = %alert.message, "ALERT triggered");
593
594        // Send to configured receivers
595        for receiver in &self.config.alertmanager.receivers {
596            self.send_to_receiver(receiver, alert).await?;
597        }
598
599        Ok(())
600    }
601
602    /// Send alert resolution notification
603    async fn send_alert_resolution(&self, alert: &Alert) -> Result<(), MonitoringError> {
604        tracing::info!(rule = %alert.rule_name, message = %alert.message, "ALERT resolved");
605        Ok(())
606    }
607
608    /// Send alert to specific receiver
609    async fn send_to_receiver(
610        &self,
611        receiver: &AlertReceiver,
612        alert: &Alert,
613    ) -> Result<(), MonitoringError> {
614        // Send to webhook endpoints
615        for webhook in &receiver.webhook_configs {
616            self.send_webhook_alert(webhook, alert).await?;
617        }
618
619        // Send to email endpoints
620        for email in &receiver.email_configs {
621            self.send_email_alert(email, alert).await?;
622        }
623
624        // Send to Slack endpoints
625        for slack in &receiver.slack_configs {
626            self.send_slack_alert(slack, alert).await?;
627        }
628
629        Ok(())
630    }
631
632    /// Send webhook alert
633    async fn send_webhook_alert(
634        &self,
635        _webhook: &WebhookConfig,
636        _alert: &Alert,
637    ) -> Result<(), MonitoringError> {
638        // Implement webhook sending
639        Ok(())
640    }
641
642    /// Send email alert
643    async fn send_email_alert(
644        &self,
645        _email: &EmailConfig,
646        _alert: &Alert,
647    ) -> Result<(), MonitoringError> {
648        // Implement email sending
649        Ok(())
650    }
651
652    /// Send Slack alert
653    async fn send_slack_alert(
654        &self,
655        _slack: &SlackConfig,
656        _alert: &Alert,
657    ) -> Result<(), MonitoringError> {
658        // Implement Slack sending
659        Ok(())
660    }
661
662    /// Load dashboards configuration
663    async fn load_dashboards(&mut self) -> Result<(), MonitoringError> {
664        self.dashboards = self.config.dashboards.clone();
665        Ok(())
666    }
667
668    /// Provision Grafana dashboards
669    async fn provision_grafana_dashboards(&self) -> Result<(), MonitoringError> {
670        for dashboard in &self.config.dashboards {
671            tracing::info!(title = %dashboard.title, "Provisioning Grafana dashboard");
672        }
673        Ok(())
674    }
675
676    /// Export metrics in Prometheus format
677    pub fn export_prometheus_metrics(&self) -> String {
678        let mut output = String::new();
679
680        for (name, metric) in &self.metrics {
681            // Add metric metadata
682            output.push_str(&format!("# HELP {} {}\n", name, metric.description));
683            output.push_str(&format!(
684                "# TYPE {} {}\n",
685                name,
686                match metric.metric_type {
687                    MetricType::Counter => "counter",
688                    MetricType::Gauge => "gauge",
689                    MetricType::Histogram => "histogram",
690                    MetricType::Summary => "summary",
691                }
692            ));
693
694            // Add metric values
695            for value in &metric.values {
696                let labels = if value.labels.is_empty() {
697                    String::new()
698                } else {
699                    let label_pairs: Vec<String> = value
700                        .labels
701                        .iter()
702                        .map(|(k, v)| format!("{}=\"{}\"", k, v))
703                        .collect();
704                    format!("{{{}}}", label_pairs.join(","))
705                };
706
707                output.push_str(&format!(
708                    "{}{} {} {}\n",
709                    name,
710                    labels,
711                    value.value,
712                    value.timestamp * 1000
713                ));
714            }
715        }
716
717        output
718    }
719
720    /// Get current metrics
721    pub fn get_metrics(&self) -> &HashMap<String, Metric> {
722        &self.metrics
723    }
724
725    /// Get active alerts
726    pub fn get_active_alerts(&self) -> &Vec<Alert> {
727        &self.active_alerts
728    }
729
730    /// Get dashboards
731    pub fn get_dashboards(&self) -> &Vec<Dashboard> {
732        &self.dashboards
733    }
734}
735
736impl Default for MonitoringConfig {
737    fn default() -> Self {
738        Self {
739            enabled: true,
740            prometheus: PrometheusConfig {
741                endpoint: "http://localhost:9090".to_string(),
742                scrape_interval: Duration::from_secs(15),
743                evaluation_interval: Duration::from_secs(15),
744                external_labels: HashMap::new(),
745                rule_files: vec!["alerts.yml".to_string()],
746            },
747            grafana: GrafanaConfig {
748                endpoint: "http://localhost:3000".to_string(),
749                api_key: "".to_string().into(),
750                organization: "Main Org.".to_string(),
751                datasource: "Prometheus".to_string(),
752                auto_provision: true,
753            },
754            alertmanager: AlertManagerConfig {
755                endpoint: "http://localhost:9093".to_string(),
756                receivers: vec![AlertReceiver {
757                    name: "default".to_string(),
758                    webhook_configs: Vec::new(),
759                    email_configs: Vec::new(),
760                    slack_configs: Vec::new(),
761                }],
762                routing: AlertRouting {
763                    group_by: vec!["alertname".to_string()],
764                    group_wait: Duration::from_secs(10),
765                    group_interval: Duration::from_secs(10),
766                    repeat_interval: Duration::from_secs(3600),
767                    receiver: "default".to_string(),
768                    routes: Vec::new(),
769                },
770                inhibit_rules: Vec::new(),
771            },
772            custom_exporters: Vec::new(),
773            dashboards: Vec::new(),
774            retention_policy: RetentionPolicy {
775                default_retention: Duration::from_secs(30 * 24 * 3600), // 30 days
776                metric_retentions: HashMap::new(),
777                downsampling_rules: Vec::new(),
778            },
779        }
780    }
781}
782
783#[cfg(test)]
784mod tests {
785    use super::*;
786
787    #[test]
788    fn test_monitoring_system_creation() {
789        let config = MonitoringConfig::default();
790        let system = MonitoringSystem::new(config);
791
792        assert!(system.metrics.is_empty());
793        assert!(system.alert_rules.is_empty());
794        assert!(system.active_alerts.is_empty());
795    }
796
797    #[tokio::test]
798    async fn test_metric_registration() {
799        let config = MonitoringConfig::default();
800        let mut system = MonitoringSystem::new(config);
801
802        let metric = Metric {
803            name: "test_metric".to_string(),
804            metric_type: MetricType::Counter,
805            description: "Test metric".to_string(),
806            unit: "count".to_string(),
807            values: Vec::new(),
808            retention: Duration::from_secs(3600),
809        };
810
811        system.register_metric(metric);
812        assert!(system.metrics.contains_key("test_metric"));
813    }
814
815    #[tokio::test]
816    async fn test_metric_recording() {
817        let config = MonitoringConfig::default();
818        let mut system = MonitoringSystem::new(config);
819
820        let metric = Metric {
821            name: "test_counter".to_string(),
822            metric_type: MetricType::Counter,
823            description: "Test counter".to_string(),
824            unit: "count".to_string(),
825            values: Vec::new(),
826            retention: Duration::from_secs(3600),
827        };
828
829        system.register_metric(metric);
830
831        let mut labels = HashMap::new();
832        labels.insert("service".to_string(), "auth".to_string());
833
834        let result = system.record_metric("test_counter", 1.0, labels);
835        assert!(result.is_ok());
836
837        let metric = system.metrics.get("test_counter").unwrap();
838        assert_eq!(metric.values.len(), 1);
839        assert_eq!(metric.values[0].value, 1.0);
840    }
841
842    #[test]
843    fn test_prometheus_export() {
844        let config = MonitoringConfig::default();
845        let mut system = MonitoringSystem::new(config);
846
847        let metric = Metric {
848            name: "test_gauge".to_string(),
849            metric_type: MetricType::Gauge,
850            description: "Test gauge".to_string(),
851            unit: "value".to_string(),
852            values: vec![MetricValue {
853                value: 42.0,
854                timestamp: 1640995200,
855                labels: HashMap::new(),
856            }],
857            retention: Duration::from_secs(3600),
858        };
859
860        system.register_metric(metric);
861
862        let export = system.export_prometheus_metrics();
863        assert!(export.contains("# HELP test_gauge Test gauge"));
864        assert!(export.contains("# TYPE test_gauge gauge"));
865        assert!(export.contains("test_gauge 42"));
866    }
867}