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
//! Exports metrics by logging them in a textual format.
//!
//! This exporter utilizes the `log` crate to log a textual representation of metrics in a given
//! snapshot.  Metrics that are scoped are represented using an indented tree structure.
//!
//! As an example, for a snapshot with two metrics -- `server.msgs_received` and `server.msgs_sent`
//! -- we would expect to see:
//!
//! ```c
//! root:
//!   server:
//!     msgs_received: 42
//!     msgs_sent: 13
//! ```
//!
//! If we added another metric -- `configuration_reloads` -- we would expect to see:
//!
//! ```c
//! root:
//!   configuration_reloads: 2
//!   server:
//!     msgs_received: 42
//!     msgs_sent: 13
//! ```
//!
//! Metrics are sorted alphabetically.
//!
//! ## Histograms
//!
//! Histograms received a little extra love and care when it comes to formatting.  This is the
//! general format of a given histogram when rendered:
//!
//! ```c
//! root:
//!   connect_time count: 15
//!   connect_time min: 1334ns
//!   connect_time p50: 1934ns
//!   connect_time p99: 5330ns
//!   connect_time max: 139389ns
//! ```
//!
//! The percentiles shown are based on the percentiles configured for hotmic itself, which are
//! generated for us and can't be influenced at the exporter level. The `count` value represents
//! the number of samples in the histogram at the time of measurement.
#[macro_use]
extern crate log;
extern crate hotmic;

use hotmic::{
    snapshot::{Snapshot, SummarizedHistogram, TypedMeasurement},
    Controller,
};
use log::Level;
use std::collections::{HashMap, VecDeque};
use std::fmt::Display;
use std::thread;
use std::time::Duration;

/// Exports metrics by logging them in a textual format.
pub struct StdoutExporter {
    controller: Controller,
    level: Level,
    interval: Duration,
}

impl StdoutExporter {
    /// Creates a new `StdoutExporter`.
    ///
    /// The exporter will take a snapshot based on the configured `controller`, outputting at the
    /// configured `level`, at the configured `interval`.
    pub fn new(controller: Controller, level: Level, interval: Duration) -> Self {
        StdoutExporter {
            controller,
            level,
            interval,
        }
    }

    fn turn(&mut self) {
        match self.controller.get_snapshot() {
            Ok(snapshot) => self.process_snapshot(snapshot),
            Err(e) => error!("caught error getting metrics snapshot: {}", e),
        }
    }

    /// Runs the exporter synchronously, blocking the calling thread.
    ///
    /// You should run this in a dedicated thread:
    ///
    /// ```c
    /// let mut exporter = StdoutExporter::new(controller, Level::Info, Duration::from_secs(5));
    /// std::thread::spawn(move || exporter.run());
    /// ```
    pub fn run(&mut self) {
        loop {
            self.turn();
            thread::sleep(self.interval);
        }
    }

    fn process_snapshot(&self, snapshot: Snapshot) {
        let mut nested = Nested::default();

        for measurement in snapshot.into_vec() {
            let (name_parts, mut values) = match measurement {
                TypedMeasurement::Counter(key, value) => {
                    let (layers, name) = name_to_parts(key);
                    let values = single_value_to_values(name, value);

                    (layers, values)
                }
                TypedMeasurement::Gauge(key, value) => {
                    let (layers, name) = name_to_parts(key);
                    let values = single_value_to_values(name, value);

                    (layers, values)
                }
                TypedMeasurement::TimingHistogram(key, summary) => {
                    let (layers, name) = name_to_parts(key);
                    let values = summary_to_values(name, summary, "ns");

                    (layers, values)
                }
                TypedMeasurement::ValueHistogram(key, summary) => {
                    let (layers, name) = name_to_parts(key);
                    let values = summary_to_values(name, summary, "");

                    (layers, values)
                }
            };

            nested.insert(name_parts, &mut values);
        }

        let output = nested.into_output();
        log!(self.level, "metrics:\n{}", output);
    }
}

fn name_to_parts(name: String) -> (VecDeque<String>, String) {
    let mut parts = name
        .split('.')
        .map(ToOwned::to_owned)
        .collect::<VecDeque<_>>();
    let name = parts.pop_back().expect("name didn't have a single part");

    (parts, name)
}

fn single_value_to_values<T>(name: String, value: T) -> Vec<String>
where
    T: Display,
{
    let fvalue = format!("{}: {}", name, value);
    vec![fvalue]
}

fn summary_to_values(name: String, summary: SummarizedHistogram, suffix: &str) -> Vec<String> {
    let mut values = Vec::new();

    values.push(format!("{} count: {}", name, summary.count()));
    for (percentile, value) in summary.measurements() {
        values.push(format!(
            "{} {}: {}{}",
            name,
            percentile.label(),
            value,
            suffix
        ));
    }

    values
}

struct Nested {
    level: usize,
    current: Vec<String>,
    next: HashMap<String, Nested>,
}

impl Nested {
    pub fn with_level(level: usize) -> Self {
        Nested {
            level,
            ..Default::default()
        }
    }

    pub fn insert(&mut self, mut name_parts: VecDeque<String>, values: &mut Vec<String>) {
        match name_parts.len() {
            0 => {
                let indent = "  ".repeat(self.level + 1);
                let mut indented = values
                    .iter()
                    .map(move |x| format!("{}{}", indent, x))
                    .collect::<Vec<_>>();
                self.current.append(&mut indented);
            }
            _ => {
                let name = name_parts
                    .pop_front()
                    .expect("failed to get next name component");
                let current_level = self.level;
                let inner = self
                    .next
                    .entry(name)
                    .or_insert_with(move || Nested::with_level(current_level + 1));
                inner.insert(name_parts, values);
            }
        }
    }

    pub fn into_output(self) -> String {
        let indent = "  ".repeat(self.level + 1);
        let mut output = String::new();
        if self.level == 0 {
            output.push_str("\nroot:\n");
        }

        let mut sorted = self
            .current
            .into_iter()
            .map(SortEntry::Inline)
            .chain(self.next.into_iter().map(|(k, v)| SortEntry::Nested(k, v)))
            .collect::<Vec<_>>();
        sorted.sort();

        for entry in sorted {
            match entry {
                SortEntry::Inline(s) => {
                    output.push_str(s.as_str());
                    output.push_str("\n");
                }
                SortEntry::Nested(s, inner) => {
                    output.push_str(indent.as_str());
                    output.push_str(s.as_str());
                    output.push_str(":\n");

                    let layer_output = inner.into_output();
                    output.push_str(layer_output.as_str());
                }
            }
        }

        output
    }
}

impl Default for Nested {
    fn default() -> Self {
        Nested {
            level: 0,
            current: Vec::new(),
            next: HashMap::new(),
        }
    }
}

enum SortEntry {
    Inline(String),
    Nested(String, Nested),
}

impl SortEntry {
    fn name(&self) -> &String {
        match self {
            SortEntry::Inline(s) => s,
            SortEntry::Nested(s, _) => s,
        }
    }
}

impl PartialEq for SortEntry {
    fn eq(&self, other: &SortEntry) -> bool {
        self.name() == other.name()
    }
}

impl Eq for SortEntry {}

impl std::cmp::PartialOrd for SortEntry {
    fn partial_cmp(&self, other: &SortEntry) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl std::cmp::Ord for SortEntry {
    fn cmp(&self, other: &SortEntry) -> std::cmp::Ordering {
        self.name().cmp(other.name())
    }
}