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
//! Cockpits are used to monitor different aspects of a component
use std::time::{Duration, Instant};

use instruments::*;
use snapshot::{ItemKind, Snapshot};
use util;
use {HandlesObservations, Observation, PutsSnapshot};

/// A cockpit groups panels.
///
/// Use a cockpit to group panels that are somehow related.
/// Since the Cockpit is generic over its label you can
/// use an enum as a label for grouping panels easily.
///
/// # Example
///
/// Imagine you have a HTTP component that makes requests to
/// another service and you want to track successful and failed
/// requests individually.
///
/// ```
/// use std::time::Instant;
/// use metrix::{Observation, HandlesObservations};
/// use metrix::instruments::*;
/// use metrix::cockpit::*;
///
/// #[derive(Clone, PartialEq, Eq)]
/// enum Request {
///     Successful,
///     Failed,
/// }
///
/// 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 success_panel = Panel::with_name(Request::Successful,
/// "succesful_requests"); success_panel.set_counter(counter);
/// success_panel.set_gauge(gauge);
/// success_panel.set_meter(meter);
/// success_panel.set_histogram(histogram);
///
/// 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 failed_panel = Panel::with_name(Request::Failed, "failed_requests");
/// failed_panel.set_counter(counter);
/// failed_panel.set_gauge(gauge);
/// failed_panel.set_meter(meter);
/// failed_panel.set_histogram(histogram);
///
/// let mut cockpit = Cockpit::new("requests", None);
/// cockpit.add_panel(success_panel);
/// cockpit.add_panel(failed_panel);
///
/// let observation = Observation::ObservedOneValue {
///     label: Request::Successful,
///     value: 100,
///     timestamp: Instant::now(),
/// };
///
/// cockpit.handle_observation(&observation);
///
/// {
///     let panels = cockpit.panels();
///     let success_panel = panels
///         .iter()
///         .find(|p| p.label() == &Request::Successful)
///         .unwrap();
///
///     assert_eq!(Some(1), success_panel.counter().map(|c| c.get()));
///     assert_eq!(Some(100), success_panel.gauge().and_then(|g| g.get()));
/// }
///
/// let observation = Observation::ObservedOneValue {
///     label: Request::Failed,
///     value: 667,
///     timestamp: Instant::now(),
/// };
///
/// cockpit.handle_observation(&observation);
///
/// let panels = cockpit.panels();
/// let failed_panel = panels
///     .iter()
///     .find(|p| p.label() == &Request::Failed)
///     .unwrap();
///
/// assert_eq!(Some(1), failed_panel.counter().map(|c| c.get()));
/// assert_eq!(Some(667), failed_panel.gauge().and_then(|g| g.get()));
/// ```
pub struct Cockpit<L> {
    name: Option<String>,
    title: Option<String>,
    description: Option<String>,
    panels: Vec<Panel<L>>,
    value_scaling: Option<ValueScaling>,
    handlers: Vec<Box<HandlesObservations<Label = L>>>,
    snapshooters: Vec<Box<PutsSnapshot>>,
    last_activity_at: Instant,
    max_inactivity_duration: Option<Duration>,
}

