2018-07-07 20:08:14 -07:00
|
|
|
use influx_db_client as influxdb;
|
|
|
|
use metrics;
|
2018-05-30 21:24:21 -07:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2018-06-08 10:11:07 -07:00
|
|
|
use timing;
|
2018-05-30 21:24:21 -07:00
|
|
|
|
2018-07-10 12:37:39 -07:00
|
|
|
const INFLUX_RATE: usize = 100;
|
|
|
|
|
2018-05-30 21:24:21 -07:00
|
|
|
pub struct Counter {
|
|
|
|
pub name: &'static str,
|
2018-07-10 15:14:59 -07:00
|
|
|
/// total accumulated value
|
2018-05-30 21:24:21 -07:00
|
|
|
pub counts: AtomicUsize,
|
|
|
|
pub times: AtomicUsize,
|
2018-07-10 15:14:59 -07:00
|
|
|
/// last accumulated value logged
|
|
|
|
pub lastlog: AtomicUsize,
|
2018-05-30 21:24:21 -07:00
|
|
|
pub lograte: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! create_counter {
|
|
|
|
($name:expr, $lograte:expr) => {
|
|
|
|
Counter {
|
|
|
|
name: $name,
|
|
|
|
counts: AtomicUsize::new(0),
|
|
|
|
times: AtomicUsize::new(0),
|
2018-07-10 15:14:59 -07:00
|
|
|
lastlog: AtomicUsize::new(0),
|
2018-05-30 21:24:21 -07:00
|
|
|
lograte: $lograte,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! inc_counter {
|
2018-07-10 12:37:39 -07:00
|
|
|
($name:expr, $count:expr) => {
|
|
|
|
unsafe { $name.inc($count) };
|
2018-05-30 21:24:21 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-16 18:33:50 -07:00
|
|
|
macro_rules! inc_new_counter {
|
|
|
|
($name:expr, $count:expr) => {{
|
|
|
|
static mut INC_NEW_COUNTER: Counter = create_counter!($name, 10);
|
|
|
|
inc_counter!(INC_NEW_COUNTER, $count);
|
|
|
|
}};
|
|
|
|
($name:expr, $count:expr, $lograte:expr) => {{
|
|
|
|
static mut INC_NEW_COUNTER: Counter = create_counter!($name, $lograte);
|
|
|
|
inc_counter!(INC_NEW_COUNTER, $count);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
2018-05-30 21:24:21 -07:00
|
|
|
impl Counter {
|
2018-07-10 12:37:39 -07:00
|
|
|
pub fn inc(&mut self, events: usize) {
|
2018-05-30 21:24:21 -07:00
|
|
|
let counts = self.counts.fetch_add(events, Ordering::Relaxed);
|
|
|
|
let times = self.times.fetch_add(1, Ordering::Relaxed);
|
2018-07-10 15:14:59 -07:00
|
|
|
let lastlog = self.lastlog.load(Ordering::Relaxed);
|
2018-05-30 21:24:21 -07:00
|
|
|
if times % self.lograte == 0 && times > 0 {
|
|
|
|
info!(
|
2018-07-10 12:37:39 -07:00
|
|
|
"COUNTER:{{\"name\": \"{}\", \"counts\": {}, \"samples\": {}, \"now\": {}}}",
|
2018-05-30 21:24:21 -07:00
|
|
|
self.name,
|
|
|
|
counts,
|
|
|
|
times,
|
2018-06-08 10:11:07 -07:00
|
|
|
timing::timestamp(),
|
2018-05-30 21:24:21 -07:00
|
|
|
);
|
2018-07-10 12:37:39 -07:00
|
|
|
}
|
|
|
|
if times % INFLUX_RATE == 0 && times > 0 {
|
2018-07-10 15:14:59 -07:00
|
|
|
metrics::submit(
|
2018-07-16 15:55:54 -07:00
|
|
|
influxdb::Point::new(&format!("counter-{}", self.name))
|
2018-07-10 15:14:59 -07:00
|
|
|
.add_field(
|
|
|
|
"count",
|
|
|
|
influxdb::Value::Integer(counts as i64 - lastlog as i64),
|
|
|
|
)
|
|
|
|
.to_owned(),
|
|
|
|
);
|
|
|
|
self.lastlog
|
|
|
|
.compare_and_swap(lastlog, counts, Ordering::Relaxed);
|
2018-05-30 21:24:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use counter::Counter;
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
#[test]
|
|
|
|
fn test_counter() {
|
|
|
|
static mut COUNTER: Counter = create_counter!("test", 100);
|
|
|
|
let count = 1;
|
2018-07-10 12:37:39 -07:00
|
|
|
inc_counter!(COUNTER, count);
|
2018-05-30 21:24:21 -07:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(COUNTER.counts.load(Ordering::Relaxed), 1);
|
|
|
|
assert_eq!(COUNTER.times.load(Ordering::Relaxed), 1);
|
|
|
|
assert_eq!(COUNTER.lograte, 100);
|
2018-07-10 15:14:59 -07:00
|
|
|
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 0);
|
2018-05-30 21:24:21 -07:00
|
|
|
assert_eq!(COUNTER.name, "test");
|
|
|
|
}
|
2018-07-10 15:14:59 -07:00
|
|
|
for _ in 0..199 {
|
2018-07-10 12:37:39 -07:00
|
|
|
inc_counter!(COUNTER, 2);
|
2018-07-10 15:14:59 -07:00
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 199);
|
|
|
|
}
|
2018-07-10 12:37:39 -07:00
|
|
|
inc_counter!(COUNTER, 2);
|
2018-07-10 15:14:59 -07:00
|
|
|
unsafe {
|
|
|
|
assert_eq!(COUNTER.lastlog.load(Ordering::Relaxed), 399);
|
|
|
|
}
|
2018-05-30 21:24:21 -07:00
|
|
|
}
|
2018-07-16 18:33:50 -07:00
|
|
|
#[test]
|
|
|
|
fn test_inc_new_counter() {
|
|
|
|
//make sure that macros are syntactically correct
|
|
|
|
//the variable is internal to the macro scope so there is no way to introspect it
|
|
|
|
inc_new_counter!("counter-1", 1);
|
|
|
|
inc_new_counter!("counter-2", 1, 2);
|
|
|
|
}
|
2018-05-30 21:24:21 -07:00
|
|
|
}
|