Quiet counter (#574)

* only submit to influx when we log

* test accumulated value logging
This commit is contained in:
anatoly yakovenko 2018-07-10 15:14:59 -07:00 committed by GitHub
parent 7687436bef
commit 0d4e4b18c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 9 deletions

View File

@ -6,9 +6,12 @@ use timing;
pub struct Counter {
pub name: &'static str,
/// total accumulated value
pub counts: AtomicUsize,
pub nanos: AtomicUsize,
pub times: AtomicUsize,
/// last accumulated value logged
pub lastlog: AtomicUsize,
pub lograte: usize,
}
@ -19,6 +22,7 @@ macro_rules! create_counter {
counts: AtomicUsize::new(0),
nanos: AtomicUsize::new(0),
times: AtomicUsize::new(0),
lastlog: AtomicUsize::new(0),
lograte: $lograte,
}
};
@ -36,6 +40,7 @@ impl Counter {
let counts = self.counts.fetch_add(events, Ordering::Relaxed);
let nanos = self.nanos.fetch_add(total as usize, Ordering::Relaxed);
let times = self.times.fetch_add(1, Ordering::Relaxed);
let lastlog = self.lastlog.load(Ordering::Relaxed);
if times % self.lograte == 0 && times > 0 {
info!(
"COUNTER:{{\"name\": \"{}\", \"counts\": {}, \"nanos\": {}, \"samples\": {}, \"rate\": {}, \"now\": {}}}",
@ -46,16 +51,21 @@ impl Counter {
counts as f64 * 1e9 / nanos as f64,
timing::timestamp(),
);
metrics::submit(
influxdb::Point::new(&format!("counter_{}", self.name))
.add_field(
"count",
influxdb::Value::Integer(counts as i64 - lastlog as i64),
)
.add_field(
"duration_ms",
influxdb::Value::Integer(timing::duration_as_ms(&dur) as i64),
)
.to_owned(),
);
self.lastlog
.compare_and_swap(lastlog, counts, Ordering::Relaxed);
}
metrics::submit(
influxdb::Point::new(&format!("counter_{}", self.name))
.add_field("count", influxdb::Value::Integer(events as i64))
.add_field(
"duration_ms",
influxdb::Value::Integer(timing::duration_as_ms(&dur) as i64),
)
.to_owned(),
);
}
}
#[cfg(test)]
@ -74,7 +84,18 @@ mod tests {
assert_ne!(COUNTER.nanos.load(Ordering::Relaxed), 0);
assert_eq!(COUNTER.times.load(Ordering::Relaxed), 1);
assert_eq!(COUNTER.lograte, 100);
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 0);
assert_eq!(COUNTER.name, "test");
}
for _ in 0..199 {
inc_counter!(COUNTER, 2, start);
}
unsafe {
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 199);
}
inc_counter!(COUNTER, 2, start);
unsafe {
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 399);
}
}
}