laminar_connectors/prom/
mod.rs1use prometheus::{IntCounter, IntCounterVec, IntGauge, IntGaugeVec, Opts, Registry};
7
8pub struct RegHandle<'a> {
10 registry: &'a Registry,
11 _local: Option<Registry>,
12}
13
14impl RegHandle<'_> {
15 #[must_use]
17 pub fn registry(&self) -> &Registry {
18 self.registry
19 }
20
21 #[must_use]
27 pub fn counter(&self, name: &str, help: &str) -> IntCounter {
28 let c =
29 IntCounter::new(name, help).unwrap_or_else(|e| panic!("invalid counter '{name}': {e}"));
30 self.registry.register(Box::new(c.clone())).ok();
31 c
32 }
33
34 #[must_use]
40 pub fn gauge(&self, name: &str, help: &str) -> IntGauge {
41 let g = IntGauge::new(name, help).unwrap_or_else(|e| panic!("invalid gauge '{name}': {e}"));
42 self.registry.register(Box::new(g.clone())).ok();
43 g
44 }
45
46 #[must_use]
52 pub fn counter_vec(&self, name: &str, help: &str, labels: &[&str]) -> IntCounterVec {
53 let v = IntCounterVec::new(Opts::new(name, help), labels)
54 .unwrap_or_else(|e| panic!("invalid counter_vec '{name}': {e}"));
55 self.registry.register(Box::new(v.clone())).ok();
56 v
57 }
58
59 #[must_use]
65 pub fn gauge_vec(&self, name: &str, help: &str, labels: &[&str]) -> IntGaugeVec {
66 let v = IntGaugeVec::new(Opts::new(name, help), labels)
67 .unwrap_or_else(|e| panic!("invalid gauge_vec '{name}': {e}"));
68 self.registry.register(Box::new(v.clone())).ok();
69 v
70 }
71}
72
73#[must_use]
77#[allow(clippy::missing_panics_doc)] pub fn reg_or_local<'a>(
79 registry: Option<&'a Registry>,
80 local: &'a mut Option<Registry>,
81) -> RegHandle<'a> {
82 if let Some(r) = registry {
83 RegHandle {
84 registry: r,
85 _local: None,
86 }
87 } else {
88 *local = Some(Registry::new());
89 RegHandle {
90 registry: local.as_ref().expect("just initialized"),
91 _local: None,
92 }
93 }
94}
95
96#[cfg(test)]
97mod tests;