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
//! Instruments that track values and/or derive values
//! from observations.
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;

/// Scales incoming values.
///
/// This can be used either with a `Cockpit`
/// or with a `Panel`. Be careful when using on both
/// since both components do not care whether the
/// other already scaled a value.
#[derive(Debug, Clone, Copy)]
pub enum ValueScaling {
    /// Consider incoming values nanos and make them millis
    NanosToMillis,
    /// Consider incoming values nanos and make them micros
    NanosToMicros,
}

#[derive(Debug, Clone)]
/// An update instruction for an instrument
pub enum Update {
    /// Many observations without a value observed at a given time
    Observations(u64, Instant),
    /// One observation without a value observed at a given time
    Observation(Instant),
    /// One observation with a value observed at a given time
    ObservationWithValue(u64, Instant),
}

impl Update {
    /// Scale by the given `ValueScaling`
    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
        }
    }
}

/// A label with the associated `Update`
///
/// This is basically a split `Observation`
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)),
        }
    }
}

/// Implementors of `Updates`
/// can handle `Update`s.
///
/// `Update`s are basically observations without a label.
pub trait Updates {
    /// Update the internal state according to the given `Update`.
    ///
    /// Not all `Update`s might modify the internal state.
    /// Only those that are appropriate and meaningful for
    /// the implementor.
    fn update(&mut self, with: &Update);
}

/// Requirement for an instrument
pub trait Instrument: Updates + PutsSnapshot {}

/// The panel shows recorded
/// observations with the same label
/// in different representations.
///
/// Let's say you want to monitor the successful requests
/// of a specific endpoint of your REST API.
/// You would then create a panel for this and might
/// want to add a counter and a meter and a histogram
/// to track latencies.
///
/// # Example
///
/// ```
/// use std::time::Instant;
/// use metrix::instruments::*;
///
/// #[derive(Clone, PartialEq, Eq)]
/// struct SuccessfulRequests;
///
/// let counter = Counter::new_with_defaults("count");
/// let gauge = Gauge::new_with_defaults("last_latency");
/// let meter = Meter::new_with_defaults("per_second");
/// let histogram = Histogram::new_with_defaults("latencies");
///
/// assert_eq!(0, counter.get());
/// assert_eq!(None, gauge.get());
///
/// let mut panel = Panel::with_name(SuccessfulRequests, "succesful_requests");
/// panel.set_counter(counter);
/// panel.set_gauge(gauge);
/// panel.set_meter(meter);
/// panel.set_histogram(histogram);
///
/// let update = Update::ObservationWithValue(12, Instant::now());
/// panel.update(&update);
///
/// assert_eq!(Some(1), panel.counter().map(|c| c.get()));
/// assert_eq!(Some(12), panel.gauge().and_then(|g| g.get()));
/// ```
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> {
    /// Create a new `Panel` without a name.
    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(),
        }
    }

    /// Create a new `Panel` with the given name
    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)
    }

    /// Gets the name of this `Panel`
    pub fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|n| &**n)
    }

    /// Set the name if this `Panel`.
    ///
    /// The name is a path segment within a `Snapshot`
    pub fn set_name<T: Into<String>>(&mut self, name: T) {
        self.name = Some(name.into());
    }

    /// Sets the `title` of this `Panel`.
    ///
    /// A title can be part of a descriptive `Snapshot`
    pub fn set_title<T: Into<String>>(&mut self, title: T) {
        self.title = Some(title.into())
    }

    /// Sets the `description` of this `Panel`.
    ///
    /// A description can be part of a descriptive `Snapshot`
    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)
    }
}