prometheus_reqwest_remote_write/
lib.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::{collections::HashMap, time::SystemTime};

use prometheus::proto::MetricFamily;
use reqwest::Client;

/// Special label for the name of a metric.
pub const LABEL_NAME: &str = "__name__";
pub const CONTENT_TYPE: &str = "application/x-protobuf";
pub const HEADER_NAME_REMOTE_WRITE_VERSION: &str = "X-Prometheus-Remote-Write-Version";
pub const REMOTE_WRITE_VERSION_01: &str = "0.1.0";
pub const COUNT_SUFFIX: &str = "_count";
pub const SUM_SUFFIX: &str = "_sum";
pub const TOTAL_SUFFIX: &str = "_total";

/// A label.
///
/// .proto:
/// ```protobuf
/// message Label {
///   string name  = 1;
///   string value = 2;
/// }
/// ```
#[derive(prost::Message, Clone, Hash, PartialEq, Eq)]
pub struct Label {
    #[prost(string, tag = "1")]
    pub name: String,
    #[prost(string, tag = "2")]
    pub value: String,
}

/// A sample.
///
/// .proto:
/// ```protobuf
/// message Sample {
///   double value    = 1;
///   int64 timestamp = 2;
/// }
/// ```
#[derive(prost::Message, Clone, PartialEq)]
pub struct Sample {
    #[prost(double, tag = "1")]
    pub value: f64,
    #[prost(int64, tag = "2")]
    pub timestamp: i64,
}

pub enum ExtraLabel {
    LessThan(f64),
    Quantile(f64),
}

/// A time series.
///
/// .proto:
/// ```protobuf
/// message TimeSeries {
///   repeated Label labels   = 1;
///   repeated Sample samples = 2;
/// }
/// ```
#[derive(prost::Message, Clone, PartialEq)]
pub struct TimeSeries {
    #[prost(message, repeated, tag = "1")]
    pub labels: Vec<Label>,
    #[prost(message, repeated, tag = "2")]
    pub samples: Vec<Sample>,
}

impl TimeSeries {
    /// Sort labels by name, and the samples by timestamp.
    ///
    /// Required by the specification.
    pub fn sort_labels_and_samples(&mut self) {
        self.labels.sort_by(|a, b| a.name.cmp(&b.name));
        self.samples.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
    }
}

/// A write request.
///
/// .proto:
/// ```protobuf
/// message WriteRequest {
///   repeated TimeSeries timeseries = 1;
///   // Cortex uses this field to determine the source of the write request.
///   // We reserve it to avoid any compatibility issues.
///   reserved  2;

///   // Prometheus uses this field to send metadata, but this is
///   // omitted from v1 of the spec as it is experimental.
///   reserved  3;
/// }
/// ```
#[derive(prost::Message, Clone, PartialEq)]
pub struct WriteRequest {
    #[prost(message, repeated, tag = "1")]
    pub timeseries: Vec<TimeSeries>,
}

fn get_timestamp() -> i64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .unwrap()
        .as_millis() as i64
}

impl WriteRequest {
    /// Prepare the write request for sending.
    ///
    /// Ensures that the request conforms to the specification.
    /// See https://prometheus.io/docs/concepts/remote_write_spec.
    pub fn sort(&mut self) {
        for series in &mut self.timeseries {
            series.sort_labels_and_samples();
        }
    }

    pub fn sorted(mut self) -> Self {
        self.sort();
        self
    }

    /// Encode this write request as a protobuf message.
    ///
    /// NOTE: The API requires snappy compression, not a raw protobuf message.
    pub fn encode_proto3(self) -> Vec<u8> {
        prost::Message::encode_to_vec(&self.sorted())
    }

    pub fn encode_compressed(self) -> Result<Vec<u8>, snap::Error> {
        snap::raw::Encoder::new().compress_vec(&self.encode_proto3())
    }

