1use 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#[derive(Debug, Clone, Serialize, Deserialize)]
31pub enum MetricType {
32 Counter,
33 Gauge,
34 Histogram,
35 Summary,
36}
37
38#[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#[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#[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#[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#[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#[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
239impl 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
352pub 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 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 pub async fn initialize(&mut self) -> Result<(), MonitoringError> {
375 if !self.config.enabled {
376 return Ok(());
377 }
378
379 self.initialize_prometheus().await?;
381
382 self.initialize_grafana().await?;
384
385 self.initialize_alertmanager().await?;
387
388 self.setup_default_metrics().await?;
390
391 self.load_dashboards().await?;
393
394 Ok(())
395 }
396
397 async fn initialize_prometheus(&self) -> Result<(), MonitoringError> {
399 tracing::info!(
401 endpoint = %self.config.prometheus.endpoint,
402 "Initializing Prometheus"
403 );
404 Ok(())
405 }
406
407 async fn initialize_grafana(&self) -> Result<(), MonitoringError> {
409 if self.config.grafana.auto_provision {
410 self.provision_grafana_dashboards().await?;
412 }
413 Ok(())
414 }
415
416 async fn initialize_alertmanager(&self) -> Result<(), MonitoringError> {
418 tracing::info!(
420 endpoint = %self.config.alertmanager.endpoint,
421 "Initializing AlertManager"
422 );
423 Ok(())
424 }
425
426 async fn setup_default_metrics(&mut self) -> Result<(), MonitoringError> {
428 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), });
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 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 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), });
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), });
493
494 Ok(())
495 }
496
497 pub fn register_metric(&mut self, metric: Metric) {
499 self.metrics.insert(metric.name.clone(), metric);
500 }
501
502 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()
511 .duration_since(UNIX_EPOCH)
512 .unwrap_or_default();
513
514 let metric_value = MetricValue {
515 value,
516 timestamp: now.as_secs(),
517 labels,
518 };
519
520 metric.values.push(metric_value);
521
522 let cutoff = now.as_secs() - metric.retention.as_secs();
524 metric.values.retain(|v| v.timestamp > cutoff);
525
526 Ok(())
527 } else {
528 Err(MonitoringError::MetricCollection(format!(
529 "Metric not found: {}",
530 name
531 )))
532 }
533 }
534
535 pub fn add_alert_rule(&mut self, rule: AlertRule) {
537 self.alert_rules.push(rule);
538 }
539
540 pub async fn evaluate_alerts(&mut self) -> Result<(), MonitoringError> {
542 let now = SystemTime::now()
543 .duration_since(UNIX_EPOCH)
544 .unwrap_or_default();
545
546 for rule in &self.alert_rules {
547 if !rule.enabled {
548 continue;
549 }
550
551 if let Some(metric) = self.metrics.get(&rule.metric_name)
552 && let Some(latest_value) = metric.values.last()
553 {
554 let should_alert = match rule.condition {
555 AlertCondition::GreaterThan => latest_value.value > rule.threshold,
556 AlertCondition::LessThan => latest_value.value < rule.threshold,
557 AlertCondition::Equal => {
558 (latest_value.value - rule.threshold).abs() < f64::EPSILON
559 }
560 AlertCondition::NotEqual => {
561 (latest_value.value - rule.threshold).abs() > f64::EPSILON
562 }
563 AlertCondition::GreaterThanOrEqual => latest_value.value >= rule.threshold,
564 AlertCondition::LessThanOrEqual => latest_value.value <= rule.threshold,
565 };
566
567 if should_alert {
568 let existing_alert = self
570 .active_alerts
571 .iter()
572 .find(|alert| alert.rule_name == rule.name && alert.resolved_at.is_none());
573
574 if existing_alert.is_none() {
575 let alert = Alert {
576 rule_name: rule.name.clone(),
577 metric_name: rule.metric_name.clone(),
578 current_value: latest_value.value,
579 threshold: rule.threshold,
580 severity: rule.severity.clone(),
581 message: format!(
582 "Alert: {} - Current value: {}, Threshold: {}",
583 rule.name, latest_value.value, rule.threshold
584 ),
585 started_at: now.as_secs(),
586 resolved_at: None,
587 labels: rule.labels.clone(),
588 };
589
590 self.active_alerts.push(alert.clone());
591 self.send_alert(&alert).await?;
592 }
593 } else {
594 let mut alerts_to_resolve = Vec::new();
596 for alert in &mut self.active_alerts {
597 if alert.rule_name == rule.name && alert.resolved_at.is_none() {
598 alert.resolved_at = Some(now.as_secs());
599 alerts_to_resolve.push(alert.clone());
600 }
601 }
602
603 for alert in &alerts_to_resolve {
605 self.send_alert_resolution(alert).await?;
606 }
607 }
608 }
609 }
610 Ok(())
611 }
612
613 async fn send_alert(&self, alert: &Alert) -> Result<(), MonitoringError> {
615 tracing::warn!(rule = %alert.rule_name, message = %alert.message, "ALERT triggered");
617
618 for receiver in &self.config.alertmanager.receivers {
620 self.send_to_receiver(receiver, alert).await?;
621 }
622
623 Ok(())
624 }
625
626 async fn send_alert_resolution(&self, alert: &Alert) -> Result<(), MonitoringError> {
628 tracing::info!(rule = %alert.rule_name, message = %alert.message, "ALERT resolved");
629 Ok(())
630 }
631
632 async fn send_to_receiver(
634 &self,
635 receiver: &AlertReceiver,
636 alert: &Alert,
637 ) -> Result<(), MonitoringError> {
638 for webhook in &receiver.webhook_configs {
640 self.send_webhook_alert(webhook, alert).await?;
641 }
642
643 for email in &receiver.email_configs {
645 self.send_email_alert(email, alert).await?;
646 }
647
648 for slack in &receiver.slack_configs {
650 self.send_slack_alert(slack, alert).await?;
651 }
652
653 Ok(())
654 }
655
656 async fn send_webhook_alert(
658 &self,
659 _webhook: &WebhookConfig,
660 _alert: &Alert,
661 ) -> Result<(), MonitoringError> {
662 Ok(())
664 }
665
666 async fn send_email_alert(
668 &self,
669 _email: &EmailConfig,
670 _alert: &Alert,
671 ) -> Result<(), MonitoringError> {
672 Ok(())
674 }
675
676 async fn send_slack_alert(
678 &self,
679 _slack: &SlackConfig,
680 _alert: &Alert,
681 ) -> Result<(), MonitoringError> {
682 Ok(())
684 }
685
686 async fn load_dashboards(&mut self) -> Result<(), MonitoringError> {
688 self.dashboards = self.config.dashboards.clone();
689 Ok(())
690 }
691
692 async fn provision_grafana_dashboards(&self) -> Result<(), MonitoringError> {
694 for dashboard in &self.config.dashboards {
695 tracing::info!(title = %dashboard.title, "Provisioning Grafana dashboard");
696 }
697 Ok(())
698 }
699
700 pub fn export_prometheus_metrics(&self) -> String {
702 let mut output = String::new();
703
704 for (name, metric) in &self.metrics {
705 output.push_str(&format!("# HELP {} {}\n", name, metric.description));
707 output.push_str(&format!(
708 "# TYPE {} {}\n",
709 name,
710 match metric.metric_type {
711 MetricType::Counter => "counter",
712 MetricType::Gauge => "gauge",
713 MetricType::Histogram => "histogram",
714 MetricType::Summary => "summary",
715 }
716 ));
717
718 for value in &metric.values {
720 let labels = if value.labels.is_empty() {
721 String::new()
722 } else {
723 let label_pairs: Vec<String> = value
724 .labels
725 .iter()
726 .map(|(k, v)| format!("{}=\"{}\"", k, v))
727 .collect();
728 format!("{{{}}}", label_pairs.join(","))
729 };
730
731 output.push_str(&format!(
732 "{}{} {} {}\n",
733 name,
734 labels,
735 value.value,
736 value.timestamp * 1000
737 ));
738 }
739 }
740
741 output
742 }
743
744 pub fn get_metrics(&self) -> &HashMap<String, Metric> {
746 &self.metrics
747 }
748
749 pub fn get_active_alerts(&self) -> &Vec<Alert> {
751 &self.active_alerts
752 }
753
754 pub fn get_dashboards(&self) -> &Vec<Dashboard> {
756 &self.dashboards
757 }
758}
759
760impl Default for MonitoringConfig {
761 fn default() -> Self {
762 Self {
763 enabled: true,
764 prometheus: PrometheusConfig {
765 endpoint: "http://localhost:9090".to_string(),
766 scrape_interval: Duration::from_secs(15),
767 evaluation_interval: Duration::from_secs(15),
768 external_labels: HashMap::new(),
769 rule_files: vec!["alerts.yml".to_string()],
770 },
771 grafana: GrafanaConfig {
772 endpoint: "http://localhost:3000".to_string(),
773 api_key: "".to_string(),
774 organization: "Main Org.".to_string(),
775 datasource: "Prometheus".to_string(),
776 auto_provision: true,
777 },
778 alertmanager: AlertManagerConfig {
779 endpoint: "http://localhost:9093".to_string(),
780 receivers: vec![AlertReceiver {
781 name: "default".to_string(),
782 webhook_configs: Vec::new(),
783 email_configs: Vec::new(),
784 slack_configs: Vec::new(),
785 }],
786 routing: AlertRouting {
787 group_by: vec!["alertname".to_string()],
788 group_wait: Duration::from_secs(10),
789 group_interval: Duration::from_secs(10),
790 repeat_interval: Duration::from_secs(3600),
791 receiver: "default".to_string(),
792 routes: Vec::new(),
793 },
794 inhibit_rules: Vec::new(),
795 },
796 custom_exporters: Vec::new(),
797 dashboards: Vec::new(),
798 retention_policy: RetentionPolicy {
799 default_retention: Duration::from_secs(30 * 24 * 3600), metric_retentions: HashMap::new(),
801 downsampling_rules: Vec::new(),
802 },
803 }
804 }
805}
806
807#[cfg(test)]
808mod tests {
809 use super::*;
810
811 #[test]
812 fn test_monitoring_system_creation() {
813 let config = MonitoringConfig::default();
814 let system = MonitoringSystem::new(config);
815
816 assert!(system.metrics.is_empty());
817 assert!(system.alert_rules.is_empty());
818 assert!(system.active_alerts.is_empty());
819 }
820
821 #[tokio::test]
822 async fn test_metric_registration() {
823 let config = MonitoringConfig::default();
824 let mut system = MonitoringSystem::new(config);
825
826 let metric = Metric {
827 name: "test_metric".to_string(),
828 metric_type: MetricType::Counter,
829 description: "Test metric".to_string(),
830 unit: "count".to_string(),
831 values: Vec::new(),
832 retention: Duration::from_secs(3600),
833 };
834
835 system.register_metric(metric);
836 assert!(system.metrics.contains_key("test_metric"));
837 }
838
839 #[tokio::test]
840 async fn test_metric_recording() {
841 let config = MonitoringConfig::default();
842 let mut system = MonitoringSystem::new(config);
843
844 let metric = Metric {
845 name: "test_counter".to_string(),
846 metric_type: MetricType::Counter,
847 description: "Test counter".to_string(),
848 unit: "count".to_string(),
849 values: Vec::new(),
850 retention: Duration::from_secs(3600),
851 };
852
853 system.register_metric(metric);
854
855 let mut labels = HashMap::new();
856 labels.insert("service".to_string(), "auth".to_string());
857
858 let result = system.record_metric("test_counter", 1.0, labels);
859 assert!(result.is_ok());
860
861 let metric = system.metrics.get("test_counter").unwrap();
862 assert_eq!(metric.values.len(), 1);
863 assert_eq!(metric.values[0].value, 1.0);
864 }
865
866 #[test]
867 fn test_prometheus_export() {
868 let config = MonitoringConfig::default();
869 let mut system = MonitoringSystem::new(config);
870
871 let metric = Metric {
872 name: "test_gauge".to_string(),
873 metric_type: MetricType::Gauge,
874 description: "Test gauge".to_string(),
875 unit: "value".to_string(),
876 values: vec![MetricValue {
877 value: 42.0,
878 timestamp: 1640995200,
879 labels: HashMap::new(),
880 }],
881 retention: Duration::from_secs(3600),
882 };
883
884 system.register_metric(metric);
885
886 let export = system.export_prometheus_metrics();
887 assert!(export.contains("# HELP test_gauge Test gauge"));
888 assert!(export.contains("# TYPE test_gauge gauge"));
889 assert!(export.contains("test_gauge 42"));
890 }
891}