biometrics_prometheus 0.10.0

biometrics_prometheus provides a prometheus-compatible exporter of biometrics.
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
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::os::fd::AsRawFd;
use std::time::{Duration, Instant};
use std::{collections::HashSet, fmt};

use biometrics::{Counter, Gauge, Histogram, Moments, Sensor};
use utf8path::Path;

////////////////////////////////////////////// Options /////////////////////////////////////////////

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Options {
    pub segment_size: usize,
    pub flush_interval: Duration,
    pub prefix: Path<'static>,
}

impl Default for Options {
    fn default() -> Self {
        Self {
            segment_size: 64 * 1048576,
            flush_interval: Duration::from_secs(30),
            prefix: Path::new("biometrics."),
        }
    }
}

////////////////////////////////////////////// Emitter /////////////////////////////////////////////

#[derive(Clone, Debug, Default)]
pub struct SlashMetrics {
    output: Option<String>,
}

impl SlashMetrics {
    pub fn new() -> Self {
        let output = None;
        Self { output }
    }

    pub fn take(mut self) -> String {
        self.output.take().unwrap_or_default()
    }

    fn write_line(&mut self, line: impl AsRef<str>) -> Result<(), std::io::Error> {
        let output = self.output.get_or_insert_with(String::new);
        *output += line.as_ref();
        Ok(())
    }
}

impl biometrics::Emitter for SlashMetrics {
    type Error = std::io::Error;

    fn emit_counter(&mut self, counter: &Counter, now: u64) -> Result<(), std::io::Error> {
        let label = counter.label().replace(".", "_");
        let reading = counter.read();
        self.write_line(format!(
            "# TYPE {label} counter
{label} {reading} {now}\n",
        ))?;
        Ok(())
    }

    fn emit_gauge(&mut self, gauge: &Gauge, now: u64) -> Result<(), std::io::Error> {
        let label = gauge.label().replace(".", "_");
        let reading = gauge.read();
        self.write_line(format!(
            "# TYPE {label} gauge
{label} {reading} {now}\n"
        ))?;
        Ok(())
    }

    fn emit_moments(&mut self, moments: &Moments, now: u64) -> Result<(), std::io::Error> {
        let label = moments.label().replace(".", "_");
        let reading = moments.read();
        self.write_line(format!(
            "# TYPE {label}_count counter
{label}_count {} {now}
# TYPE {label}_mean gauge
{label}_mean {} {now}
# TYPE {label}_variance gauge
{label}_variance {} {now}
# TYPE {label}_skewness gauge
{label}_skewness {} {now}
# TYPE {label}_kurtosis gauge
{label}_kurtosis {} {now}\n",
            reading.n(),
            reading.mean(),
            reading.variance(),
            reading.skewness(),
            reading.kurtosis(),
        ))?;
        Ok(())
    }

    fn emit_histogram(&mut self, histogram: &Histogram, now: u64) -> Result<(), std::io::Error> {
        let label = histogram.label().replace(".", "_");
        self.write_line(format!("# TYPE {label} histogram\n"))?;
        let mut total = 0;
        let mut acc = 0.0;
        for (bucket, count) in histogram.read().iter() {
            total += count;
            acc += bucket * count as f64;
            self.write_line(format!(
                "{label}_bucket{{le=\"{bucket:0.4}\"}} {total} {now}\n"
            ))?;
        }
        self.write_line(format!("{label}_sum {acc} {now}\n"))?;
        self.write_line(format!("{label}_count {total} {now}\n"))?;
        let exceeds_max = histogram.exceeds_max().read();
        self.write_line(format!("{label}_exceeds_max {exceeds_max} {now}\n"))?;
        let is_negative = histogram.is_negative().read();
        self.write_line(format!("{label}_is_negative {is_negative} {now}\n"))?;
        Ok(())
    }
}

////////////////////////////////////////////// Emitter /////////////////////////////////////////////

pub struct Emitter {
    options: Options,
    output: Option<BufWriter<File>>,
    written: usize,
    last_flush: Instant,
    flush_trigger: Option<u64>,
    emitted_types: HashSet<String>,
}

impl Emitter {
    pub fn new(options: Options) -> Self {
        let output = None;
        let written = 0;
        let last_flush = Instant::now();
        let flush_trigger = None;
        let emitted_types = HashSet::new();
        Self {
            options,
            output,
            written,
            last_flush,
            flush_trigger,
            emitted_types,
        }
    }

