Skip to main content

brainos_core/
metrics.rs

1//! Shared subsystem metrics used across subsystems for Prometheus exposition.
2//!
3//! Kept in `brain_core` so that subsystems (signal, hippocampus, backends) can
4//! increment counters without taking a dependency on the HTTP adapter. The HTTP
5//! adapter is the only component that renders these as Prometheus text.
6
7use std::sync::atomic::{AtomicU64, Ordering};
8
9/// Cross-subsystem counters covering memory activity, embedding, consolidation,
10/// circuit-breaker events, and intent classification. Safe to `Arc<>` and share.
11#[derive(Default, Debug)]
12pub struct SubsystemMetrics {
13    // Embedding
14    pub embedding_requests_total: AtomicU64,
15    pub embedding_fallbacks_total: AtomicU64,
16
17    // Consolidation
18    pub consolidation_runs_total: AtomicU64,
19    pub consolidation_pruned_total: AtomicU64,
20    pub consolidation_promoted_total: AtomicU64,
21
22    // Circuit breaker
23    pub circuit_open_total: AtomicU64,
24    pub circuit_resets_total: AtomicU64,
25
26    // Thalamus
27    pub intent_classifications_total: AtomicU64,
28    pub intent_llm_fallbacks_total: AtomicU64,
29}
30
31impl SubsystemMetrics {
32    pub fn new() -> Self {
33        Self::default()
34    }
35
36    #[inline]
37    pub fn inc_embedding_request(&self) {
38        self.embedding_requests_total
39            .fetch_add(1, Ordering::Relaxed);
40    }
41
42    #[inline]
43    pub fn inc_embedding_fallback(&self) {
44        self.embedding_fallbacks_total
45            .fetch_add(1, Ordering::Relaxed);
46    }
47
48    #[inline]
49    pub fn inc_consolidation_run(&self) {
50        self.consolidation_runs_total
51            .fetch_add(1, Ordering::Relaxed);
52    }
53
54    #[inline]
55    pub fn add_consolidation_pruned(&self, n: u64) {
56        self.consolidation_pruned_total
57            .fetch_add(n, Ordering::Relaxed);
58    }
59
60    #[inline]
61    pub fn add_consolidation_promoted(&self, n: u64) {
62        self.consolidation_promoted_total
63            .fetch_add(n, Ordering::Relaxed);
64    }
65
66    #[inline]
67    pub fn inc_circuit_open(&self) {
68        self.circuit_open_total.fetch_add(1, Ordering::Relaxed);
69    }
70
71    #[inline]
72    pub fn inc_circuit_reset(&self) {
73        self.circuit_resets_total.fetch_add(1, Ordering::Relaxed);
74    }
75
76    #[inline]
77    pub fn inc_intent_classification(&self) {
78        self.intent_classifications_total
79            .fetch_add(1, Ordering::Relaxed);
80    }
81
82    #[inline]
83    pub fn inc_intent_llm_fallback(&self) {
84        self.intent_llm_fallbacks_total
85            .fetch_add(1, Ordering::Relaxed);
86    }
87
88    /// Snapshot of all counters for rendering.
89    pub fn snapshot(&self) -> SubsystemSnapshot {
90        SubsystemSnapshot {
91            embedding_requests_total: self.embedding_requests_total.load(Ordering::Relaxed),
92            embedding_fallbacks_total: self.embedding_fallbacks_total.load(Ordering::Relaxed),
93            consolidation_runs_total: self.consolidation_runs_total.load(Ordering::Relaxed),
94            consolidation_pruned_total: self.consolidation_pruned_total.load(Ordering::Relaxed),
95            consolidation_promoted_total: self.consolidation_promoted_total.load(Ordering::Relaxed),
96            circuit_open_total: self.circuit_open_total.load(Ordering::Relaxed),
97            circuit_resets_total: self.circuit_resets_total.load(Ordering::Relaxed),
98            intent_classifications_total: self.intent_classifications_total.load(Ordering::Relaxed),
99            intent_llm_fallbacks_total: self.intent_llm_fallbacks_total.load(Ordering::Relaxed),
100        }
101    }
102}
103
104#[derive(Debug, Clone, Copy)]
105pub struct SubsystemSnapshot {
106    pub embedding_requests_total: u64,
107    pub embedding_fallbacks_total: u64,
108    pub consolidation_runs_total: u64,
109    pub consolidation_pruned_total: u64,
110    pub consolidation_promoted_total: u64,
111    pub circuit_open_total: u64,
112    pub circuit_resets_total: u64,
113    pub intent_classifications_total: u64,
114    pub intent_llm_fallbacks_total: u64,
115}