helia_utils/
metrics.rs

1//! Metrics implementations
2
3use std::collections::HashMap;
4use std::sync::Arc;
5
6use async_trait::async_trait;
7use dashmap::DashMap;
8
9use helia_interface::Metrics;
10
11/// Simple in-memory metrics implementation
12pub struct SimpleMetrics {
13    counters: Arc<DashMap<String, u64>>,
14    gauges: Arc<DashMap<String, f64>>,
15    histograms: Arc<DashMap<String, Vec<f64>>>,
16}
17
18impl SimpleMetrics {
19    pub fn new() -> Self {
20        Self {
21            counters: Arc::new(DashMap::new()),
22            gauges: Arc::new(DashMap::new()),
23            histograms: Arc::new(DashMap::new()),
24        }
25    }
26
27    pub fn get_counter(&self, name: &str) -> Option<u64> {
28        self.counters.get(name).map(|v| *v)
29    }
30
31    pub fn get_gauge(&self, name: &str) -> Option<f64> {
32        self.gauges.get(name).map(|v| *v)
33    }
34
35    pub fn get_histogram(&self, name: &str) -> Option<Vec<f64>> {
36        self.histograms.get(name).map(|v| v.clone())
37    }
38}
39
40impl Default for SimpleMetrics {
41    fn default() -> Self {
42        Self::new()
43    }
44}
45
46#[async_trait]
47impl Metrics for SimpleMetrics {
48    async fn record_counter(&self, name: &str, value: u64, _labels: HashMap<String, String>) {
49        *self.counters.entry(name.to_string()).or_insert(0) += value;
50    }
51
52    async fn record_gauge(&self, name: &str, value: f64, _labels: HashMap<String, String>) {
53        self.gauges.insert(name.to_string(), value);
54    }
55
56    async fn record_histogram(&self, name: &str, value: f64, _labels: HashMap<String, String>) {
57        self.histograms
58            .entry(name.to_string())
59            .or_insert_with(Vec::new)
60            .push(value);
61    }
62}