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