    pub fn flush(&mut self) -> Result<(), std::io::Error> {
        if let Some(output) = self.output.as_mut() {
            output.flush()?;
        }
        Ok(())
    }

    fn write_line(&mut self, line: impl AsRef<str>, now_millis: u64) -> Result<(), std::io::Error> {
        if let Some(flush_trigger) = self.flush_trigger
            && now_millis > flush_trigger
        {
            self.flush()?;
            self.output.take();
            self.written = 0;
            self.last_flush = Instant::now();
            self.flush_trigger = None;
            self.emitted_types.clear();
        }
        let flush_trigger = self.flush_trigger;
        let last_flush = self.last_flush;
        self.written += line.as_ref().len();
        let written = self.written;
        let output = self.get_output(now_millis)?;
        output.write_all(line.as_ref().as_bytes())?;
        if flush_trigger.is_none()
            && (written > self.options.segment_size
                || last_flush.elapsed() > self.options.flush_interval)
        {
            self.flush_trigger = Some(now_millis);
        }
        Ok(())
    }

    fn get_output(&mut self, now_millis: u64) -> Result<&mut dyn std::io::Write, std::io::Error> {
        if self.output.is_none() {
            let path = self.options.prefix.as_str().to_owned() + &format!("{now_millis}.prom");
            let file = OpenOptions::new().create_new(true).write(true).open(path)?;
            // NOTE(rescrv): l_type,l_whence is 16 bits on some platforms and 32 bits on others.
            // The annotations here are for cross-platform compatibility.
            #[allow(clippy::useless_conversion)]
            #[allow(clippy::unnecessary_cast)]
            let flock = libc::flock {
                l_type: libc::F_WRLCK as i16,
                l_whence: libc::SEEK_SET as i16,
                l_start: 0,
                l_len: 0,
                l_pid: 0,
                #[cfg(target_os = "freebsd")]
                l_sysid: 0,
            };
            // NOTE(rescrv):  This should never fail, as others will only acquire the lock when the
            // file size is non-zero.  Just bail impolitely if it does.
            if unsafe { libc::fcntl(file.as_raw_fd(), libc::F_SETLK, &flock) < 0 } {
                return Err(std::io::Error::last_os_error());
            }
            let output = BufWriter::new(file);
            self.output = Some(output);
        }
        Ok(self.output.as_mut().unwrap())
    }

    fn emit_type_once(
        &mut self,
        label: impl fmt::Display,
        kind: &str,
        now: u64,
    ) -> Result<(), std::io::Error> {
        let label = label.to_string();
        if self.emitted_types.insert(label.clone()) {
            self.write_line(format!("# TYPE {label} {kind}\n"), now)?;
        }
        Ok(())
    }
}

impl biometrics::Emitter for Emitter {
    type Error = std::io::Error;

    fn emit_counter(&mut self, counter: &Counter, now: u64) -> Result<(), std::io::Error> {
        let label = counter.label();
        let reading = counter.read();
        self.emit_type_once(label, "counter", now)?;
        self.write_line(format!("{label} {reading} {now}\n"), now)?;
        Ok(())
    }

    fn emit_gauge(&mut self, gauge: &Gauge, now: u64) -> Result<(), std::io::Error> {
        let label = gauge.label();
        let reading = gauge.read();
        self.emit_type_once(label, "gauge", now)?;
        self.write_line(format!("{label} {reading} {now}\n"), now)?;
        Ok(())
    }

    fn emit_moments(&mut self, moments: &Moments, now: u64) -> Result<(), std::io::Error> {
        let label = moments.label();
        let reading = moments.read();
        self.emit_type_once(format!("{label}_count"), "counter", now)?;
        self.emit_type_once(format!("{label}_mean"), "gauge", now)?;
        self.emit_type_once(format!("{label}_variance"), "gauge", now)?;
        self.emit_type_once(format!("{label}_skewness"), "gauge", now)?;
        self.emit_type_once(format!("{label}_kurtosis"), "gauge", now)?;
        self.write_line(
            format!(
                "{label}_count {} {now}
{label}_mean {} {now}
{label}_variance {} {now}
{label}_skewness {} {now}
{label}_kurtosis {} {now}
",
                reading.n(),
                reading.mean(),
                reading.variance(),
                reading.skewness(),
                reading.kurtosis(),
            ),
            now,
        )?;
        Ok(())
    }

