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