sui_security_watchdog/
metrics.rs1use prometheus::{IntGauge, Registry, register_int_gauge_with_registry};
5use std::collections::HashMap;
6use tokio::sync::Mutex;
7
8#[derive(Debug)]
11pub struct WatchdogMetrics {
12 registry: Registry,
14 metrics: Mutex<HashMap<String, IntGauge>>,
17}
18
19impl WatchdogMetrics {
20 pub fn new(registry: &Registry) -> Self {
22 Self {
23 registry: registry.clone(),
24 metrics: Mutex::new(HashMap::new()),
25 }
26 }
27
28 pub async fn get(&self, metric_name: &str) -> anyhow::Result<IntGauge> {
31 let mut metrics = self.metrics.lock().await;
32 let metric = metrics.entry(metric_name.to_string()).or_insert_with(|| {
34 register_int_gauge_with_registry!(metric_name, metric_name, &self.registry).unwrap()
35 });
36 Ok(metric.clone())
37 }
38
39 pub async fn get_exact(&self, metric_name: &str) -> anyhow::Result<IntGauge> {
42 let mut metrics = self.metrics.lock().await;
43 let metric_name = format!("{}_exact", metric_name);
44 let metric = metrics.entry(metric_name.clone()).or_insert_with(|| {
46 register_int_gauge_with_registry!(&metric_name, &metric_name, &self.registry).unwrap()
47 });
48 Ok(metric.clone())
49 }
50
51 pub async fn get_lower(&self, metric_name: &str) -> anyhow::Result<IntGauge> {
53 let mut metrics = self.metrics.lock().await;
54 let metric_name = format!("{}_lower", metric_name);
55 let metric = metrics.entry(metric_name.clone()).or_insert_with(|| {
56 register_int_gauge_with_registry!(&metric_name, &metric_name, &self.registry).unwrap()
57 });
58 Ok(metric.clone())
59 }
60
61 pub async fn get_upper(&self, metric_name: &str) -> anyhow::Result<IntGauge> {
63 let mut metrics = self.metrics.lock().await;
64 let metric_name = format!("{}_upper", metric_name);
65 let metric = metrics.entry(metric_name.clone()).or_insert_with(|| {
66 register_int_gauge_with_registry!(&metric_name, &metric_name, &self.registry).unwrap()
67 });
68 Ok(metric.clone())
69 }
70}