maple_runtime/telemetry/
mod.rs1use std::cell::RefCell;
4use crate::runtime_core::ResonatorHandle;
5use crate::config::TelemetryConfig;
6
7pub 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 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 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 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 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 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 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
87struct 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 tracing::trace!("Metrics flushed (placeholder)");
112 }
113}