Skip to content

Commit

Permalink
Snapshot and buffer raws correctly (#101)
Browse files Browse the repository at this point in the history
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 <blt@postmates.com>
  • Loading branch information
blt authored Sep 20, 2016
1 parent a7f980a commit 26dfa2f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/buckets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use string_cache::Atom;
pub struct Buckets {
counters: LruCache<Atom, f64>,
gauges: LruCache<Atom, f64>,
raws: LruCache<Atom, f64>,
raws: LruCache<Atom, Vec<Metric>>,
timers: LruCache<Atom, CKMS<f64>>,
histograms: LruCache<Atom, CKMS<f64>>,
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -126,6 +130,11 @@ impl Buckets {
&self.gauges
}

/// Get the gauges as a borrowed reference.
pub fn raws(&self) -> &LruCache<Atom, Vec<Metric>> {
&self.raws
}

/// Get the histograms as a borrowed reference.
pub fn histograms(&self) -> &LruCache<Atom, CKMS<f64>> {
&self.histograms
Expand Down
4 changes: 2 additions & 2 deletions src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl MetricQOS {
}
}

#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone)]
pub enum MetricKind {
Counter(f64),
Gauge,
Expand All @@ -40,7 +40,7 @@ pub enum MetricSign {
Negative,
}

#[derive(PartialEq, Debug)]
#[derive(PartialEq, Debug, Clone)]
pub struct Metric {
pub kind: MetricKind,
pub name: Atom,
Expand Down
7 changes: 7 additions & 0 deletions src/sinks/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
19 changes: 18 additions & 1 deletion src/sinks/wavefront.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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");
Expand All @@ -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]
Expand Down

0 comments on commit 26dfa2f

Please sign in to comment.