    /// Encode Prometheus metric families into a WriteRequest
    pub fn from_metric_families(
        metric_families: Vec<MetricFamily>,
        custom_labels: Option<Vec<(String, String)>>,
    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        let mut timeseries = Vec::new();
        let now = get_timestamp();
        let custom_labels = custom_labels.unwrap_or_default();
        metric_families
            .iter()
            .for_each(|mf| match mf.get_field_type() {
                prometheus::proto::MetricType::GAUGE => {
                    mf.get_metric().iter().for_each(|m| {
                        let mut labels = m
                            .get_label()
                            .iter()
                            .map(|l| (l.get_name().to_string(), l.get_value().to_string()))
                            .collect::<Vec<_>>();
                        labels.push((LABEL_NAME.to_string(), mf.get_name().to_string()));
                        labels.extend_from_slice(&custom_labels);

                        let samples = vec![Sample {
                            value: m.get_gauge().get_value(),
                            timestamp: now,
                        }];

                        timeseries.push(TimeSeries {
                            labels: labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect::<Vec<_>>(),
                            samples,
                        });
                    });
                }
                prometheus::proto::MetricType::COUNTER => {
                    mf.get_metric().iter().for_each(|m| {
                        let mut labels = m
                            .get_label()
                            .iter()
                            .map(|l| (l.get_name().to_string(), l.get_value().to_string()))
                            .collect::<Vec<_>>();
                        labels.push((LABEL_NAME.to_string(), mf.get_name().to_string()));
                        let samples = vec![Sample {
                            value: m.get_counter().get_value(),
                            timestamp: now,
                        }];

                        timeseries.push(TimeSeries {
                            labels: labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect::<Vec<_>>(),
                            samples,
                        });
                    });
                }
                prometheus::proto::MetricType::SUMMARY => {
                    mf.get_metric().iter().for_each(|m| {
                        let mut labels = m
                            .get_label()
                            .iter()
                            .map(|l| (l.get_name().to_string(), l.get_value().to_string()))
                            .collect::<HashMap<String, String>>();
                        labels.insert(LABEL_NAME.to_string(), mf.get_name().to_string());
                        m.get_summary().get_quantile().iter().for_each(|quantile| {
                            let mut our_labels = labels.clone();
                            our_labels.insert(
                                "quantile".to_string(),
                                quantile.get_quantile().to_string(),
                            );
                            let samples = vec![Sample {
                                value: quantile.get_value(),
                                timestamp: now,
                            }];
                            timeseries.push(TimeSeries {
                                labels: our_labels
                                    .iter()
                                    .map(|(k, v)| Label {
                                        name: k.to_string(),
                                        value: v.to_string(),
                                    })
                                    .collect::<Vec<_>>(),
                                samples,
                            });
                        });
                        let mut top_level_labels = labels.clone();
                        top_level_labels.insert(
                            LABEL_NAME.to_string(),
                            format!("{}{}", mf.get_name(), SUM_SUFFIX),
                        );
                        timeseries.push(TimeSeries {
                            samples: vec![Sample {
                                value: m.get_summary().get_sample_sum(),
                                timestamp: now,
                            }],
                            labels: top_level_labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect(),
                        });
                        top_level_labels.insert(
                            LABEL_NAME.to_string(),
                            format!("{}{}", mf.get_name(), COUNT_SUFFIX),
                        );
                        timeseries.push(TimeSeries {
                            samples: vec![Sample {
                                value: m.get_summary().get_sample_count() as f64,
                                timestamp: now,
                            }],
                            labels: top_level_labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect(),
                        });
                    });
                }
                prometheus::proto::MetricType::UNTYPED => {
                    mf.get_metric().iter().for_each(|m| {
                        let mut labels = m
                            .get_label()
                            .iter()
                            .map(|l| (l.get_name().to_string(), l.get_value().to_string()))
                            .collect::<Vec<_>>();
                        labels.push((LABEL_NAME.to_string(), mf.get_name().to_string()));
                        let samples = vec![Sample {
                            value: m.get_untyped().get_value(),
                            timestamp: get_timestamp(),
                        }];

                        timeseries.push(TimeSeries {
                            labels: labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect::<Vec<_>>(),
                            samples,
                        });
                    });
                }
                prometheus::proto::MetricType::HISTOGRAM => {
                    mf.get_metric().iter().for_each(|m| {
                        let mut labels = m
                            .get_label()
                            .iter()
                            .map(|l| (l.get_name().to_string(), l.get_value().to_string()))
                            .collect::<HashMap<String, String>>();
                        labels.insert(LABEL_NAME.to_string(), mf.get_name().to_string());

                        m.get_histogram().get_bucket().iter().for_each(|bucket| {
                            let mut our_labels = labels.clone();
                            our_labels
                                .insert("le".to_string(), bucket.get_upper_bound().to_string());
                            let samples = vec![Sample {
                                value: bucket.get_cumulative_count() as f64,
                                timestamp: now,
                            }];
                            timeseries.push(TimeSeries {
                                labels: our_labels
                                    .iter()
                                    .map(|(k, v)| Label {
                                        name: k.to_string(),
                                        value: v.to_string(),
                                    })
                                    .collect::<Vec<_>>(),
                                samples,
                            });
                        });
                        let mut top_level_labels = labels.clone();
                        top_level_labels.insert(
                            LABEL_NAME.to_string(),
                            format!("{}{}", mf.get_name(), SUM_SUFFIX),
                        );
                        timeseries.push(TimeSeries {
                            samples: vec![Sample {
                                value: m.get_histogram().get_sample_sum(),
                                timestamp: now,
                            }],
                            labels: top_level_labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect(),
                        });
                        top_level_labels.insert(
                            LABEL_NAME.to_string(),
                            format!("{}{}", mf.get_name(), COUNT_SUFFIX),
                        );
                        timeseries.push(TimeSeries {
                            samples: vec![Sample {
                                value: m.get_histogram().get_sample_count() as f64,
                                timestamp: now,
                            }],
                            labels: top_level_labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect(),
                        });
                        top_level_labels
                            .insert(LABEL_NAME.to_string(), mf.get_name().to_string());
                        top_level_labels.insert("le".into(), "+Inf".into());
                        timeseries.push(TimeSeries {
                            samples: vec![Sample {
                                value: m.get_histogram().get_sample_count() as f64,
                                timestamp: now,
                            }],
                            labels: top_level_labels
                                .iter()
                                .map(|(k, v)| Label {
                                    name: k.to_string(),
                                    value: v.to_string(),
                                })
                                .collect(),
                        });
                    });
                }
            });
        timeseries.sort_by(|a, b| {
            let name_a = a.labels.iter().find(|l| l.name == LABEL_NAME).unwrap();
            let name_b = b.labels.iter().find(|l| l.name == LABEL_NAME).unwrap();
            name_a.value.cmp(&name_b.value)
        });
        let s = Self { timeseries };
        Ok(s.sorted())
    }

