fibre_stats 0.9.0

Metrics for applications and libraries: counters, gauges, timers and states defined once with metric!, aggregated per second in process and handed to a sink in batches.
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
//! Metric definitions and the handles that record through them.
//!
//! A definition is written once with [`metric!`](crate::metric!) and holds the
//! name, kind, unit, description and allowed tags. The handle it produces is
//! the only way to record that metric, so the facts and the recording cannot
//! drift apart. Every definition also registers itself, so [`registry`] lists it
//! without anything being called.

use std::time::Duration;

use crate::collect::{PollGauge, PollState, PollTotal};
use crate::recorder::{Observation, Recorder, Sample, global};
use crate::tag::{Name, Tags};
use crate::time::Stopwatch;
use crate::value::{NumberValue, StateValue};

/// What a metric measures. Decides which samples a series accepts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Kind {
  /// A delta, summed over a window.
  Counter,
  /// A level at a moment. The last one in a window is kept.
  Gauge,
  /// A duration, kept as a histogram per window.
  Timer,
  /// A boolean, a character or an enum label. The last one in a window is kept.
  State,
}

impl Kind {
  pub fn as_str(&self) -> &'static str {
    match self {
      Kind::Counter => "counter",
      Kind::Gauge => "gauge",
      Kind::Timer => "timer",
      Kind::State => "state",
    }
  }
}

impl std::fmt::Display for Kind {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(self.as_str())
  }
}

/// The unit of a metric's values. A counter is `Count` and a timer is
/// `Seconds` unless the definition says otherwise.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Unit {
  None,
  Count,
  Seconds,
  Bytes,
  Percent,
  Ratio,
  Celsius,
  Hertz,
  Custom(&'static str),
}

impl Unit {
  pub fn as_str(&self) -> &'static str {
    match self {
      Unit::None => "",
      Unit::Count => "count",
      Unit::Seconds => "seconds",
      Unit::Bytes => "bytes",
      Unit::Percent => "percent",
      Unit::Ratio => "ratio",
      Unit::Celsius => "celsius",
      Unit::Hertz => "hertz",
      Unit::Custom(s) => s,
    }
  }
}

/// One attribute from a `metric!` definition.
#[derive(Debug, Clone, Copy)]
pub enum MetricAttr {
  Unit(Unit),
  Tags(&'static [&'static str]),
  Resolution(Duration),
}

/// The facts about one metric.
#[derive(Debug)]
pub struct Metric {
  pub name: &'static str,
  pub kind: Kind,
  pub unit: Unit,
  /// The doc comment lines, as written.
  pub description: &'static [&'static str],
  /// The tag names a recording may carry.
  pub tags: &'static [&'static str],
  /// The resolution exact samples are kept at, from `#[high_res("...")]`.
  pub resolution: Option<Duration>,
}

impl Metric {
  pub const fn new(
    name: &'static str,
    kind: Kind,
    description: &'static [&'static str],
    attrs: &'static [MetricAttr],
  ) -> Metric {
    let mut unit = match kind {
      Kind::Counter => Unit::Count,
      Kind::Timer => Unit::Seconds,
      Kind::Gauge | Kind::State => Unit::None,
    };
    let mut tags: &'static [&'static str] = &[];
    let mut resolution: Option<Duration> = None;
    let mut i = 0;
    while i < attrs.len() {
      match attrs[i] {
        MetricAttr::Unit(u) => unit = u,
        MetricAttr::Tags(t) => tags = t,
        MetricAttr::Resolution(r) => resolution = Some(r),
      }
      i += 1;
    }
    Metric {
      name,
      kind,
      unit,
      description,
      tags,
      resolution,
    }
  }

  /// The description with each line trimmed and joined by newlines.
  pub fn description_text(&self) -> String {
    let mut out = String::new();
    for line in self.description {
      let line = line.trim();
      if !out.is_empty() {
        out.push('\n');
      }
      out.push_str(line);
    }
    out
  }

  pub fn allows(&self, tag: &str) -> bool {
    self.tags.contains(&tag)
  }

  fn check_tags(&self, tags: &Tags) {
    if cfg!(debug_assertions) {
      for tag in tags {
        assert!(
          self.allows(&tag.name),
          "metric `{}` does not declare tag `{}`; add it to #[tags(...)]",
          self.name,
          tag.name
        );
      }
    }
  }

  fn recording_resolution(&self) -> Option<Duration> {
    if cfg!(feature = "high_res") { self.resolution } else { None }
  }

  fn observation(&self, recorder: &dyn Recorder, tags: Tags, sample: Sample) -> Observation {
    self.check_tags(&tags);
    Observation {
      name: Name::Borrowed(self.name),
      kind: self.kind,
      tags,
      at_ns: recorder.now_ns(),
      sample,
      resolution: self.recording_resolution(),
    }
  }
}

