Skip to main content

commonware_runtime/telemetry/metrics/
registration.rs

1use commonware_utils::sync::Exclusive;
2use std::sync::Arc;
3
4/// A shared lifecycle token for a [`Registered`](super::Registered) metric handle.
5///
6/// When the last clone of the associated [`Registered`](super::Registered)
7/// handle is dropped, this registration is dropped as well. Runtime-managed
8/// metrics use that drop to unregister themselves from the runtime registry,
9/// while external callers may attach custom cleanup with [`Registration::from`].
10#[derive(Clone)]
11pub struct Registration {
12    _guard: Arc<dyn Send + Sync>,
13}
14
15impl Registration {
16    /// Create a registration from a guard that should be dropped when the last
17    /// associated [`Registered`](super::Registered) handle is dropped.
18    pub fn from<G>(guard: G) -> Self
19    where
20        G: Send + 'static,
21    {
22        // Exclusive access is all the holder ever needs: the guard is dropped, never read.
23        Self {
24            _guard: Arc::new(Exclusive::new(guard)),
25        }
26    }
27}