    pub fn build_http_request(
        self,
        client: Client,
        endpoint: &str,
        user_agent: &str,
    ) -> Result<reqwest::Request, reqwest::Error> {
        client
            .post(endpoint)
            .header(reqwest::header::CONTENT_TYPE, CONTENT_TYPE)
            .header(HEADER_NAME_REMOTE_WRITE_VERSION, REMOTE_WRITE_VERSION_01)
            .header(reqwest::header::CONTENT_ENCODING, "snappy")
            .header(reqwest::header::USER_AGENT, user_agent)
            .body(
                self.encode_compressed()
                    .expect("Failed to compress metrics data"),
            )
            .build()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use prometheus::{histogram_opts, Counter, Gauge, Histogram, Registry};

    #[test]
    pub fn can_encode_counter() {
        let registry = Registry::new();
        let counter_name = "my_counter";
        let help = "an extra description";
        let counter = Counter::new(counter_name, help).unwrap();
        registry.register(Box::new(counter.clone())).unwrap();
        let incremented_by = 5.0;
        counter.inc_by(incremented_by);
        let req = WriteRequest::from_metric_families(registry.gather(), None)
            .expect("Failed to encode counter");
        assert_eq!(req.timeseries.len(), 1);
        let entry = req.timeseries.first().unwrap();
        assert_eq!(entry.labels.len(), 1);
        assert_eq!(
            entry
                .labels
                .iter()
                .find(|l| l.name == LABEL_NAME)
                .unwrap()
                .value,
            counter_name
        );
        assert_eq!(entry.samples.first().unwrap().value, incremented_by);
    }
    #[test]
    pub fn can_encode_gauge() {
        let registry = Registry::new();
        let gauge_name = "my_gauge";
        let help = "an extra description";
        let counter = Gauge::new(gauge_name, help).unwrap();
        registry.register(Box::new(counter.clone())).unwrap();
        let incremented_by = 5.0;
        counter.set(incremented_by);
        let req = WriteRequest::from_metric_families(registry.gather(), None)
            .expect("Failed to encode gauge");
        assert_eq!(req.timeseries.len(), 1);
        let entry = req.timeseries.first().unwrap();
        assert_eq!(entry.labels.len(), 1);
        assert_eq!(
            entry
                .labels
                .iter()
                .find(|l| l.name == LABEL_NAME)
                .unwrap()
                .value,
            gauge_name
        );
        assert_eq!(entry.samples.first().unwrap().value, incremented_by);
    }
    #[test]
    pub fn can_encode_histogram() {
        let registry = Registry::new();
        let histogram_name = "my_histogram";
        let help = "an extra description".to_string();
        let opts = histogram_opts!(histogram_name, help, vec![10.0, 1000.0, 10000.0]);
        let histogram = Histogram::with_opts(opts).unwrap();
        registry.register(Box::new(histogram.clone())).unwrap();
        histogram.observe(5.0);
        histogram.observe(500.0);
        histogram.observe(5000.0);
        histogram.observe(50000.0);
        let req = WriteRequest::from_metric_families(registry.gather(), None)
            .expect("Failed to encode histogram");
        assert_eq!(req.timeseries.len(), 6);
        let bucket_names: Vec<String> = req
            .timeseries
            .clone()
            .into_iter()
            .filter_map(|ts| {
                ts.labels
                    .iter()
                    .find(|l| l.name == "le")
                    .map(|l| l.value.clone())
            })
            .collect();
        assert_eq!(bucket_names, vec!["10", "1000", "10000", "+Inf"]);

        let count_observations = req
            .timeseries
            .clone()
            .iter()
            .find(|l| {
                l.labels.iter().any(|label| {
                    label.name == LABEL_NAME
                        && label.value == format!("{}{}", histogram_name, COUNT_SUFFIX)
                })
            })
            .map(|ts| ts.samples.first().unwrap().value)
            .unwrap();
        assert_eq!(count_observations, 4.0);
        let sum_observation = req
            .timeseries
            .iter()
            .find(|l| {
                l.labels.iter().any(|label| {
                    label.name == LABEL_NAME
                        && label.value == format!("{}{}", histogram_name, SUM_SUFFIX)
                })
            })
            .map(|ts| ts.samples.first().unwrap().value)
            .unwrap();
        assert_eq!(sum_observation, 55505.0)
    }
}