measured 0.0.25

A better way to measure your application statistics
Documentation
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
//! Prometheus Text based exporter

use std::{
    convert::Infallible,
    io::{self, Write},
};

use bytes::{BufMut, Bytes, BytesMut};
use memchr::memchr3_iter;

use crate::{
    label::{LabelGroup, LabelGroupVisitor, LabelName, LabelValue, LabelVisitor},
    metric::{
        MetricEncoding,
        counter::CounterState,
        gauge::{FloatGaugeState, GaugeState},
        group::{Encoding, MetricValue},
        histogram::{HistogramState, Thresholds},
        name::{Bucket, Count, MetricNameEncoder, Sum},
    },
};

/// The prometheus text encoder helper
pub struct TextEncoder<W> {
    state: State,
    /// The inner writer for this text encoder.
    pub writer: W,
}

#[derive(Clone, Copy, Debug, PartialEq)]
enum State {
    Info,
    Metrics,
}

/// Prometheus only supports these 5 types of metrics
#[derive(Clone, Copy, Debug)]
pub enum MetricType {
    /// Corresponds to [`Counter`](crate::Counter)
    Counter,
    /// Corresponds to [`Histogram`](crate::Histogram)
    Histogram,
    /// Corresponds to [`Gauge`](crate::Gauge)
    Gauge,
    /// Not currently supported
    Summary,
    /// Not currently supported
    Untyped,
}

impl<W: Write> Encoding for TextEncoder<W> {
    type Err = std::io::Error;

    const MIME_TYPE: &'static str = "text/plain; version=0.0.4";

    /// Write the help line for a metric
    fn write_help(
        &mut self,
        name: impl MetricNameEncoder,
        help: &str,
    ) -> Result<(), std::io::Error> {
        if self.state == State::Metrics {
            self.write_line()?;
        }
        self.state = State::Info;

        self.writer.write_all(b"# HELP ")?;
        name.encode_utf8(&mut self.writer)?;
        self.writer.write_all(b" ")?;
        self.writer.write_all(help.as_bytes())?;
        self.writer.write_all(b"\n")?;
        Ok(())
    }
}

impl<W: Write> TextEncoder<W> {
    /// Create a new text encoder.
    ///
    /// This should ideally be cached and re-used between collections to reduce re-allocating
    pub fn new(w: W) -> Self {
        Self {
            state: State::Info,
            writer: w,
        }
    }

    /// Finish the text encoding and extract the bytes to send in a HTTP response.
    pub fn flush(&mut self) -> std::io::Result<()> {
        self.state = State::Info;
        self.writer.flush()
    }

    fn write_line(&mut self) -> std::io::Result<()> {
        self.writer.write_all(b"\n")
    }

    /// Write the type line for a metric
    pub fn write_type(
        &mut self,
        name: &impl MetricNameEncoder,
        typ: MetricType,
    ) -> Result<(), std::io::Error> {
        if self.state == State::Metrics {
            self.write_line()?;
        }
        self.state = State::Info;

        self.writer.write_all(b"# TYPE ")?;
        name.encode_utf8(&mut self.writer)?;
        match typ {
            MetricType::Counter => self.writer.write_all(b" counter\n"),
            MetricType::Histogram => self.writer.write_all(b" histogram\n"),
            MetricType::Gauge => self.writer.write_all(b" gauge\n"),
            MetricType::Summary => self.writer.write_all(b" summary\n"),
            MetricType::Untyped => self.writer.write_all(b" untyped\n"),
        }
    }

    /// Write the metric data
    fn write_metric_value(
        &mut self,
        name: impl MetricNameEncoder,
        labels: impl LabelGroup,
        value: MetricValue,
    ) -> Result<(), std::io::Error> {
        struct Visitor<'a, W> {
            writer: &'a mut W,
        }
        impl<W: Write> LabelVisitor for Visitor<'_, W> {
            type Output = Result<(), std::io::Error>;
            fn write_int(self, x: i64) -> Result<(), std::io::Error> {
                self.write_str(itoa::Buffer::new().format(x))
            }

            fn write_float(self, x: f64) -> Result<(), std::io::Error> {
                if x.is_infinite() {
                    if x.is_sign_positive() {
                        self.write_str("+Inf")
                    } else {
                        self.write_str("-Inf")
                    }
                } else if x.is_nan() {
                    self.write_str("NaN")
                } else {
                    self.write_str(ryu::Buffer::new().format(x))
                }
            }

