anya-core 1.2.0

Enterprise-grade Bitcoin Infrastructure Platform
Documentation
// Core metrics implementation using Prometheus
use std::collections::HashMap;
// [AIR-3][AIS-3][BPC-3][RES-3] Removed unused import: crate::AnyaResult

/// Simple prometheus metrics implementation
#[derive(Default)]
pub struct PrometheusMetrics {
    /// Counters for various metrics
    counters: HashMap<String, u64>,
    /// Gauges for various metrics
    gauges: HashMap<String, f64>,
    /// Labels for metrics (key -> (label, value))
    labels: HashMap<String, Vec<(String, String)>>,
}

impl PrometheusMetrics {
    /// Create new metrics collector
    pub fn new() -> Self {
        Self::default()
    }

    /// Increment a counter with optional labels
    pub fn increment_counter(&mut self, name: &str, label_name: &str, label_value: &str) {
        let counter = self.counters.entry(name.to_string()).or_insert(0);
        *counter += 1;

        // Add label if provided
        if !label_name.is_empty() && !label_value.is_empty() {
            let labels = self.labels.entry(name.to_string()).or_default();
            labels.push((label_name.to_string(), label_value.to_string()));
        }
    }

    /// Set a gauge value
    pub fn set_gauge(&mut self, name: &str, value: f64) {
        self.gauges.insert(name.to_string(), value);
    }

    /// Get a counter value
    pub fn get_counter(&self, name: &str) -> Option<u64> {
        self.counters.get(name).copied()
    }

    /// Get a gauge value
    pub fn get_gauge(&self, name: &str) -> Option<f64> {
        self.gauges.get(name).copied()
    }

    /// Get all metrics as JSON-compatible structure
    pub fn get_metrics_json(&self) -> HashMap<String, serde_json::Value> {
        let mut result = HashMap::new();

        // Add counters
        for (name, value) in &self.counters {
            result.insert(
                format!("counter_{name}"),
                serde_json::json!({
                    "value": value,
                    "type": "counter",
                    "labels": self.labels.get(name).unwrap_or(&Vec::new())
                }),
            );
        }

        // Add gauges
        for (name, value) in &self.gauges {
            result.insert(
                format!("gauge_{name}"),
                serde_json::json!({
                    "value": value,
                    "type": "gauge",
                    "labels": self.labels.get(name).unwrap_or(&Vec::new())
                }),
            );
        }

        result
    }
}