From 26dfa2ff3c67048f8ef59aa7c16828efd914ee1d Mon Sep 17 00:00:00 2001 From: "Brian L. Troutwine" Date: Tue, 20 Sep 2016 12:28:34 -0700 Subject: [PATCH] Snapshot and buffer raws correctly (#101) In 0.3.2 we made the distinction between raws and gauges but did not, owing to a lack of coverage, correctly test that they were being emitted on each snapshot. D'oh. This commit _does_ correctly test that raws are snapshotted correctly and resolves #86 at the same time. Signed-off-by: Brian L. Troutwine --- src/buckets.rs | 13 +++++++++++-- src/metric.rs | 4 ++-- src/sinks/console.rs | 7 +++++++ src/sinks/wavefront.rs | 19 ++++++++++++++++++- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/buckets.rs b/src/buckets.rs index 041c66c3..cbef3291 100644 --- a/src/buckets.rs +++ b/src/buckets.rs @@ -12,7 +12,7 @@ use string_cache::Atom; pub struct Buckets { counters: LruCache, gauges: LruCache, - raws: LruCache, + raws: LruCache>, timers: LruCache>, histograms: LruCache>, } @@ -96,7 +96,11 @@ impl Buckets { self.gauges.insert(name, value.value); } MetricKind::Raw => { - self.raws.insert(name, value.value); + if !self.raws.contains_key(&name) { + let _ = self.raws.insert(value.name.to_owned(), Default::default()); + }; + let raw = self.raws.get_mut(&name).expect("shouldn't happen but did, raw"); + (*raw).push((*value).clone()); } MetricKind::Histogram => { if !self.histograms.contains_key(&name) { @@ -126,6 +130,11 @@ impl Buckets { &self.gauges } + /// Get the gauges as a borrowed reference. + pub fn raws(&self) -> &LruCache> { + &self.raws + } + /// Get the histograms as a borrowed reference. pub fn histograms(&self) -> &LruCache> { &self.histograms diff --git a/src/metric.rs b/src/metric.rs index 0093141c..95edef3b 100644 --- a/src/metric.rs +++ b/src/metric.rs @@ -24,7 +24,7 @@ impl MetricQOS { } } -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] pub enum MetricKind { Counter(f64), Gauge, @@ -40,7 +40,7 @@ pub enum MetricSign { Negative, } -#[derive(PartialEq, Debug)] +#[derive(PartialEq, Debug, Clone)] pub struct Metric { pub kind: MetricKind, pub name: Atom, diff --git a/src/sinks/console.rs b/src/sinks/console.rs index 898b2cd1..e608463f 100644 --- a/src/sinks/console.rs +++ b/src/sinks/console.rs @@ -52,6 +52,13 @@ impl Sink for Console { fmt_line(key, &value); } + println!(" raws:"); + for (key, value) in self.aggrs.raws() { + for m in value { + fmt_line(key, &m.value); + } + } + println!(" histograms:"); for (key, value) in self.aggrs.histograms() { for tup in &[("min", 0.0), diff --git a/src/sinks/wavefront.rs b/src/sinks/wavefront.rs index 68c1c46b..7685397f 100644 --- a/src/sinks/wavefront.rs +++ b/src/sinks/wavefront.rs @@ -128,6 +128,13 @@ impl Wavefront { } } + // Raw points have no QOS as we can make no valid aggregation of them. + for (key, value) in self.aggrs.raws().iter() { + for m in value { + write!(stats, "{} {} {} {}\n", key, m.value, start, self.tags).unwrap(); + } + } + stats } } @@ -202,11 +209,19 @@ mod test { 3.101, Some(dt), MetricKind::Timer))); + wavefront.deliver(Arc::new(Metric::new_with_time(Atom::from("test.raw"), + 1.0, + Some(dt), + MetricKind::Raw))); + wavefront.deliver(Arc::new(Metric::new_with_time(Atom::from("test.raw"), + 1.0, + Some(dt), + MetricKind::Raw))); let result = wavefront.format_stats(Some(10101)); let lines: Vec<&str> = result.lines().collect(); println!("{:?}", lines); - assert_eq!(16, lines.len()); + assert_eq!(18, lines.len()); assert_eq!(lines[0], "test.counter 1 10101 source=test-src"); assert_eq!(lines[1], "test.gauge 3.211 10101 source=test-src"); assert_eq!(lines[2], "test.timer.min 1.101 10101 source=test-src"); @@ -223,6 +238,8 @@ mod test { assert_eq!(lines[13], "test.timer.99 12.101 10101 source=test-src"); assert_eq!(lines[14], "test.timer.999 12.101 10101 source=test-src"); assert_eq!(lines[15], "test.timer.count 3 10101 source=test-src"); + assert_eq!(lines[16], "test.raw 1 10101 source=test-src"); + assert_eq!(lines[17], "test.raw 1 10101 source=test-src"); } #[test]