Skip to main content

commonware_storage/qmdb/sync/
metrics.rs

1use commonware_runtime::telemetry::metrics::{Gauge, GaugeExt, MetricsExt};
2
3/// Progress gauges updated by a sync flow.
4///
5/// Progress is expressed as the number of leaves synced against the total
6/// leaves in the current sync target. The gauges match once the flow has
7/// synced the latest target.
8pub struct Metrics {
9    /// Total leaves in the current sync target.
10    target_leaf_count: Gauge,
11    /// Leaves synced so far.
12    leaf_count: Gauge,
13}
14
15impl Metrics {
16    /// Register sync progress metrics on the provided context.
17    pub fn new(context: &impl commonware_runtime::Metrics) -> Self {
18        Self {
19            target_leaf_count: context.gauge(
20                "target_leaf_count",
21                "Total leaves in the current sync target",
22            ),
23            leaf_count: context.gauge(
24                "leaf_count",
25                "Leaves synced so far, equal to target_leaf_count when sync completes",
26            ),
27        }
28    }
29
30    /// Record the leaf count of the current sync target.
31    pub fn record_target(&self, leaf_count: u64) {
32        let _ = self.target_leaf_count.try_set(leaf_count);
33    }
34
35    /// Record the number of leaves synced so far.
36    pub fn record_synced(&self, leaf_count: u64) {
37        let _ = self.leaf_count.try_set(leaf_count);
38    }
39}