            fn write_str(self, x: &str) -> Result<(), std::io::Error> {
                self.writer.write_all(b"=\"")?;
                write_label_str_value(x, &mut *self.writer)?;
                self.writer.write_all(b"\"")?;
                Ok(())
            }
        }

        struct GroupVisitor<'a, W> {
            first: bool,
            writer: &'a mut W,
        }
        impl<W: Write> LabelGroupVisitor for GroupVisitor<'_, W> {
            type Output = Result<(), std::io::Error>;
            fn write_value(
                &mut self,
                name: &LabelName,
                x: &impl LabelValue,
            ) -> Result<(), std::io::Error> {
                if self.first {
                    self.first = false;
                    self.writer.write_all(b"{")?;
                } else {
                    self.writer.write_all(b",")?;
                }
                self.writer.write_all(name.as_str().as_bytes())?;
                x.visit(Visitor {
                    writer: self.writer,
                })
            }
        }

        self.state = State::Metrics;
        name.encode_utf8(&mut self.writer)?;

        let mut visitor = GroupVisitor {
            first: true,
            writer: &mut self.writer,
        };
        labels.visit_values(&mut visitor);
        if !visitor.first {
            self.writer.write_all(b"}")?;
        }
        self.writer.write_all(b" ")?;
        match value {
            MetricValue::Int(x) => self
                .writer
                .write_all(itoa::Buffer::new().format(x).as_bytes())?,
            MetricValue::Float(x) => self
                .writer
                .write_all(ryu::Buffer::new().format(x).as_bytes())?,
        }
        self.writer.write_all(b"\n")?;
        Ok(())
    }
}

impl<W: Write, const N: usize> MetricEncoding<TextEncoder<W>> for HistogramState<N> {
    fn write_type(
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_type(&name, MetricType::Histogram)
    }
    fn collect_into(
        &self,
        metadata: &Thresholds<N>,
        labels: impl LabelGroup,
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        struct F64(f64);
        impl LabelValue for F64 {
            fn visit<V: LabelVisitor>(&self, v: V) -> V::Output {
                v.write_float(self.0)
            }
        }

        struct HistogramLabelLe {
            le: f64,
        }

        impl LabelGroup for HistogramLabelLe {
            fn visit_values(&self, v: &mut impl LabelGroupVisitor) {
                const LE: &LabelName = LabelName::from_str("le");
                v.write_value(LE, &F64(self.le));
            }
        }

        let (buckets, inf, sum) = self.inner.write().sample();
        let mut val = 0;

        #[allow(clippy::needless_range_loop)]
        for i in 0..N {
            let le = metadata.get()[i];
            val += buckets[i];
            enc.write_metric_value(
                name.by_ref().with_suffix(Bucket),
                labels.by_ref().compose_with(HistogramLabelLe { le }),
                MetricValue::Int(val as i64),
            )?;
        }
        let count = val + inf;
        enc.write_metric_value(
            name.by_ref().with_suffix(Bucket),
            labels
                .by_ref()
                .compose_with(HistogramLabelLe { le: f64::INFINITY }),
            MetricValue::Int(count as i64),
        )?;
        enc.write_metric_value(
            name.by_ref().with_suffix(Sum),
            labels.by_ref(),
            MetricValue::Float(sum),
        )?;
        enc.write_metric_value(
            name.by_ref().with_suffix(Count),
            labels,
            MetricValue::Int(count as i64),
        )?;
        Ok(())
    }
}

impl<W: Write> MetricEncoding<TextEncoder<W>> for CounterState {
    fn write_type(
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_type(&name, MetricType::Counter)
    }
    fn collect_into(
        &self,
        _m: &(),
        labels: impl LabelGroup,
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_metric_value(
            &name,
            labels,
            MetricValue::Int(self.count.load(core::sync::atomic::Ordering::Relaxed) as i64),
        )
    }
}

impl<W: Write> MetricEncoding<TextEncoder<W>> for GaugeState {
    fn write_type(
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_type(&name, MetricType::Gauge)
    }
    fn collect_into(
        &self,
        _m: &(),
        labels: impl LabelGroup,
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_metric_value(
            &name,
            labels,
            MetricValue::Int(self.count.load(core::sync::atomic::Ordering::Relaxed)),
        )
    }
}

impl<W: Write> MetricEncoding<TextEncoder<W>> for FloatGaugeState {
    fn write_type(
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_type(&name, MetricType::Gauge)
    }
    fn collect_into(
        &self,
        _m: &(),
        labels: impl LabelGroup,
        name: impl MetricNameEncoder,
        enc: &mut TextEncoder<W>,
    ) -> Result<(), std::io::Error> {
        enc.write_metric_value(&name, labels, MetricValue::Float(self.count.get()))
    }
}

