risingwave_common_metrics/
relabeled_metric.rs1use prometheus::core::{Collector, MetricVec, MetricVecBuilder};
16use prometheus::{HistogramVec, IntCounterVec};
17
18use crate::{
19 LabelGuardedHistogramVec, LabelGuardedIntCounterVec, LabelGuardedIntGaugeVec,
20 LabelGuardedMetric, LabelGuardedMetricVec, MetricLevel,
21};
22
23#[derive(Clone, Debug)]
44pub struct RelabeledMetricVec<M> {
45 relabel_threshold: MetricLevel,
46 metric_level: MetricLevel,
47 metric: M,
48
49 relabel_num: usize,
55}
56
57impl<M> RelabeledMetricVec<M> {
58 pub fn with_metric_level(
59 metric_level: MetricLevel,
60 metric: M,
61 relabel_threshold: MetricLevel,
62 ) -> Self {
63 Self {
64 relabel_threshold,
65 metric_level,
66 metric,
67 relabel_num: usize::MAX,
68 }
69 }
70
71 pub fn with_metric_level_relabel_n(
72 metric_level: MetricLevel,
73 metric: M,
74 relabel_threshold: MetricLevel,
75 relabel_num: usize,
76 ) -> Self {
77 Self {
78 relabel_threshold,
79 metric_level,
80 metric,
81 relabel_num,
82 }
83 }
84}
85
86#[easy_ext::ext(MetricVecRelabelExt)]
87impl<M> M
88where
89 M: Sized,
90{
91 pub fn relabel(
93 self,
94 metric_level: MetricLevel,
95 relabel_threshold: MetricLevel,
96 ) -> RelabeledMetricVec<M> {
97 RelabeledMetricVec::with_metric_level(metric_level, self, relabel_threshold)
98 }
99
100 pub fn relabel_n(
102 self,
103 metric_level: MetricLevel,
104 relabel_threshold: MetricLevel,
105 relabel_num: usize,
106 ) -> RelabeledMetricVec<M> {
107 RelabeledMetricVec::with_metric_level_relabel_n(
108 metric_level,
109 self,
110 relabel_threshold,
111 relabel_num,
112 )
113 }
114
115 pub fn relabel_debug_1(self, relabel_threshold: MetricLevel) -> RelabeledMetricVec<M> {
118 RelabeledMetricVec::with_metric_level_relabel_n(
119 MetricLevel::Debug,
120 self,
121 relabel_threshold,
122 1,
123 )
124 }
125}
126
127impl<T: MetricVecBuilder> RelabeledMetricVec<MetricVec<T>> {
128 pub fn with_label_values<V: AsRef<str> + std::fmt::Debug>(&self, vals: &[V]) -> T::M {
129 if self.metric_level > self.relabel_threshold {
130 let mut relabeled_vals = vals
132 .iter()
133 .map(|v| v.as_ref().to_owned())
134 .collect::<Vec<_>>();
135 for label in relabeled_vals.iter_mut().take(self.relabel_num) {
136 *label = String::new();
137 }
138 return self.metric.with_label_values(&relabeled_vals);
139 }
140 self.metric.with_label_values(vals)
141 }
142}
143
144impl<T: MetricVecBuilder> RelabeledMetricVec<LabelGuardedMetricVec<T>> {
145 pub fn with_guarded_label_values<V: AsRef<str> + std::fmt::Debug>(
146 &self,
147 vals: &[V],
148 ) -> LabelGuardedMetric<T::M> {
149 if self.metric_level > self.relabel_threshold {
150 let mut relabeled_vals = vals
152 .iter()
153 .map(|v| v.as_ref().to_owned())
154 .collect::<Vec<_>>();
155 for label in relabeled_vals.iter_mut().take(self.relabel_num) {
156 *label = String::new();
157 }
158 return self.metric.with_guarded_label_values(&relabeled_vals);
159 }
160 self.metric.with_guarded_label_values(vals)
161 }
162}
163
164impl<T: Collector> Collector for RelabeledMetricVec<T> {
165 fn desc(&self) -> Vec<&prometheus::core::Desc> {
166 self.metric.desc()
167 }
168
169 fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
170 self.metric.collect()
171 }
172}
173
174pub type RelabeledCounterVec = RelabeledMetricVec<IntCounterVec>;
175pub type RelabeledHistogramVec = RelabeledMetricVec<HistogramVec>;
176
177pub type RelabeledGuardedHistogramVec = RelabeledMetricVec<LabelGuardedHistogramVec>;
178pub type RelabeledGuardedIntCounterVec = RelabeledMetricVec<LabelGuardedIntCounterVec>;
179
180pub type RelabeledGuardedIntGaugeVec = RelabeledMetricVec<LabelGuardedIntGaugeVec>;