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
use crate::clear::{Clear, Clearable};
pub use aspect::{Advice, OnResult, OnResultMut, Enter};
use serde::Serialize;
use std::marker::PhantomData;
pub trait Metric<R>: Default + OnResultMut<R> + Clear + Serialize {}
#[doc(hidden)]
pub fn on_result<R, A: Metric<R>>(metric: &A, _enter: <A as Enter>::E, _result: &mut R) -> Advice {
metric.on_result(_enter, _result)
}
pub struct ExitGuard<'a, R, M: Metric<R>> {
metric: &'a M,
enter: Option<<M as Enter>::E>,
_phantom: PhantomData<R>,
}
impl<'a, R, M: Metric<R>> ExitGuard<'a, R, M> {
pub fn new(metric: &'a M) -> Self {
Self {
metric,
enter: Some(metric.enter()),
_phantom: PhantomData,
}
}
pub fn on_result(mut self, result: &mut R) {
if let Some(enter) = self.enter.take() {
self.metric.on_result(enter, result);
} else {
}
}
}
impl<'a, R, M: Metric<R>> Drop for ExitGuard<'a, R, M> {
fn drop(&mut self) {
if let Some(enter) = self.enter.take() {
self.metric.leave_scope(enter);
} else {
}
}
}
pub trait Counter: Default + Clear + Clearable + Serialize {
fn incr(&self);
}
pub trait Gauge: Default + Clear + Serialize {
fn incr(&self);
fn decr(&self);
}
pub trait Histogram: Clear + Serialize {
fn with_bound(max_value: u64) -> Self;
fn record(&self, value: u64);
}