Skip to main content

prometheus/
proto_ext.rs

1use protobuf::{EnumOrUnknown, MessageField};
2
3use crate::proto::{
4    Bucket, Counter, Gauge, Histogram, LabelPair, Metric, MetricFamily, MetricType, Quantile,
5    Summary,
6};
7
8impl Metric {
9    /// Creates a new metric with the specified label pairs.
10    pub fn from_label(label: Vec<LabelPair>) -> Self {
11        Metric {
12            label,
13            ..Default::default()
14        }
15    }
16
17    /// Creates a new metric with the specified gauge value.
18    pub fn from_gauge(gauge: Gauge) -> Self {
19        Metric {
20            gauge: gauge.into(),
21            ..Default::default()
22        }
23    }
24
25    #[deprecated(since = "0.14.0", note = "Please use `.timestamp_ms()` instead")]
26    /// Returns the timestamp of this metric.
27    pub fn get_timestamp_ms(&self) -> i64 {
28        self.timestamp_ms()
29    }
30
31    /// Returns the summary of this metric.
32    pub fn get_summary(&self) -> &MessageField<Summary> {
33        &self.summary
34    }
35
36    /// Sets the summary of this metric to the specified summary.
37    pub fn set_summary(&mut self, summary: Summary) {
38        self.summary = summary.into();
39    }
40
41    /// Returns the value of the counter for this metric.
42    pub fn get_counter(&self) -> &MessageField<Counter> {
43        &self.counter
44    }
45
46    /// Sets the counter of this metric to the specified counter.
47    pub fn set_counter(&mut self, counter: Counter) {
48        self.counter = counter.into();
49    }
50
51    /// Returns all label pairs associated with this metric.
52    pub fn get_label(&self) -> &[LabelPair] {
53        &self.label
54    }
55
56    /// Sets the label pairs associated with this metric.
57    pub fn set_label(&mut self, label: Vec<LabelPair>) {
58        self.label = label;
59    }
60
61    /// Returns all label pairs associated with ownership.
62    pub fn take_label(&mut self) -> Vec<LabelPair> {
63        std::mem::take(&mut self.label)
64    }
65
66    /// Returns the gauge of this metric.
67    pub fn get_gauge(&self) -> &MessageField<Gauge> {
68        &self.gauge
69    }
70
71    /// Sets the gauge of this metric to the specified gauge.
72    pub fn set_gauge(&mut self, gauge: Gauge) {
73        self.gauge = gauge.into();
74    }
75
76    /// Returns the histogram of this metric.
77    pub fn get_histogram(&self) -> &MessageField<Histogram> {
78        &self.histogram
79    }
80
81    /// Sets the histogram of this metric to the specified histogram.
82    pub fn set_histogram(&mut self, histogram: Histogram) {
83        self.histogram = histogram.into();
84    }
85}
86
87impl MetricFamily {
88    #[deprecated(since = "0.14.0", note = "Please use `.name()` instead")]
89    /// Returns the name of this metric family.
90    pub fn get_name(&self) -> &str {
91        self.name()
92    }
93
94    #[deprecated(since = "0.14.0", note = "Please use `.help()` instead")]
95    /// Returns the help text of this metric family.
96    pub fn get_help(&self) -> &str {
97        self.help()
98    }
99
100    /// Sets the metric for this metric family (replaces any existing metrics).
101    pub fn set_metric(&mut self, metric: Vec<Metric>) {
102        self.metric = metric;
103    }
104
105    /// Returns the type of this metric family.
106    pub fn get_field_type(&self) -> MetricType {
107        self.type_()
108    }
109
110    /// Sets the type of this metric family.
111    pub fn set_field_type(&mut self, t: MetricType) {
112        self.type_ = t.into();
113    }
114
115    /// Returns all metrics in this metric family.
116    pub fn get_metric(&self) -> &[Metric] {
117        &self.metric
118    }
119
120    /// Returns all metrics in this metric family mutably.
121    pub fn mut_metric(&mut self) -> &mut Vec<Metric> {
122        &mut self.metric
123    }
124
125    /// Returns all metrics in this metric family with taking ownership.
126    pub fn take_metric(&mut self) -> Vec<Metric> {
127        std::mem::take(&mut self.metric)
128    }
129}
130
131impl Summary {
132    /// Sets the quantiles for this summary.
133    pub fn set_quantile(&mut self, quantiles: Vec<Quantile>) {
134        self.quantile = quantiles;
135    }
136
137    /// Returns the quantiles of this summary.
138    pub fn get_quantile(&self) -> &[Quantile] {
139        &self.quantile
140    }
141
142    #[deprecated(since = "0.14.0", note = "Please use `.sample_count()` instead")]
143    /// Returns the sample count of this summary.
144    pub fn get_sample_count(&self) -> u64 {
145        self.sample_count()
146    }
147
148    #[deprecated(since = "0.14.0", note = "Please use `.sample_sum()` instead")]
149    /// Returns the sample sum of this summary.
150    pub fn get_sample_sum(&self) -> f64 {
151        self.sample_sum()
152    }
153}
154
155impl Quantile {
156    #[deprecated(since = "0.14.0", note = "Please use `.quantile()` instead")]
157    /// Returns the quantile of this quantile.
158    pub fn get_quantile(&self) -> f64 {
159        self.quantile()
160    }
161
162    #[deprecated(since = "0.14.0", note = "Please use `.value()` instead")]
163    /// Returns the value of this quantile.
164    pub fn get_value(&self) -> f64 {
165        self.value()
166    }
167}
168
169pub trait MessageFieldExt {
170    /// Returns the value of the wrapped gauge.
171    #[allow(dead_code)]
172    fn get_value(&self) -> f64;
173}
174
175impl MessageFieldExt for MessageField<Gauge> {
176    fn get_value(&self) -> f64 {
177        self.value()
178    }
179}
180
181impl MessageFieldExt for MessageField<Counter> {
182    fn get_value(&self) -> f64 {
183        self.value()
184    }
185}
186
187impl Histogram {
188    /// Returns the sample count of this histogram.
189    pub fn get_sample_count(&self) -> u64 {
190        self.sample_count.unwrap_or_default()
191    }
192
193    /// Returns the sample sum of this histogram.
194    pub fn get_sample_sum(&self) -> f64 {
195        self.sample_sum.unwrap_or_default()
196    }
197
198    /// Returns all buckets in this histogram.
199    pub fn get_bucket(&self) -> &[Bucket] {
200        &self.bucket
201    }
202
203    /// Sets the buckets of this histogram.
204    pub fn set_bucket(&mut self, bucket: Vec<Bucket>) {
205        self.bucket = bucket;
206    }
207}
208
209impl Bucket {
210    #[deprecated(since = "0.14.0", note = "Please use `.cumulative_count()` instead")]
211    /// Returns the cumulative count of this bucket.
212    pub fn get_cumulative_count(&self) -> u64 {
213        self.cumulative_count()
214    }
215
216    #[deprecated(since = "0.14.0", note = "Please use `.upper_bound()` instead")]
217    /// Returns the upper bound of this bucket.
218    pub fn get_upper_bound(&self) -> f64 {
219        self.upper_bound()
220    }
221}
222
223impl LabelPair {
224    #[deprecated(since = "0.14.0", note = "Please use `.value()` instead")]
225    /// Returns the value of this label pair.
226    pub fn get_value(&self) -> &str {
227        self.value()
228    }
229
230    #[deprecated(since = "0.14.0", note = "Please use `.name()` instead")]
231    /// Returns the name of this label pair.
232    pub fn get_name(&self) -> &str {
233        self.name()
234    }
235}
236
237impl From<Counter> for MessageField<Counter> {
238    fn from(value: Counter) -> Self {
239        MessageField::some(value)
240    }
241}
242
243impl From<Gauge> for MessageField<Gauge> {
244    fn from(value: Gauge) -> Self {
245        MessageField::some(value)
246    }
247}
248
249impl From<Histogram> for MessageField<Histogram> {
250    fn from(value: Histogram) -> Self {
251        MessageField::some(value)
252    }
253}
254
255impl From<Summary> for MessageField<Summary> {
256    fn from(value: Summary) -> Self {
257        MessageField::some(value)
258    }
259}
260
261impl From<MetricType> for Option<EnumOrUnknown<MetricType>> {
262    fn from(value: MetricType) -> Self {
263        Some(EnumOrUnknown::from(value))
264    }
265}