impl<L> Cockpit<L>
where
    L: Clone + Eq + Send + 'static,
{
    /// Creates a new instance.
    ///
    /// Even though the name is optional it is suggested to use
    /// a name for a cockpit since in most cases a `Cockpit` is
    /// a meaningful grouping for panels and instruments.
    pub fn new<T: Into<String>>(name: T, value_scaling: Option<ValueScaling>) -> Cockpit<L> {
        let mut cockpit = Cockpit::default();
        cockpit.name = Some(name.into());
        cockpit.value_scaling = value_scaling;
        cockpit
    }

    /// Creates a new `Cockpit` without a name.
    ///
    /// This will have the effect that there will be no grouping in the
    /// snapshot around the contained components.
    pub fn without_name(value_scaling: Option<ValueScaling>) -> Cockpit<L> {
        let mut cockpit = Cockpit::default();
        cockpit.value_scaling = value_scaling;
        cockpit
    }

    /// Creates a new `Cockpit` with a name and a `ValueScaling`.
    ///
    /// The scaling is calculated on the value of
    /// `Observation::ObservedOneValue` prior to delegating the
    /// `Observation` to the `Panel`s. If the containing
    /// components also do a value scaling, the scaling will be applied
    /// multiple times which is most likely wrong.
    pub fn with_value_scaling<T: Into<String>>(
        &mut self,
        name: T,
        value_scaling: ValueScaling,
    ) -> Cockpit<L> {
        Cockpit::new(name, Some(value_scaling))
    }

    /// Creates a new `Cockpit` with just a name.
    pub fn without_value_scaling<T: Into<String>>(&mut self, name: T) -> Cockpit<L> {
        Cockpit::new(name, None)
    }

    /// Returns the name of this cockpit.
    ///
    /// If there is a name set, this will group the inner components in the
    /// snapshot.
    pub fn name(&self) -> Option<&str> {
        self.name.as_ref().map(|n| &**n)
    }

    /// Sets a name for this `Cockpit`. This will also enable grouping.
    pub fn set_name<T: Into<String>>(&mut self, name: T) {
        self.name = Some(name.into())
    }

    /// Sets the title which will be displayed if a descriptive snaphot is
    /// requested.
    pub fn set_title<T: Into<String>>(&mut self, title: T) {
        self.title = Some(title.into())
    }

    /// Sets the description which will be displayed if a descriptive snaphot
    /// is requested.
    pub fn set_description<T: Into<String>>(&mut self, description: T) {
        self.description = Some(description.into())
    }

    /// Set and enable value scaling.
    pub fn set_value_scaling(&mut self, value_scaling: ValueScaling) {
        self.value_scaling = Some(value_scaling)
    }

    /// Sets the maximum amount of time this cockpit may be
    /// inactive until no more snapshots are taken
    pub fn set_inactivity_limit(&mut self, limit: Duration) {
        self.max_inactivity_duration = Some(limit);
    }

    /// Add a `Panel` to this cockpit.
    ///
    /// A `Panel` will receive only those `Observation`s wher
    /// labels match.
    ///
    /// There can be multiple `Panel`s for the same label.
    pub fn add_panel(&mut self, panel: Panel<L>) {
        self.panels.push(panel);
    }

    /// Returns the `Panel`s
    pub fn panels(&self) -> Vec<&Panel<L>> {
        self.panels.iter().collect()
    }

    /// Returns the `Panel`s mutable
    pub fn panels_mut(&mut self) -> Vec<&mut Panel<L>> {
        self.panels.iter_mut().collect()
    }

    /// Add a handler. This can be custom logic for
    /// metrics.
    ///
    /// A handler will be passed all `Observation`s unlike
    /// a `Panel` which will only receive `Observation`s where
    /// the label matches.
    pub fn add_handler<T>(&mut self, handler: T)
    where
        T: HandlesObservations<Label = L>,
    {
        self.handlers.push(Box::new(handler))
    }

    /// Returns all the handlers.
    pub fn handlers(&self) -> Vec<&HandlesObservations<Label = L>> {
        self.handlers.iter().map(|h| &**h).collect()
    }

    /// Adds a snapshooter.
    ///
    /// A snapshooter will only be invoked when a `Snapshot` is requested.
    /// It will never receive an `Observation`.
    pub fn add_snapshooter<T: PutsSnapshot>(&mut self, snapshooter: T) {
        self.snapshooters.push(Box::new(snapshooter));
    }

    /// Returns all snapshooters.
    pub fn snapshooters(&self) -> Vec<&PutsSnapshot> {
        self.snapshooters.iter().map(|p| &**p).collect()
    }

    fn put_values_into_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
        util::put_default_descriptives(self, into, descriptive);

        if let Some(d) = self.max_inactivity_duration {
            if self.last_activity_at.elapsed() > d {
                into.items
                    .push(("_inactive".to_string(), ItemKind::Boolean(true)));
                into.items
                    .push(("_active".to_string(), ItemKind::Boolean(false)));
                return;
            } else {
                into.items
                    .push(("_inactive".to_string(), ItemKind::Boolean(false)));
                into.items
                    .push(("_active".to_string(), ItemKind::Boolean(true)));
            }
        };

        self.panels
            .iter()
            .for_each(|p| p.put_snapshot(into, descriptive));

        self.handlers
            .iter()
            .for_each(|h| h.put_snapshot(into, descriptive));

        self.snapshooters
            .iter()
            .for_each(|s| s.put_snapshot(into, descriptive));
    }
}

impl<L> PutsSnapshot for Cockpit<L>
where
    L: Clone + Eq + 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> Default for Cockpit<L>
where
    L: Clone + Eq + Send + 'static,
{
    fn default() -> Cockpit<L> {
        Cockpit {
            name: None,
            title: None,
            description: None,
            panels: Vec::new(),
            value_scaling: None,
            handlers: Vec::new(),
            snapshooters: Vec::new(),
            last_activity_at: Instant::now(),
            max_inactivity_duration: None,
        }
    }
}

impl<L> HandlesObservations for Cockpit<L>
where
    L: Clone + Eq + Send + 'static,
{
    type Label = L;

    fn handle_observation(&mut self, observation: &Observation<Self::Label>) {
        self.last_activity_at = Instant::now();

        let observation = if let Some(scaling) = self.value_scaling {
            observation.scaled(scaling)
        } else {
            observation.clone()
        };

        self.handlers
            .iter_mut()
            .for_each(|h| h.handle_observation(&observation));

        let LabelAndUpdate(label, update) = observation.into();

        self.panels
            .iter_mut()
            .filter(|p| p.label() == &label)
            .for_each(|p| p.update(&update));
    }
}

impl<L> ::Descriptive for Cockpit<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)
    }
}