hyperlight_host/metrics/
int_gauge_vec.rs1use prometheus::core::{AtomicI64, GenericGaugeVec};
18use prometheus::register_int_gauge_vec_with_registry;
19use tracing::{instrument, Span};
20
21use super::{
22 get_metric_opts, get_metrics_registry, GetHyperlightMetric, HyperlightMetric,
23 HyperlightMetricOps,
24};
25use crate::{new_error, HyperlightError, Result};
26
27#[derive(Debug)]
29pub struct IntGaugeVec {
30 gauge: GenericGaugeVec<AtomicI64>,
31 pub name: &'static str,
33}
34
35impl IntGaugeVec {
36 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
38 pub fn new(name: &'static str, help: &str, labels: &[&str]) -> Result<Self> {
39 let registry = get_metrics_registry();
40 let opts = get_metric_opts(name, help);
41 let gauge = register_int_gauge_vec_with_registry!(opts, labels, registry)?;
42 Ok(Self { gauge, name })
43 }
44 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
46 pub fn inc(&self, label_vals: &[&str]) {
47 self.gauge
48 .get_metric_with_label_values(label_vals)
49 .unwrap()
50 .inc();
51 }
52 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
54 pub fn dec(&self, label_vals: &[&str]) {
55 self.gauge
56 .get_metric_with_label_values(label_vals)
57 .unwrap()
58 .dec();
59 }
60 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
62 pub fn get(&self, label_vals: &[&str]) -> i64 {
63 self.gauge
64 .get_metric_with_label_values(label_vals)
65 .unwrap()
66 .get()
67 }
68 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
70 pub fn set(&self, label_vals: &[&str], val: i64) {
71 self.gauge
72 .get_metric_with_label_values(label_vals)
73 .unwrap()
74 .set(val);
75 }
76 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
78 pub fn add(&self, label_vals: &[&str], val: i64) {
79 self.gauge
80 .get_metric_with_label_values(label_vals)
81 .unwrap()
82 .add(val);
83 }
84 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
86 pub fn sub(&self, label_vals: &[&str], val: i64) {
87 self.gauge
88 .get_metric_with_label_values(label_vals)
89 .unwrap()
90 .sub(val);
91 }
92}
93
94impl<S: HyperlightMetricOps> GetHyperlightMetric<IntGaugeVec> for S {
95 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
96 fn metric(&self) -> Result<&IntGaugeVec> {
97 let metric = self.get_metric()?;
98 <&HyperlightMetric as TryInto<&IntGaugeVec>>::try_into(metric)
99 }
100}
101
102impl<'a> TryFrom<&'a HyperlightMetric> for &'a IntGaugeVec {
103 type Error = HyperlightError;
104 #[instrument(err(Debug), skip_all, parent = Span::current(), level= "Trace")]
105 fn try_from(metric: &'a HyperlightMetric) -> Result<Self> {
106 match metric {
107 HyperlightMetric::IntGaugeVec(gauge) => Ok(gauge),
108 _ => Err(new_error!("metric is not a IntGaugeVec")),
109 }
110 }
111}
112
113impl From<IntGaugeVec> for HyperlightMetric {
114 #[instrument(skip_all, parent = Span::current(), level= "Trace")]
115 fn from(gauge: IntGaugeVec) -> Self {
116 HyperlightMetric::IntGaugeVec(gauge)
117 }
118}