metrics_runtime/
control.rs

1use crate::{
2    data::Snapshot,
3    registry::{MetricRegistry, ScopeRegistry},
4};
5
6use metrics_core::{Observe, Observer};
7
8use std::sync::Arc;
9
10/// Handle for acquiring snapshots.
11///
12/// `Controller` is [`metrics-core`]-compatible as a snapshot provider, both for synchronous and
13/// asynchronous snapshotting.
14///
15/// [`metrics-core`]: https://docs.rs/metrics-core
16#[derive(Clone)]
17pub struct Controller {
18    metric_registry: Arc<MetricRegistry>,
19    scope_registry: Arc<ScopeRegistry>,
20}
21
22impl Controller {
23    pub(crate) fn new(
24        metric_registry: Arc<MetricRegistry>,
25        scope_registry: Arc<ScopeRegistry>,
26    ) -> Controller {
27        Controller {
28            metric_registry,
29            scope_registry,
30        }
31    }
32
33    /// Provide a snapshot of its collected metrics.
34    pub fn snapshot(&self) -> Snapshot {
35        self.metric_registry.snapshot()
36    }
37}
38
39impl Observe for Controller {
40    fn observe<O: Observer>(&self, observer: &mut O) {
41        self.metric_registry.observe(observer)
42    }
43}