1use 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#[derive(Debug, Clone, Serialize, Deserialize)]
32pub enum MetricType {
33 Counter,
34 Gauge,
35 Histogram,
36 Summary,
37}
38
39#[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#[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#[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#[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#[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#[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(Debug, Clone, Serialize, Deserialize)]
197pub struct WebhookConfig {
198 pub url: String,
199 pub http_config: Option<HttpConfig>,
200}
201
202#[derive(Debug, Clone, Serialize, Deserialize)]
203pub struct EmailConfig {
204 pub to: Vec<String>,
205 pub from: String,
206 pub subject: String,
207 pub body: String,
208}
209
210#[derive(Debug, Clone, Serialize, Deserialize)]
211pub struct SlackConfig {
212 pub api_url: String,
213 pub channel: String,
214 pub username: String,
215 pub title: String,
216 pub text: String,
217}
218
219#[derive(Debug, Clone, Serialize, Deserialize)]
220pub struct HttpConfig {
221 pub basic_auth: Option<BasicAuth>,
222 pub bearer_token: Option<String>,
223 pub tls_config: Option<TlsConfig>,
224}
225
226#[derive(Debug, Clone, Serialize, Deserialize)]
227pub struct BasicAuth {
228 pub username: String,
229 pub password: String,
230}
231
232#[derive(Debug, Clone, Serialize, Deserialize)]
233pub struct TlsConfig {
234 pub ca_file: Option<String>,
235 pub cert_file: Option<String>,
236 pub key_file: Option<String>,
237 pub insecure_skip_verify: bool,
238}
239
240#[derive(Debug, Clone, Serialize, Deserialize)]
241pub struct AlertRouting {
242 pub group_by: Vec<String>,
243 pub group_wait: Duration,
244 pub group_interval: Duration,
245 pub repeat_interval: Duration,
246 pub receiver: String,
247 pub routes: Vec<Route>,
248}
249
250#[derive(Debug, Clone, Serialize, Deserialize)]
251pub struct Route {
252 pub matchers: HashMap<String, String>,
253 pub receiver: String,
254 pub group_by: Vec<String>,
255 pub continue_route: bool,
256}
257
258#[derive(Debug, Clone, Serialize, Deserialize)]
259pub struct InhibitRule {
260 pub source_matchers: HashMap<String, String>,
261 pub target_matchers: HashMap<String, String>,
262 pub equal: Vec<String>,
263}
264
265#[derive(Debug, Clone, Serialize, Deserialize)]
266pub struct ExporterConfig {
267 pub name: String,
268 pub endpoint: String,
269 pub interval: Duration,
270 pub timeout: Duration,
271 pub labels: HashMap<String, String>,
272}
273
274#[derive(Debug, Clone, Serialize, Deserialize)]
275pub struct RetentionPolicy {
276 pub default_retention: Duration,
277 pub metric_retentions: HashMap<String, Duration>,
278 pub downsampling_rules: Vec<DownsamplingRule>,
279}
280
281#[derive(Debug, Clone, Serialize, Deserialize)]
282pub struct DownsamplingRule {
283 pub resolution: Duration,
284 pub retention: Duration,
285 pub aggregation: AggregationType,
286}
287
288#[derive(Debug, Clone, Serialize, Deserialize)]
289pub enum AggregationType {
290 Average,
291 Sum,
292 Min,
293 Max,
294 Count,
295}
296
297pub struct MonitoringSystem {
299 config: MonitoringConfig,
300 metrics: HashMap<String, Metric>,
301 alert_rules: Vec<AlertRule>,
302 active_alerts: Vec<Alert>,
303 dashboards: Vec<Dashboard>,
304}
305
306impl MonitoringSystem {
307 pub fn new(config: MonitoringConfig) -> Self {
309 Self {
310 config,
311 metrics: HashMap::new(),
312 alert_rules: Vec::new(),
313 active_alerts: Vec::new(),
314 dashboards: Vec::new(),
315 }
316 }
317
318 pub async fn initialize(&mut self) -> Result<(), MonitoringError> {
320 if !self.config.enabled {
321 return Ok(());
322 }
323
324 self.initialize_prometheus().await?;
326
327 self.initialize_grafana().await?;
329
330 self.initialize_alertmanager().await?;
332
333 self.setup_default_metrics().await?;
335
336 self.load_dashboards().await?;
338
339 Ok(())
340 }
341
342 async fn initialize_prometheus(&self) -> Result<(), MonitoringError> {
344 tracing::info!(
346 endpoint = %self.config.prometheus.endpoint,
347 "Initializing Prometheus"
348 );
349 Ok(())
350 }
351
352 async fn initialize_grafana(&self) -> Result<(), MonitoringError> {
354 if self.config.grafana.auto_provision {
355 self.provision_grafana_dashboards().await?;
357 }
358 Ok(())
359 }
360
361 async fn initialize_alertmanager(&self) -> Result<(), MonitoringError> {
363 tracing::info!(
365 endpoint = %self.config.alertmanager.endpoint,
366 "Initializing AlertManager"
367 );
368 Ok(())
369 }
370
371 async fn setup_default_metrics(&mut self) -> Result<(), MonitoringError> {
373 self.register_metric(Metric {
375 name: "auth_requests_total".to_string(),
376 metric_type: MetricType::Counter,
377 description: "Total number of authentication requests".to_string(),
378 unit: "requests".to_string(),
379 values: Vec::new(),
380 retention: Duration::from_secs(30 * 24 * 3600), });
382
383 self.register_metric(Metric {
384 name: "auth_success_total".to_string(),
385 metric_type: MetricType::Counter,
386 description: "Total number of successful authentications".to_string(),
387 unit: "requests".to_string(),
388 values: Vec::new(),
389 retention: Duration::from_secs(30 * 24 * 3600),
390 });
391
392 self.register_metric(Metric {
393 name: "auth_failures_total".to_string(),
394 metric_type: MetricType::Counter,
395 description: "Total number of failed authentications".to_string(),
396 unit: "requests".to_string(),
397 values: Vec::new(),
398 retention: Duration::from_secs(30 * 24 * 3600),
399 });
400
401 self.register_metric(Metric {
403 name: "authz_checks_total".to_string(),
404 metric_type: MetricType::Counter,
405 description: "Total number of authorization checks".to_string(),
406 unit: "checks".to_string(),
407 values: Vec::new(),
408 retention: Duration::from_secs(30 * 24 * 3600),
409 });
410
411 self.register_metric(Metric {
412 name: "authz_denied_total".to_string(),
413 metric_type: MetricType::Counter,
414 description: "Total number of denied authorization checks".to_string(),
415 unit: "checks".to_string(),
416 values: Vec::new(),
417 retention: Duration::from_secs(30 * 24 * 3600),
418 });
419
420 self.register_metric(Metric {
422 name: "request_duration_seconds".to_string(),
423 metric_type: MetricType::Histogram,
424 description: "Request duration in seconds".to_string(),
425 unit: "seconds".to_string(),
426 values: Vec::new(),
427 retention: Duration::from_secs(7 * 24 * 3600), });
429
430 self.register_metric(Metric {
431 name: "active_sessions".to_string(),
432 metric_type: MetricType::Gauge,
433 description: "Number of active user sessions".to_string(),
434 unit: "sessions".to_string(),
435 values: Vec::new(),
436 retention: Duration::from_secs(24 * 3600), });
438
439 Ok(())
440 }
441
442 pub fn register_metric(&mut self, metric: Metric) {
444 self.metrics.insert(metric.name.clone(), metric);
445 }
446
447 pub fn record_metric(
449 &mut self,
450 name: &str,
451 value: f64,
452 labels: HashMap<String, String>,
453 ) -> Result<(), MonitoringError> {
454 if let Some(metric) = self.metrics.get_mut(name) {
455 let now = SystemTime::now()
456 .duration_since(UNIX_EPOCH)
457 .unwrap_or_default();
458
459 let metric_value = MetricValue {
460 value,
461 timestamp: now.as_secs(),
462 labels,
463 };
464
465 metric.values.push(metric_value);
466
467 let cutoff = now.as_secs() - metric.retention.as_secs();
469 metric.values.retain(|v| v.timestamp > cutoff);
470
471 Ok(())
472 } else {
473 Err(MonitoringError::MetricCollection(format!(
474 "Metric not found: {}",
475 name
476 )))
477 }
478 }
479
480 pub fn add_alert_rule(&mut self, rule: AlertRule) {
482 self.alert_rules.push(rule);
483 }
484
485 pub async fn evaluate_alerts(&mut self) -> Result<(), MonitoringError> {
487 let now = SystemTime::now()
488 .duration_since(UNIX_EPOCH)
489 .unwrap_or_default();
490
491 for rule in &self.alert_rules {
492 if !rule.enabled {
493 continue;
494 }
495
496 if let Some(metric) = self.metrics.get(&rule.metric_name)
497 && let Some(latest_value) = metric.values.last()
498 {
499 let should_alert = match rule.condition {
500 AlertCondition::GreaterThan => latest_value.value > rule.threshold,
501 AlertCondition::LessThan => latest_value.value < rule.threshold,
502 AlertCondition::Equal => {
503 (latest_value.value - rule.threshold).abs() < f64::EPSILON
504 }
505 AlertCondition::NotEqual => {
506 (latest_value.value - rule.threshold).abs() > f64::EPSILON
507 }
508 AlertCondition::GreaterThanOrEqual => latest_value.value >= rule.threshold,
509 AlertCondition::LessThanOrEqual => latest_value.value <= rule.threshold,
510 };
511
512 if should_alert {
513 let existing_alert = self
515 .active_alerts
516 .iter()
517 .find(|alert| alert.rule_name == rule.name && alert.resolved_at.is_none());
518
519 if existing_alert.is_none() {
520 let alert = Alert {
521 rule_name: rule.name.clone(),
522 metric_name: rule.metric_name.clone(),
523 current_value: latest_value.value,
524 threshold: rule.threshold,
525 severity: rule.severity.clone(),
526 message: format!(
527 "Alert: {} - Current value: {}, Threshold: {}",
528 rule.name, latest_value.value, rule.threshold
529 ),
530 started_at: now.as_secs(),
531 resolved_at: None,
532 labels: rule.labels.clone(),
533 };
534
535 self.active_alerts.push(alert.clone());
536 self.send_alert(&alert).await?;
537 }
538 } else {
539 let mut alerts_to_resolve = Vec::new();
541 for alert in &mut self.active_alerts {
542 if alert.rule_name == rule.name && alert.resolved_at.is_none() {
543 alert.resolved_at = Some(now.as_secs());
544 alerts_to_resolve.push(alert.clone());
545 }
546 }
547
548 for alert in &alerts_to_resolve {
550 self.send_alert_resolution(alert).await?;
551 }
552 }
553 }
554 }
555 Ok(())
556 }
557
558 async fn send_alert(&self, alert: &Alert) -> Result<(), MonitoringError> {
560 tracing::warn!(rule = %alert.rule_name, message = %alert.message, "ALERT triggered");
562
563 for receiver in &self.config.alertmanager.receivers {
565 self.send_to_receiver(receiver, alert).await?;
566 }
567
568 Ok(())
569 }
570
571 async fn send_alert_resolution(&self, alert: &Alert) -> Result<(), MonitoringError> {
573 tracing::info!(rule = %alert.rule_name, message = %alert.message, "ALERT resolved");
574 Ok(())
575 }
576
577 async fn send_to_receiver(
579 &self,
580 receiver: &AlertReceiver,
581 alert: &Alert,
582 ) -> Result<(), MonitoringError> {
583 for webhook in &receiver.webhook_configs {
585 self.send_webhook_alert(webhook, alert).await?;
586 }
587
588 for email in &receiver.email_configs {
590 self.send_email_alert(email, alert).await?;
591 }
592
593 for slack in &receiver.slack_configs {
595 self.send_slack_alert(slack, alert).await?;
596 }
597
598 Ok(())
599 }
600
601 async fn send_webhook_alert(
603 &self,
604 _webhook: &WebhookConfig,
605 _alert: &Alert,
606 ) -> Result<(), MonitoringError> {
607 Ok(())
609 }
610
611 async fn send_email_alert(
613 &self,
614 _email: &EmailConfig,
615 _alert: &Alert,
616 ) -> Result<(), MonitoringError> {
617 Ok(())
619 }
620
621 async fn send_slack_alert(
623 &self,
624 _slack: &SlackConfig,
625 _alert: &Alert,
626 ) -> Result<(), MonitoringError> {
627 Ok(())
629 }
630
631 async fn load_dashboards(&mut self) -> Result<(), MonitoringError> {
633 self.dashboards = self.config.dashboards.clone();
634 Ok(())
635 }
636
637 async fn provision_grafana_dashboards(&self) -> Result<(), MonitoringError> {
639 for dashboard in &self.config.dashboards {
640 tracing::info!(title = %dashboard.title, "Provisioning Grafana dashboard");
641 }
642 Ok(())
643 }
644
645 pub fn export_prometheus_metrics(&self) -> String {
647 let mut output = String::new();
648
649 for (name, metric) in &self.metrics {
650 output.push_str(&format!("# HELP {} {}\n", name, metric.description));
652 output.push_str(&format!(
653 "# TYPE {} {}\n",
654 name,
655 match metric.metric_type {
656 MetricType::Counter => "counter",
657 MetricType::Gauge => "gauge",
658 MetricType::Histogram => "histogram",
659 MetricType::Summary => "summary",
660 }
661 ));
662
663 for value in &metric.values {
665 let labels = if value.labels.is_empty() {
666 String::new()
667 } else {
668 let label_pairs: Vec<String> = value
669 .labels
670 .iter()
671 .map(|(k, v)| format!("{}=\"{}\"", k, v))
672 .collect();
673 format!("{{{}}}", label_pairs.join(","))
674 };
675
676 output.push_str(&format!(
677 "{}{} {} {}\n",
678 name,
679 labels,
680 value.value,
681 value.timestamp * 1000
682 ));
683 }
684 }
685
686 output
687 }
688
689 pub fn get_metrics(&self) -> &HashMap<String, Metric> {
691 &self.metrics
692 }
693
694 pub fn get_active_alerts(&self) -> &Vec<Alert> {
696 &self.active_alerts
697 }
698
699 pub fn get_dashboards(&self) -> &Vec<Dashboard> {
701 &self.dashboards
702 }
703}
704
705impl Default for MonitoringConfig {
706 fn default() -> Self {
707 Self {
708 enabled: true,
709 prometheus: PrometheusConfig {
710 endpoint: "http://localhost:9090".to_string(),
711 scrape_interval: Duration::from_secs(15),
712 evaluation_interval: Duration::from_secs(15),
713 external_labels: HashMap::new(),
714 rule_files: vec!["alerts.yml".to_string()],
715 },
716 grafana: GrafanaConfig {
717 endpoint: "http://localhost:3000".to_string(),
718 api_key: "".to_string().into(),
719 organization: "Main Org.".to_string(),
720 datasource: "Prometheus".to_string(),
721 auto_provision: true,
722 },
723 alertmanager: AlertManagerConfig {
724 endpoint: "http://localhost:9093".to_string(),
725 receivers: vec![AlertReceiver {
726 name: "default".to_string(),
727 webhook_configs: Vec::new(),
728 email_configs: Vec::new(),
729 slack_configs: Vec::new(),
730 }],
731 routing: AlertRouting {
732 group_by: vec!["alertname".to_string()],
733 group_wait: Duration::from_secs(10),
734 group_interval: Duration::from_secs(10),
735 repeat_interval: Duration::from_secs(3600),
736 receiver: "default".to_string(),
737 routes: Vec::new(),
738 },
739 inhibit_rules: Vec::new(),
740 },
741 custom_exporters: Vec::new(),
742 dashboards: Vec::new(),
743 retention_policy: RetentionPolicy {
744 default_retention: Duration::from_secs(30 * 24 * 3600), metric_retentions: HashMap::new(),
746 downsampling_rules: Vec::new(),
747 },
748 }
749 }
750}
751
752#[cfg(test)]
753mod tests {
754 use super::*;
755
756 #[test]
757 fn test_monitoring_system_creation() {
758 let config = MonitoringConfig::default();
759 let system = MonitoringSystem::new(config);
760
761 assert!(system.metrics.is_empty());
762 assert!(system.alert_rules.is_empty());
763 assert!(system.active_alerts.is_empty());
764 }
765
766 #[tokio::test]
767 async fn test_metric_registration() {
768 let config = MonitoringConfig::default();
769 let mut system = MonitoringSystem::new(config);
770
771 let metric = Metric {
772 name: "test_metric".to_string(),
773 metric_type: MetricType::Counter,
774 description: "Test metric".to_string(),
775 unit: "count".to_string(),
776 values: Vec::new(),
777 retention: Duration::from_secs(3600),
778 };
779
780 system.register_metric(metric);
781 assert!(system.metrics.contains_key("test_metric"));
782 }
783
784 #[tokio::test]
785 async fn test_metric_recording() {
786 let config = MonitoringConfig::default();
787 let mut system = MonitoringSystem::new(config);
788
789 let metric = Metric {
790 name: "test_counter".to_string(),
791 metric_type: MetricType::Counter,
792 description: "Test counter".to_string(),
793 unit: "count".to_string(),
794 values: Vec::new(),
795 retention: Duration::from_secs(3600),
796 };
797
798 system.register_metric(metric);
799
800 let mut labels = HashMap::new();
801 labels.insert("service".to_string(), "auth".to_string());
802
803 let result = system.record_metric("test_counter", 1.0, labels);
804 assert!(result.is_ok());
805
806 let metric = system.metrics.get("test_counter").unwrap();
807 assert_eq!(metric.values.len(), 1);
808 assert_eq!(metric.values[0].value, 1.0);
809 }
810
811 #[test]
812 fn test_prometheus_export() {
813 let config = MonitoringConfig::default();
814 let mut system = MonitoringSystem::new(config);
815
816 let metric = Metric {
817 name: "test_gauge".to_string(),
818 metric_type: MetricType::Gauge,
819 description: "Test gauge".to_string(),
820 unit: "value".to_string(),
821 values: vec![MetricValue {
822 value: 42.0,
823 timestamp: 1640995200,
824 labels: HashMap::new(),
825 }],
826 retention: Duration::from_secs(3600),
827 };
828
829 system.register_metric(metric);
830
831 let export = system.export_prometheus_metrics();
832 assert!(export.contains("# HELP test_gauge Test gauge"));
833 assert!(export.contains("# TYPE test_gauge gauge"));
834 assert!(export.contains("test_gauge 42"));
835 }
836}