/// The prometheus text encoder helper
pub struct BufferedTextEncoder {
    inner: TextEncoder<BytesWriter>,
}

impl Default for BufferedTextEncoder {
    fn default() -> Self {
        Self::new()
    }
}

trait Unreachable<T> {
    fn unreachable(self) -> T;
}

impl<T, E: std::fmt::Debug> Unreachable<T> for Result<T, E> {
    fn unreachable(self) -> T {
        match self {
            Ok(t) => t,
            Err(e) => unreachable!("BytesMut should not error when writing: {e:?}"),
        }
    }
}

impl Encoding for BufferedTextEncoder {
    type Err = Infallible;

    const MIME_TYPE: &'static str = TextEncoder::<BytesWriter>::MIME_TYPE;

    /// Write the help line for a metric
    fn write_help(&mut self, name: impl MetricNameEncoder, help: &str) -> Result<(), Infallible> {
        self.inner.write_help(name, help).unreachable();
        Ok(())
    }
}

impl BufferedTextEncoder {
    /// Create a new text encoder.
    ///
    /// This should ideally be cached and re-used between collections to reduce re-allocating
    pub fn new() -> Self {
        Self {
            inner: TextEncoder::new(BytesWriter {
                buf: BytesMut::new(),
            }),
        }
    }

    /// Finish the text encoding and extract the bytes to send in a HTTP response.
    pub fn finish(&mut self) -> Bytes {
        self.inner.flush().unreachable();
        self.inner.writer.buf.split().freeze()
    }
}

impl<T: MetricEncoding<TextEncoder<BytesWriter>>> MetricEncoding<BufferedTextEncoder> for T {
    fn write_type(
        name: impl MetricNameEncoder,
        enc: &mut BufferedTextEncoder,
    ) -> Result<(), Infallible> {
        Self::write_type(name, &mut enc.inner).unreachable();
        Ok(())
    }
    fn collect_into(
        &self,
        metadata: &T::Metadata,
        labels: impl LabelGroup,
        name: impl MetricNameEncoder,
        enc: &mut BufferedTextEncoder,
    ) -> Result<(), Infallible> {
        self.collect_into(metadata, labels, name, &mut enc.inner)
            .unreachable();
        Ok(())
    }
}

pub(crate) fn write_label_str_value(s: &str, b: &mut impl Write) -> io::Result<()> {
    let mut i = 0;
    for j in memchr3_iter(b'\\', b'"', b'\n', s.as_bytes()) {
        b.write_all(&s.as_bytes()[i..j])?;
        match s.as_bytes()[j] {
            b'\\' => b.write_all(b"\\\\")?,
            b'"' => b.write_all(b"\\\"")?,
            b'\n' => b.write_all(b"\\n")?,
            _ => unreachable!(),
        }
        i = j + 1;
    }
    b.write_all(&s.as_bytes()[i..])
}

struct BytesWriter {
    buf: BytesMut,
}

impl Write for BytesWriter {
    #[inline]
    fn write(&mut self, src: &[u8]) -> io::Result<usize> {
        self.write_all(src)?;
        Ok(src.len())
    }

