Skip to main content

maple_runtime/telemetry/
mod.rs

1//! Telemetry and observability for MAPLE Resonance Runtime
2
3use std::cell::RefCell;
4use crate::runtime_core::ResonatorHandle;
5use crate::config::TelemetryConfig;
6
7/// Runtime telemetry system
8pub struct RuntimeTelemetry {
9    config: TelemetryConfig,
10    metrics: RefCell<MetricsCollector>,
11}
12
13impl RuntimeTelemetry {
14    pub fn new(config: &TelemetryConfig) -> Self {
15        Self {
16            config: config.clone(),
17            metrics: RefCell::new(MetricsCollector::new()),
18        }
19    }
20
21    /// Record Resonator registration
22    pub fn resonator_registered(&self, handle: &ResonatorHandle) {
23        if !self.config.metrics_enabled {
24            return;
25        }
26
27        tracing::info!("Resonator registered: {}", handle.id);
28        self.metrics.borrow_mut().increment("resonator_registrations");
29    }
30
31    /// Record Resonator resume
32    pub fn resonator_resumed(&self, handle: &ResonatorHandle) {
33        if !self.config.metrics_enabled {
34            return;
35        }
36
37        tracing::info!("Resonator resumed: {}", handle.id);
38        self.metrics.borrow_mut().increment("resonator_resumes");
39    }
40
41    /// Flush telemetry data
42    pub async fn flush(&self) {
43        if !self.config.enabled {
44            return;
45        }
46
47        tracing::debug!("Flushing telemetry");
48        self.metrics.borrow().flush();
49    }
50
51    /// Record coupling establishment
52    pub fn coupling_established(&self, source: &str, target: &str, strength: f64) {
53        if !self.config.metrics_enabled {
54            return;
55        }
56
57        tracing::debug!(
58            "Coupling established: {} -> {} (strength: {})",
59            source,
60            target,
61            strength
62        );
63        self.metrics.borrow_mut().increment("coupling_establishments");
64    }
65
66    /// Record attention allocation
67    pub fn attention_allocated(&self, resonator: &str, amount: u64) {
68        if !self.config.detailed_metrics {
69            return;
70        }
71
72        tracing::trace!("Attention allocated: {} (amount: {})", resonator, amount);
73        self.metrics.borrow_mut().record_gauge("attention_allocated", amount as f64);
74    }
75
76    /// Record invariant violation
77    pub fn invariant_violated(&self, invariant: &str) {
78        if !self.config.metrics_enabled {
79            return;
80        }
81
82        tracing::error!("Invariant violated: {}", invariant);
83        self.metrics.borrow_mut().increment("invariant_violations");
84    }
85}
86
87/// Metrics collector
88struct MetricsCollector {
89    counters: std::collections::HashMap<String, u64>,
90    gauges: std::collections::HashMap<String, f64>,
91}
92
93impl MetricsCollector {
94    fn new() -> Self {
95        Self {
96            counters: std::collections::HashMap::new(),
97            gauges: std::collections::HashMap::new(),
98        }
99    }
100
101    fn increment(&mut self, metric: &str) {
102        *self.counters.entry(metric.to_string()).or_insert(0) += 1;
103    }
104
105    fn record_gauge(&mut self, metric: &str, value: f64) {
106        self.gauges.insert(metric.to_string(), value);
107    }
108
109    fn flush(&self) {
110        // In real implementation, would flush to metrics backend
111        tracing::trace!("Metrics flushed (placeholder)");
112    }
113}