Skip to main content

nodedb_bridge/
metrics.rs

1// SPDX-License-Identifier: BUSL-1.1
2
3//! Bridge throughput and backpressure metrics.
4//!
5//! All counters are atomic so they can be read from any thread (e.g., a metrics
6//! exporter on the Control Plane reading counters owned by the Data Plane).
7
8use std::sync::atomic::{AtomicU64, Ordering};
9
10/// Shared metrics for a single SPSC channel.
11pub struct BridgeMetrics {
12    /// Total items pushed (written by producer).
13    push_count: AtomicU64,
14
15    /// Total items popped (written by consumer).
16    pop_count: AtomicU64,
17
18    /// Number of times the producer found the queue full.
19    full_count: AtomicU64,
20}
21
22impl BridgeMetrics {
23    pub(crate) fn new() -> Self {
24        Self {
25            push_count: AtomicU64::new(0),
26            pop_count: AtomicU64::new(0),
27            full_count: AtomicU64::new(0),
28        }
29    }
30
31    pub(crate) fn record_push(&self) {
32        self.push_count.fetch_add(1, Ordering::Relaxed);
33    }
34
35    pub(crate) fn record_pop(&self) {
36        self.pop_count.fetch_add(1, Ordering::Relaxed);
37    }
38
39    pub(crate) fn record_pops(&self, n: u64) {
40        self.pop_count.fetch_add(n, Ordering::Relaxed);
41    }
42
43    pub(crate) fn record_full(&self) {
44        self.full_count.fetch_add(1, Ordering::Relaxed);
45    }
46
47    /// Total items successfully enqueued.
48    pub fn pushes(&self) -> u64 {
49        self.push_count.load(Ordering::Relaxed)
50    }
51
52    /// Total items successfully dequeued.
53    pub fn pops(&self) -> u64 {
54        self.pop_count.load(Ordering::Relaxed)
55    }
56
57    /// Number of times the producer was blocked by a full queue.
58    pub fn full_events(&self) -> u64 {
59        self.full_count.load(Ordering::Relaxed)
60    }
61
62    /// Items currently in flight (pushed but not yet popped).
63    pub fn in_flight(&self) -> u64 {
64        let pushed = self.pushes();
65        let popped = self.pops();
66        pushed.saturating_sub(popped)
67    }
68}