/// Parses `"1ms"`, `"250us"`, `"10ns"` or `"1s"` at compile time. Panics on
/// anything else, which fails the build.
pub const fn parse_duration(s: &str) -> Duration {
  let b = s.as_bytes();
  let mut i = 0;
  let mut n: u64 = 0;
  while i < b.len() && b[i] >= b'0' && b[i] <= b'9' {
    n = n * 10 + (b[i] - b'0') as u64;
    i += 1;
  }
  if i == 0 {
    panic!("a duration starts with a number: \"1ms\", \"250us\", \"10ns\" or \"1s\"");
  }
  let rest = b.len() - i;
  if rest == 2 && b[i] == b'n' && b[i + 1] == b's' {
    return Duration::from_nanos(n);
  }
  if rest == 2 && b[i] == b'u' && b[i + 1] == b's' {
    return Duration::from_micros(n);
  }
  if rest == 2 && b[i] == b'm' && b[i + 1] == b's' {
    return Duration::from_millis(n);
  }
  if rest == 1 && b[i] == b's' {
    return Duration::from_secs(n);
  }
  panic!("a duration ends in ns, us, ms or s: \"1ms\", \"250us\", \"10ns\" or \"1s\"");
}

#[doc(hidden)]
pub struct Registered(pub &'static Metric);

inventory::collect!(Registered);

/// Every `metric!` definition the linker kept, in no defined order. A definition
/// whose handle the program refers to is always kept. One in a library the
/// program does not refer to can be missing, more often on wasm than on native
/// targets. Two crates may define the same name; both appear.
pub fn registry() -> impl Iterator<Item = &'static Metric> {
  inventory::iter::<Registered>.into_iter().map(|registered| registered.0)
}

/// The first definition from [`registry`] with this name.
pub fn find(name: &str) -> Option<&'static Metric> {
  registry().find(|m| m.name == name)
}

/// The handle `metric!` produces for a `Counter`.
pub struct Counter {
  pub metric: Metric,
}

impl Counter {
  pub const fn new(metric: Metric) -> Self {
    Counter { metric }
  }

  /// Adds one on the global recorder.
  pub fn incr(&self, tags: Tags) {
    self.add_to(global(), 1, tags);
  }

  /// Adds `delta` on the global recorder.
  pub fn add(&self, delta: u64, tags: Tags) {
    self.add_to(global(), delta, tags);
  }

  pub fn add_to(&self, recorder: &dyn Recorder, delta: u64, tags: Tags) {
    let observation = self.metric.observation(recorder, tags, Sample::Count(delta));
    recorder.record(observation);
  }

  /// Reports a running total on the global recorder. The recorder turns it into
  /// deltas; see [`Recorder::total`].
  pub fn total(&self, total: u64, tags: Tags) {
    self.total_to(global(), total, tags);
  }

  pub fn total_to(&self, recorder: &dyn Recorder, total: u64, tags: Tags) {
    self.metric.check_tags(&tags);
    recorder.total(Name::Borrowed(self.metric.name), tags, total);
  }

  /// A collector that reads a running total each time it runs.
  pub fn poll_total<F>(&'static self, tags: Tags, read: F) -> PollTotal<F>
  where
    F: Fn() -> u64 + Send + 'static,
  {
    self.metric.check_tags(&tags);
    PollTotal::new(Name::Borrowed(self.metric.name), tags, read)
  }
}

/// The handle `metric!` produces for a `Gauge`.
pub struct Gauge {
  pub metric: Metric,
}

impl Gauge {
  pub const fn new(metric: Metric) -> Self {
    Gauge { metric }
  }

  /// Sets the level on the global recorder.
  pub fn set(&self, value: impl Into<NumberValue>, tags: Tags) {
    self.set_to(global(), value, tags);
  }

  pub fn set_to(&self, recorder: &dyn Recorder, value: impl Into<NumberValue>, tags: Tags) {
    let observation = self
      .metric
      .observation(recorder, tags, Sample::Number(value.into()));
    recorder.record(observation);
  }

  /// A collector that reads the level each time it runs.
  pub fn poll<F>(&'static self, tags: Tags, read: F) -> PollGauge<F>
  where
    F: Fn() -> NumberValue + Send + 'static,
  {
    self.metric.check_tags(&tags);
    PollGauge::new(Name::Borrowed(self.metric.name), tags, read)
  }
}

/// The handle `metric!` produces for a `Timer`.
pub struct Timer {
  pub metric: Metric,
}

impl Timer {
  pub const fn new(metric: Metric) -> Self {
    Timer { metric }
  }

