hyperlight_host/metrics/
int_gauge_vec.rs

1/*
2Copyright 2024 The Hyperlight Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17use 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/// A list of gauges, each backed by an `AtomicI64`
28#[derive(Debug)]
29pub struct IntGaugeVec {
30    gauge: GenericGaugeVec<AtomicI64>,
31    /// The name of the gauge vec
32    pub name: &'static str,
33}
34
35impl IntGaugeVec {
36    /// Creates a new gauge vec and registers it with the metric registry
37    #[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    /// Increments a gauge by 1
45    #[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    /// Decrements a gauge by 1
53    #[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    /// Gets the value of a gauge
61    #[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    /// Resets a gauge
69    #[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    /// Adds a value to a gauge
77    #[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    /// Subtracts a value from a gauge
85    #[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}