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
use std::time::{Duration, Instant};
use instruments::{Instrument, Update, Updates};
use snapshot::Snapshot;
use util;
use {Descriptive, PutsSnapshot};
pub struct Gauge {
name: String,
title: Option<String>,
description: Option<String>,
value: Option<State>,
memorize_extrema: Option<Duration>,
}
impl Gauge {
pub fn new_with_defaults<T: Into<String>>(name: T) -> Gauge {
Gauge {
name: name.into(),
title: None,
description: None,
value: None,
memorize_extrema: None,
}
}
pub fn set(&mut self, v: u64) {
if let Some(mut state) = self.value.take() {
if let Some(ext_dur) = self.memorize_extrema {
let now = Instant::now();
if v >= state.peak {
state.last_peak_at = now;
state.peak = v;
} else if state.last_peak_at < now - ext_dur {
state.peak = v;
}
if v <= state.bottom {
state.last_bottom_at = now;
state.bottom = v;
} else if state.last_bottom_at < now - ext_dur {
state.bottom = v;
}
}
state.current = v;
self.value = Some(state);
} else {
let now = Instant::now();
self.value = Some(State {
current: v,
peak: v,
bottom: v,
last_peak_at: now,
last_bottom_at: now,
});
}
}
pub fn get(&self) -> Option<u64> {
self.value.as_ref().map(|v| v.current)
}
pub fn set_memorize_extrema(&mut self, d: Duration) {
self.memorize_extrema = Some(d)
}
pub fn name(&self) -> &str {
&self.name
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = name.into();
}
pub fn set_title<T: Into<String>>(&mut self, title: T) {
self.title = Some(title.into())
}
pub fn set_description<T: Into<String>>(&mut self, description: T) {
self.description = Some(description.into())
}
}
impl Instrument for Gauge {}
impl PutsSnapshot for Gauge {
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_postfixed_descriptives(self, &self.name, into, descriptive);
if let Some(ref state) = self.value {
into.items.push((self.name.clone(), state.current.into()));
if let Some(ext_dur) = self.memorize_extrema {
let peak_name = format!("{}_peak", self.name);
if state.last_peak_at > Instant::now() - ext_dur {
into.items.push((peak_name, state.peak.into()));
} else {
into.items.push((peak_name, state.current.into()));
}
let bottom_name = format!("{}_bottom", self.name);
if state.last_bottom_at > Instant::now() - ext_dur {
into.items.push((bottom_name, state.bottom.into()));
} else {
into.items.push((bottom_name, state.current.into()));
}
}
}
}
}
impl Updates for Gauge {
fn update(&mut self, with: &Update) -> usize {
match *with {
Update::ObservationWithValue(v, _) => {
self.set(v);
1
}
_ => 0,
}
}
}
impl Descriptive for Gauge {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}
struct State {
current: u64,
peak: u64,
bottom: u64,
last_peak_at: Instant,
last_bottom_at: Instant,
}