    fn emit_histogram(&mut self, histogram: &Histogram, now: u64) -> Result<(), std::io::Error> {
        let label = histogram.label();
        self.emit_type_once(label, "histogram", now)?;
        self.emit_type_once(format!("{label}_sum"), "gauge", now)?;
        self.emit_type_once(format!("{label}_count"), "counter", now)?;
        self.emit_type_once(format!("{label}_exceeds_max"), "gauge", now)?;
        self.emit_type_once(format!("{label}_is_negative"), "gauge", now)?;
        let mut total = 0;
        let mut acc = 0.0;
        for (bucket, count) in histogram.read().iter() {
            total += count;
            acc += bucket * count as f64;
            self.write_line(
                format!("{label}_bucket{{le=\"{bucket:0.4}\"}} {total} {now}\n"),
                now,
            )?;
        }
        self.write_line(format!("{label}_sum {acc} {now}\n"), now)?;
        self.write_line(format!("{label}_count {total} {now}\n"), now)?;
        let exceeds_max = histogram.exceeds_max().read();
        self.write_line(format!("{label}_exceeds_max {exceeds_max} {now}\n"), now)?;
        let is_negative = histogram.is_negative().read();
        self.write_line(format!("{label}_is_negative {is_negative} {now}\n"), now)?;
        Ok(())
    }
}

////////////////////////////////////////////// Reader //////////////////////////////////////////////

#[derive(Debug)]
pub struct Reader(Path<'static>, File);

impl Reader {
    pub fn open(path: Path) -> Result<Self, std::io::Error> {
        Self::_open(path, libc::F_SETLKW)
    }

    fn _open(path: Path, cmd: libc::c_int) -> Result<Self, std::io::Error> {
        let path = path.into_owned();
        let file = OpenOptions::new().read(true).open(&path)?;
        // NOTE(rescrv): l_type,l_whence is 16 bits on some platforms and 32 bits on others.
        // The annotations here are for cross-platform compatibility.
        #[allow(clippy::useless_conversion)]
        #[allow(clippy::unnecessary_cast)]
        let flock = libc::flock {
            l_type: libc::F_RDLCK as i16,
            l_whence: libc::SEEK_SET as i16,
            l_start: 0,
            l_len: 0,
            l_pid: 0,
            #[cfg(target_os = "freebsd")]
            l_sysid: 0,
        };
        // NOTE(rescrv):  This should never fail, as others will only acquire the lock when the
        // file size is non-zero.  Just bail impolitely if it does.
        if unsafe { libc::fcntl(file.as_raw_fd(), cmd, &flock) < 0 } {
            return Err(std::io::Error::last_os_error());
        }
        Ok(Self(path, file))
    }

    pub fn path(&self) -> &Path<'static> {
        &self.0
    }

    pub fn unlink(self) -> Result<(), std::io::Error> {
        std::fs::remove_file(self.path())
    }
}

impl std::ops::Deref for Reader {
    type Target = File;

    fn deref(&self) -> &Self::Target {
        &self.1
    }
}

////////////////////////////////////////////// Watcher /////////////////////////////////////////////

pub struct Watcher {
    path: Path<'static>,
}

impl Watcher {
    pub fn new(path: Path) -> Self {
        let path = path.into_owned();
        Self { path }
    }

    /// Ingest the least recently modified file in the directory, subject to locking rules.  If the
    /// lock cannot be acquired, the function will pass over the file.  Once one call is made to
    /// `process`, the function returns Ok(()).  If the `process` function does not use
    /// `reader.unlink`, the next call to ingest_one will process the same file with high
    /// probability.
    pub fn ingest_one<E: From<std::io::Error>>(
        &self,
        mut process: impl FnMut(Reader) -> Result<(), E>,
    ) -> Result<(), E> {
        let mut path_and_timestamp = vec![];
        for dirent in std::fs::read_dir(&self.path)? {
            let dirent = dirent?;
            let metadata = dirent.metadata()?;
            let path = Path::try_from(dirent.path()).map_err(|_| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid path: not UTF-8")
            })?;
            path_and_timestamp.push((metadata.modified()?, path));
        }
        path_and_timestamp.sort_by_key(|(timestamp, _)| *timestamp);
        for (_, path) in path_and_timestamp.into_iter() {
            let reader = match Reader::_open(path, libc::F_SETLK) {
                Ok(reader) => reader,
                Err(err) => {
                    if err.kind() == std::io::ErrorKind::WouldBlock {
                        continue;
                    }
                    return Err(err.into());
                }
            };
            return process(reader);
        }
        Ok(())
    }
}

