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
use std::time::Instant;
use self::switches::*;
use snapshot::{ItemKind, Snapshot};
use util;
use Observation;
use {Descriptive, PutsSnapshot};
pub use self::counter::Counter;
pub use self::gauge::Gauge;
pub use self::histogram::Histogram;
pub use self::meter::Meter;
mod counter;
mod gauge;
mod histogram;
mod meter;
pub mod other_instruments;
pub mod polled;
pub mod switches;
#[derive(Debug, Clone, Copy)]
pub enum ValueScaling {
NanosToMillis,
NanosToMicros,
}
#[derive(Debug, Clone)]
pub enum Update {
Observations(u64, Instant),
Observation(Instant),
ObservationWithValue(u64, Instant),
}
impl Update {
pub fn scale(self, scaling: ValueScaling) -> Update {
if let Update::ObservationWithValue(v, t) = self {
match scaling {
ValueScaling::NanosToMillis => Update::ObservationWithValue(v / 1_000_000, t),
ValueScaling::NanosToMicros => Update::ObservationWithValue(v / 1_000, t),
}
} else {
self
}
}
}
pub struct LabelAndUpdate<T>(pub T, pub Update);
impl<T> From<Observation<T>> for LabelAndUpdate<T> {
fn from(obs: Observation<T>) -> LabelAndUpdate<T> {
match obs {
Observation::Observed {
label,
count,
timestamp,
..
} => LabelAndUpdate(label, Update::Observations(count, timestamp)),
Observation::ObservedOne {
label, timestamp, ..
} => LabelAndUpdate(label, Update::Observation(timestamp)),
Observation::ObservedOneValue {
label,
value,
timestamp,
..
} => LabelAndUpdate(label, Update::ObservationWithValue(value, timestamp)),
}
}
}
pub trait Updates {
fn update(&mut self, with: &Update);
}
pub trait Instrument: Updates + PutsSnapshot {}
pub struct Panel<L> {
label: L,
name: Option<String>,
title: Option<String>,
description: Option<String>,
counter: Option<Counter>,
gauge: Option<Gauge>,
meter: Option<Meter>,
histogram: Option<Histogram>,
instruments: Vec<Box<Instrument>>,
snapshooters: Vec<Box<PutsSnapshot>>,
value_scaling: Option<ValueScaling>,
}
impl<L> Panel<L> {
pub fn new(label: L) -> Panel<L> {
Panel {
label: label,
name: None,
title: None,
description: None,
counter: None,
gauge: None,
meter: None,
histogram: None,
value_scaling: None,
instruments: Vec::new(),
snapshooters: Vec::new(),
}
}
pub fn with_name<T: Into<String>>(label: L, name: T) -> Panel<L> {
let mut panel = Panel::new(label);
panel.set_name(name);
panel
}
pub fn set_counter(&mut self, counter: Counter) {
self.counter = Some(counter);
}
pub fn counter(&self) -> Option<&Counter> {
self.counter.as_ref()
}
pub fn set_gauge(&mut self, gauge: Gauge) {
self.gauge = Some(gauge);
}
pub fn gauge(&self) -> Option<&Gauge> {
self.gauge.as_ref()
}
pub fn set_meter(&mut self, meter: Meter) {
self.meter = Some(meter);
}
pub fn meter(&self) -> Option<&Meter> {
self.meter.as_ref()
}
pub fn set_histogram(&mut self, histogram: Histogram) {
self.histogram = Some(histogram);
}
pub fn histogram(&self) -> Option<&Histogram> {
self.histogram.as_ref()
}
#[deprecated(since = "0.6.0", note = "use add_instrument")]
pub fn set_staircase_timer(&mut self, timer: StaircaseTimer) {
self.add_instrument(timer);
}
#[deprecated(since = "0.6.0", note = "there will be no replacement")]
pub fn staircase_timer(&self) -> Option<&StaircaseTimer> {
None
}
pub fn add_snapshooter<T: PutsSnapshot>(&mut self, snapshooter: T) {
self.snapshooters.push(Box::new(snapshooter));
}
pub fn snapshooters(&self) -> Vec<&PutsSnapshot> {
self.snapshooters.iter().map(|p| &**p).collect()
}
pub fn add_instrument<I: Instrument>(&mut self, instrument: I) {
self.instruments.push(Box::new(instrument));
}
pub fn instruments(&self) -> Vec<&Instrument> {
self.instruments.iter().map(|p| &**p).collect()
}
pub fn set_value_scaling(&mut self, value_scaling: ValueScaling) {
self.value_scaling = Some(value_scaling)
}
pub fn name(&self) -> Option<&str> {
self.name.as_ref().map(|n| &**n)
}
pub fn set_name<T: Into<String>>(&mut self, name: T) {
self.name = Some(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())
}
pub fn label(&self) -> &L {
&self.label
}
fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
util::put_default_descriptives(self, into, descriptive);
self.counter
.as_ref()
.iter()
.for_each(|x| x.put_snapshot(into, descriptive));
self.gauge
.as_ref()
.iter()
.for_each(|x| x.put_snapshot(into, descriptive));
self.meter
.as_ref()
.iter()
.for_each(|x| x.put_snapshot(into, descriptive));
self.histogram
.as_ref()
.iter()
.for_each(|x| x.put_snapshot(into, descriptive));
self.snapshooters
.iter()
.for_each(|p| p.put_snapshot(into, descriptive));
self.instruments
.iter()
.for_each(|p| p.put_snapshot(into, descriptive));
}
}
impl<L> PutsSnapshot for Panel<L>
where
L: Send + 'static,
{
fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
if let Some(ref name) = self.name {
let mut new_level = Snapshot::default();
self.put_values_into_snapshot(&mut new_level, descriptive);
into.items
.push((name.clone(), ItemKind::Snapshot(new_level)));
} else {
self.put_values_into_snapshot(into, descriptive);
}
}
}
impl<L> Updates for Panel<L> {
fn update(&mut self, with: &Update) {
let with = if let Some(scaling) = self.value_scaling {
with.clone().scale(scaling)
} else {
with.clone()
};
self.counter.iter_mut().for_each(|x| x.update(&with));
self.gauge.iter_mut().for_each(|x| x.update(&with));
self.meter.iter_mut().for_each(|x| x.update(&with));
self.histogram.iter_mut().for_each(|x| x.update(&with));
self.instruments.iter_mut().for_each(|x| x.update(&with));
}
}
impl<L> Descriptive for Panel<L> {
fn title(&self) -> Option<&str> {
self.title.as_ref().map(|n| &**n)
}
fn description(&self) -> Option<&str> {
self.description.as_ref().map(|n| &**n)
}
}