  /// Records a duration on the global recorder.
  pub fn record(&self, duration: Duration, tags: Tags) {
    self.record_to(global(), duration, tags);
  }

  pub fn record_to(&self, recorder: &dyn Recorder, duration: Duration, tags: Tags) {
    let observation = self
      .metric
      .observation(recorder, tags, Sample::Duration(duration));
    recorder.record(observation);
  }

  /// Runs `f` and records how long it took on the global recorder.
  pub fn time<R>(&self, tags: Tags, f: impl FnOnce() -> R) -> R {
    self.time_to(global(), tags, f)
  }

  pub fn time_to<R>(&self, recorder: &dyn Recorder, tags: Tags, f: impl FnOnce() -> R) -> R {
    let start = Stopwatch::start(recorder);
    let result = f();
    self.record_to(recorder, start.elapsed(recorder), tags);
    result
  }

  /// Starts timing. The guard records on drop unless it is discarded.
  pub fn guard(&self, tags: Tags) -> TimerGuard<'_> {
    self.guard_to(global(), tags)
  }

  pub fn guard_to<'a>(&'a self, recorder: &'a dyn Recorder, tags: Tags) -> TimerGuard<'a> {
    TimerGuard {
      timer: self,
      recorder,
      tags: Some(tags),
      start: Stopwatch::start(recorder),
    }
  }
}

/// A running timer. Dropping it records the elapsed time.
pub struct TimerGuard<'a> {
  timer: &'a Timer,
  recorder: &'a dyn Recorder,
  tags: Option<Tags>,
  start: Stopwatch,
}

impl TimerGuard<'_> {
  /// Records now rather than at the end of the scope.
  pub fn stop(self) {}

  /// Drops the guard without recording.
  pub fn discard(mut self) {
    self.tags = None;
  }

  /// Replaces the tags that will be recorded, for an outcome known only at the end.
  pub fn set_tags(&mut self, tags: Tags) {
    self.tags = Some(tags);
  }

  pub fn elapsed(&self) -> Duration {
    self.start.elapsed(self.recorder)
  }
}

impl Drop for TimerGuard<'_> {
  fn drop(&mut self) {
    if let Some(tags) = self.tags.take() {
      self.timer.record_to(self.recorder, self.start.elapsed(self.recorder), tags);
    }
  }
}

/// The handle `metric!` produces for a `State`.
pub struct State {
  pub metric: Metric,
}

impl State {
  pub const fn new(metric: Metric) -> Self {
    State { metric }
  }

  /// Sets the state on the global recorder.
  pub fn set(&self, value: impl Into<StateValue>, tags: Tags) {
    self.set_to(global(), value, tags);
  }

  pub fn set_to(&self, recorder: &dyn Recorder, value: impl Into<StateValue>, tags: Tags) {
    let observation = self
      .metric
      .observation(recorder, tags, Sample::State(value.into()));
    recorder.record(observation);
  }

  /// A collector that reads the state each time it runs.
  pub fn poll<F>(&'static self, tags: Tags, read: F) -> PollState<F>
  where
    F: Fn() -> StateValue + Send + 'static,
  {
    self.metric.check_tags(&tags);
    PollState::new(Name::Borrowed(self.metric.name), tags, read)
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn parse_durations() {
    assert_eq!(parse_duration("1ms"), Duration::from_millis(1));
    assert_eq!(parse_duration("250us"), Duration::from_micros(250));
    assert_eq!(parse_duration("10ns"), Duration::from_nanos(10));
    assert_eq!(parse_duration("2s"), Duration::from_secs(2));
  }

  #[test]
  fn defaults_by_kind() {
    const NONE: &[MetricAttr] = &[];
    assert_eq!(Metric::new("a", Kind::Counter, &[], NONE).unit, Unit::Count);
    assert_eq!(Metric::new("a", Kind::Timer, &[], NONE).unit, Unit::Seconds);
    assert_eq!(Metric::new("a", Kind::Gauge, &[], NONE).unit, Unit::None);
    assert_eq!(Metric::new("a", Kind::State, &[], NONE).unit, Unit::None);
  }

  #[test]
  fn attributes_apply() {
    const ATTRS: &[MetricAttr] = &[
      MetricAttr::Unit(Unit::Bytes),
      MetricAttr::Tags(&["disk", "mount"]),
      MetricAttr::Resolution(Duration::from_millis(1)),
    ];
    let m = Metric::new("host.disk.free_bytes", Kind::Gauge, &[" Free space.", " Per mount."], ATTRS);
    assert_eq!(m.unit, Unit::Bytes);
    assert!(m.allows("disk"));
    assert!(!m.allows("cpu"));
    assert_eq!(m.resolution, Some(Duration::from_millis(1)));
    assert_eq!(m.description_text(), "Free space.\nPer mount.");
  }
}