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
use std::time::Instant;

use exponential_decay_histogram::ExponentialDecayHistogram;

use snapshot::{ItemKind, Snapshot};
use {Descriptive, PutsSnapshot};
use instruments::{Update, Updates};

use util;

/// For tracking values. E.g. request latencies
pub struct Histogram {
    name: String,
    title: Option<String>,
    description: Option<String>,
    inner_histogram: ExponentialDecayHistogram,
    last_update: Instant,
}

impl Histogram {
    pub fn new_with_defaults<T: Into<String>>(name: T) -> Histogram {
        let inner_histogram = ExponentialDecayHistogram::new();
        Histogram {
            name: name.into(),
            title: None,
            description: None,
            inner_histogram,
            last_update: Instant::now(),
        }
    }

    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())
    }

    fn put_values_into_snapshot(&self, into: &mut Snapshot) {
        let snapshot = self.inner_histogram.snapshot();

        let quantiles = vec![
            (50u16, snapshot.value(0.5)),
            (75u16, snapshot.value(0.75)),
            (99u16, snapshot.value(0.99)),
            (999u16, snapshot.value(0.999)),
        ];

        let histo_snapshot = HistogramSnapshot {
            min: snapshot.min(),
            max: snapshot.max(),
            mean: snapshot.mean(),
            stddev: snapshot.stddev(),
            count: snapshot.count(),
            quantiles: quantiles,
        };

        histo_snapshot.put_snapshot(into);
    }
}

impl PutsSnapshot for Histogram {
    fn put_snapshot(&self, into: &mut Snapshot, descriptive: bool) {
        util::put_postfixed_descriptives(self, &self.name, into, descriptive);
        let mut new_level = Snapshot::default();
        self.put_values_into_snapshot(&mut new_level);
        into.push(self.name.clone(), ItemKind::Snapshot(new_level));
    }
}

impl Updates for Histogram {
    fn update(&mut self, with: &Update) {
        match *with {
            Update::ObservationWithValue(v, t) => if t > self.last_update {
                self.inner_histogram.update_at(t, v as i64);
                self.last_update = t
            } else {
                self.inner_histogram.update(v as i64);
                self.last_update = Instant::now();
            },
            _ => (),
        }
    }
}

impl Descriptive for Histogram {
    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 HistogramSnapshot {
    pub max: i64,
    pub min: i64,
    pub mean: f64,
    pub stddev: f64,
    pub count: u64,
    pub quantiles: Vec<(u16, i64)>,
}

impl HistogramSnapshot {
    pub fn put_snapshot(&self, into: &mut Snapshot) {
        into.items.push(("max".to_string(), self.max.into()));
        into.items.push(("min".to_string(), self.min.into()));
        into.items.push(("mean".to_string(), self.mean.into()));
        into.items.push(("stddev".to_string(), self.stddev.into()));
        into.items.push(("count".to_string(), self.count.into()));

        let mut quantiles = Snapshot::default();

        for &(ref q, ref v) in &self.quantiles {
            quantiles.items.push((format!("p{}", q), ItemKind::Int(*v)));
        }

        into.items
            .push(("quantiles".to_string(), ItemKind::Snapshot(quantiles)));
    }
}