risingwave_common_metrics/
guarded_metrics.rs

1// Copyright 2025 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::any::type_name;
16use std::collections::{HashMap, HashSet};
17use std::fmt::{Debug, Formatter};
18use std::ops::Deref;
19use std::sync::Arc;
20
21use parking_lot::Mutex;
22use prometheus::core::{
23    Atomic, AtomicF64, AtomicI64, AtomicU64, Collector, Desc, GenericCounter, GenericLocalCounter,
24    MetricVec, MetricVecBuilder,
25};
26use prometheus::local::{LocalHistogram, LocalIntCounter};
27use prometheus::proto::MetricFamily;
28use prometheus::{Gauge, Histogram, IntCounter, IntGauge};
29use thiserror_ext::AsReport;
30use tracing::warn;
31
32#[macro_export]
33macro_rules! register_guarded_histogram_vec_with_registry {
34    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
35        $crate::register_guarded_histogram_vec_with_registry! {
36            {prometheus::histogram_opts!($NAME, $HELP)},
37            $LABELS_NAMES,
38            $REGISTRY
39        }
40    }};
41    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $BUCKETS:expr, $REGISTRY:expr $(,)?) => {{
42        $crate::register_guarded_histogram_vec_with_registry! {
43            {prometheus::histogram_opts!($NAME, $HELP, $BUCKETS)},
44            $LABELS_NAMES,
45            $REGISTRY
46        }
47    }};
48    ($HOPTS:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
49        let inner = prometheus::HistogramVec::new($HOPTS, $LABELS_NAMES);
50        inner.and_then(|inner| {
51            let inner = $crate::__extract_histogram_builder(inner);
52            let label_guarded = $crate::LabelGuardedHistogramVec::new(inner, { $LABELS_NAMES });
53            let result = ($REGISTRY).register(Box::new(label_guarded.clone()));
54            result.map(move |()| label_guarded)
55        })
56    }};
57}
58
59#[macro_export]
60macro_rules! register_guarded_gauge_vec_with_registry {
61    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
62        let inner = prometheus::GaugeVec::new(prometheus::opts!($NAME, $HELP), $LABELS_NAMES);
63        inner.and_then(|inner| {
64            let inner = $crate::__extract_gauge_builder(inner);
65            let label_guarded = $crate::LabelGuardedGaugeVec::new(inner, { $LABELS_NAMES });
66            let result = ($REGISTRY).register(Box::new(label_guarded.clone()));
67            result.map(move |()| label_guarded)
68        })
69    }};
70}
71
72#[macro_export]
73macro_rules! register_guarded_int_gauge_vec_with_registry {
74    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
75        let inner = prometheus::IntGaugeVec::new(prometheus::opts!($NAME, $HELP), $LABELS_NAMES);
76        inner.and_then(|inner| {
77            let inner = $crate::__extract_gauge_builder(inner);
78            let label_guarded = $crate::LabelGuardedIntGaugeVec::new(inner, { $LABELS_NAMES });
79            let result = ($REGISTRY).register(Box::new(label_guarded.clone()));
80            result.map(move |()| label_guarded)
81        })
82    }};
83}
84
85#[macro_export]
86macro_rules! register_guarded_uint_gauge_vec_with_registry {
87    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
88        let inner = prometheus::core::GenericGaugeVec::<prometheus::core::AtomicU64>::new(
89            prometheus::opts!($NAME, $HELP),
90            $LABELS_NAMES,
91        );
92        inner.and_then(|inner| {
93            let inner = $crate::__extract_gauge_builder(inner);
94            let label_guarded = $crate::LabelGuardedUintGaugeVec::new(inner, { $LABELS_NAMES });
95            let result = ($REGISTRY).register(Box::new(label_guarded.clone()));
96            result.map(move |()| label_guarded)
97        })
98    }};
99}
100
101#[macro_export]
102macro_rules! register_guarded_int_counter_vec_with_registry {
103    ($NAME:expr, $HELP:expr, $LABELS_NAMES:expr, $REGISTRY:expr $(,)?) => {{
104        let inner = prometheus::IntCounterVec::new(prometheus::opts!($NAME, $HELP), $LABELS_NAMES);
105        inner.and_then(|inner| {
106            let inner = $crate::__extract_counter_builder(inner);
107            let label_guarded = $crate::LabelGuardedIntCounterVec::new(inner, { $LABELS_NAMES });
108            let result = ($REGISTRY).register(Box::new(label_guarded.clone()));
109            result.map(move |()| label_guarded)
110        })
111    }};
112}
113
114// put TAITs in a separate module to avoid "non-defining opaque type use in defining scope"
115mod tait {
116    use prometheus::core::{
117        Atomic, GenericCounter, GenericCounterVec, GenericGauge, GenericGaugeVec, MetricVec,
118        MetricVecBuilder,
119    };
120    use prometheus::{Histogram, HistogramVec};
121
122    pub type VecBuilderOfCounter<P: Atomic> = impl MetricVecBuilder<M = GenericCounter<P>>;
123    pub type VecBuilderOfGauge<P: Atomic> = impl MetricVecBuilder<M = GenericGauge<P>>;
124    pub type VecBuilderOfHistogram = impl MetricVecBuilder<M = Histogram>;
125
126    pub fn __extract_counter_builder<P: Atomic>(
127        vec: GenericCounterVec<P>,
128    ) -> MetricVec<VecBuilderOfCounter<P>> {
129        vec
130    }
131
132    pub fn __extract_gauge_builder<P: Atomic>(
133        vec: GenericGaugeVec<P>,
134    ) -> MetricVec<VecBuilderOfGauge<P>> {
135        vec
136    }
137
138    pub fn __extract_histogram_builder(vec: HistogramVec) -> MetricVec<VecBuilderOfHistogram> {
139        vec
140    }
141}
142pub use tait::*;
143
144use crate::UintGauge;
145
146pub type LabelGuardedHistogramVec = LabelGuardedMetricVec<VecBuilderOfHistogram>;
147pub type LabelGuardedIntCounterVec = LabelGuardedMetricVec<VecBuilderOfCounter<AtomicU64>>;
148pub type LabelGuardedIntGaugeVec = LabelGuardedMetricVec<VecBuilderOfGauge<AtomicI64>>;
149pub type LabelGuardedUintGaugeVec = LabelGuardedMetricVec<VecBuilderOfGauge<AtomicU64>>;
150pub type LabelGuardedGaugeVec = LabelGuardedMetricVec<VecBuilderOfGauge<AtomicF64>>;
151
152pub type LabelGuardedHistogram = LabelGuardedMetric<Histogram>;
153pub type LabelGuardedIntCounter = LabelGuardedMetric<IntCounter>;
154pub type LabelGuardedIntGauge = LabelGuardedMetric<IntGauge>;
155pub type LabelGuardedUintGauge = LabelGuardedMetric<UintGauge>;
156pub type LabelGuardedGauge = LabelGuardedMetric<Gauge>;
157
158pub type LabelGuardedLocalHistogram = LabelGuardedMetric<LocalHistogram>;
159pub type LabelGuardedLocalIntCounter = LabelGuardedMetric<LocalIntCounter>;
160
161fn gen_test_label<const N: usize>() -> [&'static str; N] {
162    const TEST_LABELS: [&str; 5] = ["test1", "test2", "test3", "test4", "test5"];
163    (0..N)
164        .map(|i| TEST_LABELS[i])
165        .collect::<Vec<_>>()
166        .try_into()
167        .unwrap()
168}
169
170#[derive(Default)]
171struct LabelGuardedMetricsInfo {
172    labeled_metrics_count: HashMap<Box<[String]>, usize>,
173    uncollected_removed_labels: HashSet<Box<[String]>>,
174}
175
176impl LabelGuardedMetricsInfo {
177    fn register_new_label<V: AsRef<str>>(mutex: &Arc<Mutex<Self>>, labels: &[V]) -> LabelGuard {
178        let mut guard = mutex.lock();
179        let label_string = labels
180            .iter()
181            .map(|label| label.as_ref().to_owned())
182            .collect::<Vec<_>>()
183            .into_boxed_slice();
184        guard.uncollected_removed_labels.remove(&label_string);
185        *guard
186            .labeled_metrics_count
187            .entry(label_string.clone())
188            .or_insert(0) += 1;
189        LabelGuard {
190            labels: label_string,
191            info: mutex.clone(),
192        }
193    }
194}
195
196/// An RAII metrics vec with labels.
197///
198/// `LabelGuardedMetricVec` enhances the [`MetricVec`] to ensure the set of labels to be
199/// correctly removed from the Prometheus client once being dropped. This is useful for metrics
200/// that are associated with an object that can be dropped, such as streaming jobs, fragments,
201/// actors, batch tasks, etc.
202///
203/// When a set labels is dropped, it will record it in the `uncollected_removed_labels` set.
204/// Once the metrics has been collected, it will finally remove the metrics of the labels.
205///
206/// See also [`LabelGuardedMetricsInfo`] and [`LabelGuard::drop`].
207///
208/// # Arguments
209///
210/// * `T` - The type of the raw metrics vec.
211/// * `N` - The number of labels.
212#[derive(Clone)]
213pub struct LabelGuardedMetricVec<T: MetricVecBuilder> {
214    inner: MetricVec<T>,
215    info: Arc<Mutex<LabelGuardedMetricsInfo>>,
216    labels: Box<[&'static str]>,
217}
218
219impl<T: MetricVecBuilder> Debug for LabelGuardedMetricVec<T> {
220    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
221        f.debug_struct(format!("LabelGuardedMetricVec<{}>", type_name::<T>()).as_str())
222            .field("label", &self.labels)
223            .finish()
224    }
225}
226
227impl<T: MetricVecBuilder> Collector for LabelGuardedMetricVec<T> {
228    fn desc(&self) -> Vec<&Desc> {
229        self.inner.desc()
230    }
231
232    fn collect(&self) -> Vec<MetricFamily> {
233        let mut guard = self.info.lock();
234        let ret = self.inner.collect();
235        for labels in guard.uncollected_removed_labels.drain() {
236            if let Err(e) = self.inner.remove_label_values(&labels) {
237                warn!(
238                    error = %e.as_report(),
239                    "err when delete metrics of {:?} of labels {:?}",
240                    self.inner.desc().first().expect("should have desc").fq_name,
241                    self.labels,
242                );
243            }
244        }
245        ret
246    }
247}
248
249impl<T: MetricVecBuilder> LabelGuardedMetricVec<T> {
250    pub fn new(inner: MetricVec<T>, labels: &[&'static str]) -> Self {
251        Self {
252            inner,
253            info: Default::default(),
254            labels: labels.to_vec().into_boxed_slice(),
255        }
256    }
257
258    /// This is similar to the `with_label_values` of the raw metrics vec.
259    /// We need to pay special attention that, unless for some special purpose,
260    /// we should not drop the returned `LabelGuardedMetric` immediately after
261    /// using it, such as `metrics.with_guarded_label_values(...).inc();`,
262    /// because after dropped the label will be regarded as not used any more,
263    /// and the internal raw metrics will be removed and reset.
264    ///
265    /// Instead, we should store the returned `LabelGuardedMetric` in a scope with longer
266    /// lifetime so that the labels can be regarded as being used in its whole life scope.
267    /// This is also the recommended way to use the raw metrics vec.
268    pub fn with_guarded_label_values<V: AsRef<str> + std::fmt::Debug>(
269        &self,
270        labels: &[V],
271    ) -> LabelGuardedMetric<T::M> {
272        let guard = LabelGuardedMetricsInfo::register_new_label(&self.info, labels);
273        let inner = self.inner.with_label_values(labels);
274        LabelGuardedMetric {
275            inner,
276            _guard: Arc::new(guard),
277        }
278    }
279
280    pub fn with_test_label<const N: usize>(&self) -> LabelGuardedMetric<T::M> {
281        let labels = gen_test_label::<N>();
282        self.with_guarded_label_values(&labels)
283    }
284}
285
286impl LabelGuardedIntCounterVec {
287    pub fn test_int_counter_vec<const N: usize>() -> Self {
288        let registry = prometheus::Registry::new();
289        let labels = gen_test_label::<N>();
290        register_guarded_int_counter_vec_with_registry!("test", "test", &labels, &registry).unwrap()
291    }
292}
293
294impl LabelGuardedIntGaugeVec {
295    pub fn test_int_gauge_vec<const N: usize>() -> Self {
296        let registry = prometheus::Registry::new();
297        let labels = gen_test_label::<N>();
298        register_guarded_int_gauge_vec_with_registry!("test", "test", &labels, &registry).unwrap()
299    }
300}
301
302impl LabelGuardedGaugeVec {
303    pub fn test_gauge_vec<const N: usize>() -> Self {
304        let registry = prometheus::Registry::new();
305        let labels = gen_test_label::<N>();
306        register_guarded_gauge_vec_with_registry!("test", "test", &labels, &registry).unwrap()
307    }
308}
309
310impl LabelGuardedHistogramVec {
311    pub fn test_histogram_vec<const N: usize>() -> Self {
312        let registry = prometheus::Registry::new();
313        let labels = gen_test_label::<N>();
314        register_guarded_histogram_vec_with_registry!("test", "test", &labels, &registry).unwrap()
315    }
316}
317
318#[derive(Clone)]
319struct LabelGuard {
320    labels: Box<[String]>,
321    info: Arc<Mutex<LabelGuardedMetricsInfo>>,
322}
323
324impl Drop for LabelGuard {
325    fn drop(&mut self) {
326        let mut guard = self.info.lock();
327        let count = guard.labeled_metrics_count.get_mut(&self.labels).expect(
328            "should exist because the current existing dropping one means the count is not zero",
329        );
330        *count -= 1;
331        if *count == 0 {
332            guard
333                .labeled_metrics_count
334                .remove(&self.labels)
335                .expect("should exist");
336            guard.uncollected_removed_labels.insert(self.labels.clone());
337        }
338    }
339}
340
341#[derive(Clone)]
342pub struct LabelGuardedMetric<T> {
343    inner: T,
344    _guard: Arc<LabelGuard>,
345}
346
347impl<T> Debug for LabelGuardedMetric<T> {
348    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
349        f.debug_struct("LabelGuardedMetric").finish()
350    }
351}
352
353impl<T> Deref for LabelGuardedMetric<T> {
354    type Target = T;
355
356    fn deref(&self) -> &Self::Target {
357        &self.inner
358    }
359}
360
361impl LabelGuardedHistogram {
362    pub fn test_histogram<const N: usize>() -> Self {
363        LabelGuardedHistogramVec::test_histogram_vec::<N>().with_test_label::<N>()
364    }
365}
366
367impl LabelGuardedIntCounter {
368    pub fn test_int_counter<const N: usize>() -> Self {
369        LabelGuardedIntCounterVec::test_int_counter_vec::<N>().with_test_label::<N>()
370    }
371}
372
373impl LabelGuardedIntGauge {
374    pub fn test_int_gauge<const N: usize>() -> Self {
375        LabelGuardedIntGaugeVec::test_int_gauge_vec::<N>().with_test_label::<N>()
376    }
377}
378
379impl LabelGuardedGauge {
380    pub fn test_gauge<const N: usize>() -> Self {
381        LabelGuardedGaugeVec::test_gauge_vec::<N>().with_test_label::<N>()
382    }
383}
384
385pub trait MetricWithLocal {
386    type Local;
387    fn local(&self) -> Self::Local;
388}
389
390impl MetricWithLocal for Histogram {
391    type Local = LocalHistogram;
392
393    fn local(&self) -> Self::Local {
394        self.local()
395    }
396}
397
398impl<P: Atomic> MetricWithLocal for GenericCounter<P> {
399    type Local = GenericLocalCounter<P>;
400
401    fn local(&self) -> Self::Local {
402        self.local()
403    }
404}
405
406impl<T: MetricWithLocal> LabelGuardedMetric<T> {
407    pub fn local(&self) -> LabelGuardedMetric<T::Local> {
408        LabelGuardedMetric {
409            inner: self.inner.local(),
410            _guard: self._guard.clone(),
411        }
412    }
413}
414
415#[cfg(test)]
416mod tests {
417    use prometheus::core::Collector;
418
419    use crate::LabelGuardedIntCounterVec;
420
421    #[test]
422    fn test_label_guarded_metrics_drop() {
423        let vec = LabelGuardedIntCounterVec::test_int_counter_vec::<3>();
424        let m1_1 = vec.with_guarded_label_values(&["1", "2", "3"]);
425        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
426        let m1_2 = vec.with_guarded_label_values(&["1", "2", "3"]);
427        let m1_3 = m1_2.clone();
428        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
429        let m2 = vec.with_guarded_label_values(&["2", "2", "3"]);
430        assert_eq!(2, vec.collect().pop().unwrap().get_metric().len());
431        drop(m1_3);
432        assert_eq!(2, vec.collect().pop().unwrap().get_metric().len());
433        assert_eq!(2, vec.collect().pop().unwrap().get_metric().len());
434        drop(m2);
435        assert_eq!(2, vec.collect().pop().unwrap().get_metric().len());
436        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
437        drop(m1_1);
438        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
439        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
440        drop(m1_2);
441        assert_eq!(1, vec.collect().pop().unwrap().get_metric().len());
442        assert_eq!(0, vec.collect().pop().unwrap().get_metric().len());
443    }
444}