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 pub fn from_label(label: Vec<LabelPair>) -> Self {
11 Metric {
12 label,
13 ..Default::default()
14 }
15 }
16
17 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 pub fn get_timestamp_ms(&self) -> i64 {
28 self.timestamp_ms()
29 }
30
31 pub fn get_summary(&self) -> &MessageField<Summary> {
33 &self.summary
34 }
35
36 pub fn set_summary(&mut self, summary: Summary) {
38 self.summary = summary.into();
39 }
40
41 pub fn get_counter(&self) -> &MessageField<Counter> {
43 &self.counter
44 }
45
46 pub fn set_counter(&mut self, counter: Counter) {
48 self.counter = counter.into();
49 }
50
51 pub fn get_label(&self) -> &[LabelPair] {
53 &self.label
54 }
55
56 pub fn set_label(&mut self, label: Vec<LabelPair>) {
58 self.label = label;
59 }
60
61 pub fn take_label(&mut self) -> Vec<LabelPair> {
63 std::mem::take(&mut self.label)
64 }
65
66 pub fn get_gauge(&self) -> &MessageField<Gauge> {
68 &self.gauge
69 }
70
71 pub fn set_gauge(&mut self, gauge: Gauge) {
73 self.gauge = gauge.into();
74 }
75
76 pub fn get_histogram(&self) -> &MessageField<Histogram> {
78 &self.histogram
79 }
80
81 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 pub fn get_name(&self) -> &str {
91 self.name()
92 }
93
94 #[deprecated(since = "0.14.0", note = "Please use `.help()` instead")]
95 pub fn get_help(&self) -> &str {
97 self.help()
98 }
99
100 pub fn set_metric(&mut self, metric: Vec<Metric>) {
102 self.metric = metric;
103 }
104
105 pub fn get_field_type(&self) -> MetricType {
107 self.type_()
108 }
109
110 pub fn set_field_type(&mut self, t: MetricType) {
112 self.type_ = t.into();
113 }
114
115 pub fn get_metric(&self) -> &[Metric] {
117 &self.metric
118 }
119
120 pub fn mut_metric(&mut self) -> &mut Vec<Metric> {
122 &mut self.metric
123 }
124
125 pub fn take_metric(&mut self) -> Vec<Metric> {
127 std::mem::take(&mut self.metric)
128 }
129}
130
131impl Summary {
132 pub fn set_quantile(&mut self, quantiles: Vec<Quantile>) {
134 self.quantile = quantiles;
135 }
136
137 pub fn get_quantile(&self) -> &[Quantile] {
139 &self.quantile
140 }
141
142 #[deprecated(since = "0.14.0", note = "Please use `.sample_count()` instead")]
143 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 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 pub fn get_quantile(&self) -> f64 {
159 self.quantile()
160 }
161
162 #[deprecated(since = "0.14.0", note = "Please use `.value()` instead")]
163 pub fn get_value(&self) -> f64 {
165 self.value()
166 }
167}
168
169pub trait MessageFieldExt {
170 #[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 pub fn get_sample_count(&self) -> u64 {
190 self.sample_count.unwrap_or_default()
191 }
192
193 pub fn get_sample_sum(&self) -> f64 {
195 self.sample_sum.unwrap_or_default()
196 }
197
198 pub fn get_bucket(&self) -> &[Bucket] {
200 &self.bucket
201 }
202
203 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 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 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 pub fn get_value(&self) -> &str {
227 self.value()
228 }
229
230 #[deprecated(since = "0.14.0", note = "Please use `.name()` instead")]
231 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}