risingwave_common_metrics/relabeled_metric.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use prometheus::core::{Collector, MetricVec, MetricVecBuilder};
use prometheus::{HistogramVec, IntCounterVec};
use crate::{
LabelGuardedHistogramVec, LabelGuardedIntCounterVec, LabelGuardedIntGaugeVec,
LabelGuardedMetric, LabelGuardedMetricVec, MetricLevel,
};
/// For all `Relabeled*Vec` below,
/// - when `metric_level` <= `relabel_threshold`, they behaves exactly the same as their inner
/// metric.
/// - when `metric_level` > `relabel_threshold`, all their input label values are rewrite to "" when
/// calling `with_label_values`. That's means the metric vec is aggregated into a single metric.
///
///
/// These wrapper classes add a `metric_level` field to corresponding metric.
/// We could have use one single struct to represent all `MetricVec<T: MetricVecBuilder>`, rather
/// than specializing them one by one. However, that's undoable because prometheus crate doesn't
/// export `MetricVecBuilder` implementation like `HistogramVecBuilder`.
#[derive(Clone, Debug)]
pub struct RelabeledMetricVec<M> {
relabel_threshold: MetricLevel,
metric_level: MetricLevel,
metric: M,
/// The first `relabel_num` labels will be relabeled to empty string
///
/// For example, if `relabel_num` is 1, and the input labels are `["actor_id",
/// "fragment_id", "table_id"]`, when threshold is reached, the label values will be
/// `["", "<original_fragment_id>", "<original_table_id>"]`.
relabel_num: usize,
}
impl<M> RelabeledMetricVec<M> {
pub fn with_metric_level(
metric_level: MetricLevel,
metric: M,
relabel_threshold: MetricLevel,
) -> Self {
Self {
relabel_threshold,
metric_level,
metric,
relabel_num: usize::MAX,
}
}
pub fn with_metric_level_relabel_n(
metric_level: MetricLevel,
metric: M,
relabel_threshold: MetricLevel,
relabel_num: usize,
) -> Self {
Self {
relabel_threshold,
metric_level,
metric,
relabel_num,
}
}
}
#[easy_ext::ext(MetricVecRelabelExt)]
impl<M> M
where
M: Sized,
{
/// Equivalent to [`RelabeledMetricVec::with_metric_level`].
pub fn relabel(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M> {
RelabeledMetricVec::with_metric_level(metric_level, self, relabel_threshold)
}
/// Equivalent to [`RelabeledMetricVec::with_metric_level_relabel_n`].
pub fn relabel_n(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
relabel_num: usize,
) -> RelabeledMetricVec<M> {
RelabeledMetricVec::with_metric_level_relabel_n(
metric_level,
self,
relabel_threshold,
relabel_num,
)
}
/// Equivalent to [`RelabeledMetricVec::with_metric_level_relabel_n`] with `metric_level` set to
/// `MetricLevel::Debug` and `relabel_num` set to 1.
pub fn relabel_debug_1(self, relabel_threshold: MetricLevel) -> RelabeledMetricVec<M> {
RelabeledMetricVec::with_metric_level_relabel_n(
MetricLevel::Debug,
self,
relabel_threshold,
1,
)
}
}
impl<T: MetricVecBuilder> RelabeledMetricVec<MetricVec<T>> {
pub fn with_label_values(&self, vals: &[&str]) -> T::M {
if self.metric_level > self.relabel_threshold {
// relabel first n labels to empty string
let mut relabeled_vals = vals.to_vec();
for label in relabeled_vals.iter_mut().take(self.relabel_num) {
*label = "";
}
return self.metric.with_label_values(&relabeled_vals);
}
self.metric.with_label_values(vals)
}
}
impl<T: MetricVecBuilder, const N: usize> RelabeledMetricVec<LabelGuardedMetricVec<T, N>> {
pub fn with_guarded_label_values(&self, vals: &[&str; N]) -> LabelGuardedMetric<T::M, N> {
if self.metric_level > self.relabel_threshold {
// relabel first n labels to empty string
let mut relabeled_vals = *vals;
for label in relabeled_vals.iter_mut().take(self.relabel_num) {
*label = "";
}
return self.metric.with_guarded_label_values(&relabeled_vals);
}
self.metric.with_guarded_label_values(vals)
}
}
impl<T: Collector> Collector for RelabeledMetricVec<T> {
fn desc(&self) -> Vec<&prometheus::core::Desc> {
self.metric.desc()
}
fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
self.metric.collect()
}
}
pub type RelabeledCounterVec = RelabeledMetricVec<IntCounterVec>;
pub type RelabeledHistogramVec = RelabeledMetricVec<HistogramVec>;
pub type RelabeledGuardedHistogramVec<const N: usize> =
RelabeledMetricVec<LabelGuardedHistogramVec<N>>;
pub type RelabeledGuardedIntCounterVec<const N: usize> =
RelabeledMetricVec<LabelGuardedIntCounterVec<N>>;
pub type RelabeledGuardedIntGaugeVec<const N: usize> =
RelabeledMetricVec<LabelGuardedIntGaugeVec<N>>;