    #[inline]
    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
        self.buf.put(buf);
        Ok(())
    }

    #[inline]
    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use bytes::{BufMut, BytesMut};

    use crate::{
        CounterVec, Histogram,
        label::StaticLabelSet,
        metric::{
            MetricFamilyEncoding,
            group::Encoding,
            histogram::Thresholds,
            name::{MetricName, Total},
        },
    };

    use super::{BufferedTextEncoder, write_label_str_value};

    #[test]
    fn write_encoded_str() {
        let mut b = BytesMut::new().writer();
        write_label_str_value(
            r#"Hello \ "World"
This is on a new line"#,
            &mut b,
        )
        .unwrap();

        assert_eq!(
            b.into_inner(),
            r#"Hello \\ \"World\"\nThis is on a new line"#
        );
    }

    #[derive(Clone, Copy, PartialEq, Debug, measured_derive::LabelGroup)]
    #[label(crate = crate, set = RequestLabelSet)]
    struct RequestLabels {
        method: Method,
        code: StatusCode,
    }

    #[derive(Clone, Copy, PartialEq, Debug, measured_derive::FixedCardinalityLabel)]
    #[label(crate = crate, rename_all = "snake_case")]
    enum Method {
        Post,
        Get,
    }

    #[derive(Clone, Copy, PartialEq, Debug, measured_derive::FixedCardinalityLabel)]
    #[label(crate = crate)]
    enum StatusCode {
        Ok = 200,
        BadRequest = 400,
    }

    #[test]
    fn text_encoding() {
        let requests = CounterVec::with_label_set(RequestLabelSet {
            code: StaticLabelSet::new(),
            method: StaticLabelSet::new(),
        });

        let labels = RequestLabels {
            method: Method::Post,
            code: StatusCode::Ok,
        };
        requests.inc_by(labels, 1027);

        let labels = RequestLabels {
            method: Method::Get,
            code: StatusCode::BadRequest,
        };
        requests.inc_by(labels, 3);

        let mut encoder = BufferedTextEncoder::default();

        let name = MetricName::from_str("http_request").with_suffix(Total);
        encoder
            .write_help(&name, "The total number of HTTP requests.")
            .unwrap();
        requests.collect_family_into(name, &mut encoder).unwrap();

        let s = String::from_utf8(encoder.finish().to_vec()).unwrap();
        assert_eq!(
            s,
            r#"# HELP http_request_total The total number of HTTP requests.
# TYPE http_request_total counter
http_request_total{method="post",code="200"} 1027
http_request_total{method="get",code="400"} 3
"#
        );
    }

    #[test]
    fn text_histogram() {
        let thresholds = Thresholds::<8>::exponential_buckets(0.1, 2.0);
        let histogram = Histogram::with_metadata(thresholds);

        histogram.get_metric().observe(0.7);
        histogram.get_metric().observe(2.5);
        histogram.get_metric().observe(1.2);
        histogram.get_metric().observe(8.0);

        let mut encoder = BufferedTextEncoder::default();

        let name = MetricName::from_str("http_request_duration_seconds");
        encoder
            .write_help(name, "A histogram of the request duration.")
            .unwrap();
        histogram.collect_family_into(name, &mut encoder).unwrap();

        let s = String::from_utf8(encoder.finish().to_vec()).unwrap();
        assert_eq!(
            s,
            r#"# HELP http_request_duration_seconds A histogram of the request duration.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.1"} 0
http_request_duration_seconds_bucket{le="0.2"} 0
http_request_duration_seconds_bucket{le="0.4"} 0
http_request_duration_seconds_bucket{le="0.8"} 1
http_request_duration_seconds_bucket{le="1.6"} 2
http_request_duration_seconds_bucket{le="3.2"} 3
http_request_duration_seconds_bucket{le="6.4"} 3
http_request_duration_seconds_bucket{le="12.8"} 4
http_request_duration_seconds_bucket{le="+Inf"} 4
http_request_duration_seconds_sum 12.4
http_request_duration_seconds_count 4
"#
        );
    }

    /// See <https://github.com/conradludgate/measured/issues/8>
    #[test]
    fn text_encoding_rename() {
        #[derive(Clone, Copy, PartialEq, Debug, measured_derive::LabelGroup)]
        #[label(crate = crate, set = RequestLabelSet)]
        struct RequestLabels {
            method: Method,
            code: StatusCode,
        }

        #[derive(Clone, Copy, PartialEq, Debug, measured_derive::FixedCardinalityLabel)]
        #[label(crate = crate)]
        enum StatusCode {
            #[label(rename = "ok")]
            Ok = 200,

            #[label(rename = "badrequest")]
            BadRequest = 400,
        }

        let requests = CounterVec::with_label_set(RequestLabelSet {
            code: StaticLabelSet::new(),
            method: StaticLabelSet::new(),
        });

        let labels = RequestLabels {
            method: Method::Post,
            code: StatusCode::Ok,
        };
        requests.inc_by(labels, 1027);

        let labels = RequestLabels {
            method: Method::Get,
            code: StatusCode::BadRequest,
        };
        requests.inc_by(labels, 3);

        let mut encoder = BufferedTextEncoder::default();

        let name = MetricName::from_str("http_request").with_suffix(Total);
        encoder
            .write_help(&name, "The total number of HTTP requests.")
            .unwrap();
        requests.collect_family_into(name, &mut encoder).unwrap();

        let s = String::from_utf8(encoder.finish().to_vec()).unwrap();
        assert_eq!(
            s,
            r#"# HELP http_request_total The total number of HTTP requests.
# TYPE http_request_total counter
http_request_total{method="post",code="ok"} 1027
http_request_total{method="get",code="badrequest"} 3
"#
        );
    }
}