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