/////////////////////////////////////////////// tests //////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use std::fs::{read_to_string, remove_file};

    use biometrics::Emitter as EmitterTrait;

    use super::*;

    #[test]
    fn slash_metrics() {
        static COUNTER: Counter = Counter::new("foo");
        let collector = biometrics::Collector::new();
        collector.register_counter(&COUNTER);
        let mut slash_metrics = SlashMetrics::new();
        let _ = collector.emit(&mut slash_metrics, 42);
        assert_eq!(
            "# TYPE biometrics_collector_register_counter counter
biometrics_collector_register_counter 11 42
# TYPE biometrics_collector_register_gauge counter
biometrics_collector_register_gauge 0 42
# TYPE biometrics_collector_register_moments counter
biometrics_collector_register_moments 0 42
# TYPE biometrics_collector_register_histogram counter
biometrics_collector_register_histogram 0 42
# TYPE biometrics_collector_emit_counter counter
biometrics_collector_emit_counter 4 42
# TYPE biometrics_collector_emit_gauge counter
biometrics_collector_emit_gauge 0 42
# TYPE biometrics_collector_emit_moments counter
biometrics_collector_emit_moments 0 42
# TYPE biometrics_collector_emit_histogram counter
biometrics_collector_emit_histogram 0 42
# TYPE biometrics_collector_emit_failure counter
biometrics_collector_emit_failure 0 42
# TYPE biometrics_collector_time_failure counter
biometrics_collector_time_failure 0 42
# TYPE foo counter
foo 0 42
",
            slash_metrics.take()
        );
    }

    #[test]
    fn emitter() {
        static MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
        // SAFETY(rescrv):  Mutex poisoning.
        let _guard = MUTEX.lock().unwrap();
        if Path::from("tmp.foo.42.prom").exists() {
            remove_file("tmp.foo.42.prom").unwrap();
        }
        let mut emitter = Emitter::new(Options {
            segment_size: 1024,
            flush_interval: Duration::from_secs(1),
            prefix: Path::new("tmp.foo."),
        });
        emitter.emit_counter(&Counter::new("foo"), 42).unwrap();
        drop(emitter);
    }

    #[test]
    fn emitter_type_once() {
        static MUTEX: std::sync::Mutex<()> = std::sync::Mutex::new(());
        // SAFETY(rescrv):  Mutex poisoning.
        let _guard = MUTEX.lock().unwrap();
        if Path::from("tmp.type.7.prom").exists() {
            remove_file("tmp.type.7.prom").unwrap();
        }
        let mut emitter = Emitter::new(Options {
            segment_size: 1024,
            flush_interval: Duration::from_secs(1),
            prefix: Path::new("tmp.type."),
        });
        emitter.emit_counter(&Counter::new("foo"), 7).unwrap();
        emitter.emit_counter(&Counter::new("foo"), 7).unwrap();
        drop(emitter);
        let output = read_to_string("tmp.type.7.prom").unwrap();
        assert_eq!(1, output.matches("# TYPE foo counter\n").count());
        remove_file("tmp.type.7.prom").unwrap();
    }

    #[test]
    fn reader() {
        let _reader = Reader::open(Path::new("README.md")).unwrap();
    }

    #[test]
    fn watcher() {
        emitter();
        let watcher = Watcher::new(Path::new("."));
        let mut watched = vec![];
        watcher
            .ingest_one(|reader| {
                watched.push(reader);
                Ok::<(), std::io::Error>(())
            })
            .unwrap();
        let found = watched[0].0.clone();
        assert!(
            found.basename().as_str().starts_with("README.md")
                || found.basename().as_str().starts_with("Cargo.toml")
                || found.basename().as_str().starts_with("k8s.metrics")
                || found.basename().as_str().starts_with("src")
                || found.basename().as_str().starts_with("tmp.foo.")
                || found.basename().as_str().starts_with(".gitignore"),
            "found: {found:?}",
        );
    }
}