Cleanup metrics (#4230)
This commit is contained in:
@@ -22,7 +22,7 @@ pub struct Counter {
|
||||
#[macro_export]
|
||||
macro_rules! create_counter {
|
||||
($name:expr, $lograte:expr, $metricsrate:expr) => {
|
||||
Counter {
|
||||
$crate::counter::Counter {
|
||||
name: $name,
|
||||
counts: std::sync::atomic::AtomicUsize::new(0),
|
||||
times: std::sync::atomic::AtomicUsize::new(0),
|
||||
@@ -51,7 +51,8 @@ macro_rules! inc_counter_info {
|
||||
#[macro_export]
|
||||
macro_rules! inc_new_counter {
|
||||
($name:expr, $count:expr, $level:expr, $lograte:expr, $metricsrate:expr) => {{
|
||||
static mut INC_NEW_COUNTER: Counter = create_counter!($name, $lograte, $metricsrate);
|
||||
static mut INC_NEW_COUNTER: $crate::counter::Counter =
|
||||
create_counter!($name, $lograte, $metricsrate);
|
||||
static INIT_HOOK: std::sync::Once = std::sync::ONCE_INIT;
|
||||
unsafe {
|
||||
INIT_HOOK.call_once(|| {
|
||||
@@ -88,7 +89,7 @@ impl Counter {
|
||||
}
|
||||
pub fn init(&mut self) {
|
||||
self.point = Some(
|
||||
influxdb::Point::new(&format!("counter-{}", self.name))
|
||||
influxdb::Point::new(&self.name)
|
||||
.add_field("count", influxdb::Value::Integer(0))
|
||||
.to_owned(),
|
||||
);
|
||||
@@ -98,7 +99,7 @@ impl Counter {
|
||||
let times = self.times.fetch_add(1, Ordering::Relaxed);
|
||||
let mut lograte = self.lograte.load(Ordering::Relaxed);
|
||||
if lograte == 0 {
|
||||
lograte = Counter::default_log_rate();
|
||||
lograte = Self::default_log_rate();
|
||||
self.lograte.store(lograte, Ordering::Relaxed);
|
||||
}
|
||||
let mut metricsrate = self.metricsrate.load(Ordering::Relaxed);
|
||||
@@ -188,9 +189,9 @@ mod tests {
|
||||
let _readlock = get_env_lock().read();
|
||||
//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_info!("counter-1", 1);
|
||||
inc_new_counter_info!("counter-2", 1, 3);
|
||||
inc_new_counter_info!("counter-3", 1, 2, 1);
|
||||
inc_new_counter_info!("1", 1);
|
||||
inc_new_counter_info!("2", 1, 3);
|
||||
inc_new_counter_info!("3", 1, 2, 1);
|
||||
}
|
||||
#[test]
|
||||
fn test_lograte() {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
//! The `metrics` module enables sending measurements to an InfluxDB instance
|
||||
//! The `metrics` module enables sending measurements to an `InfluxDB` instance
|
||||
|
||||
use influx_db_client as influxdb;
|
||||
use lazy_static::lazy_static;
|
||||
@@ -12,6 +12,51 @@ use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use sys_info::hostname;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! datapoint {
|
||||
(@field $point:ident $name:expr, $string:expr, String) => {
|
||||
$point.add_field(
|
||||
$name,
|
||||
$crate::influxdb::Value::String($string));
|
||||
};
|
||||
(@field $point:ident $name:expr, $value:expr, i64) => {
|
||||
$point.add_field(
|
||||
$name,
|
||||
$crate::influxdb::Value::Integer($value as i64));
|
||||
};
|
||||
(@field $point:ident $name:expr, $value:expr, f64) => {
|
||||
$point.add_field(
|
||||
$name,
|
||||
$crate::influxdb::Value::Float($value as f64));
|
||||
};
|
||||
(@field $point:ident $name:expr, $value:expr, bool) => {
|
||||
$point.add_field(
|
||||
$name,
|
||||
$crate::influxdb::Value::Boolean($value as bool));
|
||||
};
|
||||
|
||||
(@fields $point:ident) => {};
|
||||
(@fields $point:ident ($name:expr, $value:expr, $type:ident) , $($rest:tt)*) => {
|
||||
$crate::datapoint!(@field $point $name, $value, $type);
|
||||
$crate::datapoint!(@fields $point $($rest)*);
|
||||
};
|
||||
(@fields $point:ident ($name:expr, $value:expr, $type:ident)) => {
|
||||
$crate::datapoint!(@field $point $name, $value, $type);
|
||||
};
|
||||
|
||||
(@point $name:expr, $($fields:tt)+) => {
|
||||
{
|
||||
let mut point = $crate::influxdb::Point::new(&$name);
|
||||
$crate::datapoint!(@fields point $($fields)+);
|
||||
point
|
||||
}
|
||||
};
|
||||
|
||||
($name:expr, $($fields:tt)+) => {
|
||||
$crate::submit($crate::datapoint!(@point $name, $($fields)+));
|
||||
};
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref HOST_INFO: String = {
|
||||
let v = env::var("SOLANA_METRICS_DISPLAY_HOSTNAME")
|
||||
@@ -48,7 +93,7 @@ struct InfluxDbMetricsWriter {
|
||||
|
||||
impl InfluxDbMetricsWriter {
|
||||
fn new() -> Self {
|
||||
InfluxDbMetricsWriter {
|
||||
Self {
|
||||
client: Self::build_client().ok(),
|
||||
}
|
||||
}
|
||||
@@ -96,7 +141,7 @@ impl MetricsAgent {
|
||||
fn new(writer: Arc<MetricsWriter + Send + Sync>, write_frequency: Duration) -> Self {
|
||||
let (sender, receiver) = channel::<MetricsCommand>();
|
||||
thread::spawn(move || Self::run(&receiver, &writer, write_frequency));
|
||||
MetricsAgent { sender }
|
||||
Self { sender }
|
||||
}
|
||||
|
||||
fn run(
|
||||
@@ -379,4 +424,67 @@ mod test {
|
||||
agent.submit(point);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_datapoint() {
|
||||
macro_rules! matches {
|
||||
($e:expr, $p:pat) => {
|
||||
match $e {
|
||||
$p => true,
|
||||
_ => false,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
datapoint!("name", ("field name", "test".to_string(), String));
|
||||
datapoint!("name", ("field name", 12.34_f64, f64));
|
||||
datapoint!("name", ("field name", true, bool));
|
||||
datapoint!("name", ("field name", 1, i64));
|
||||
datapoint!("name", ("field name", 1, i64),);
|
||||
datapoint!("name", ("field1 name", 2, i64), ("field2 name", 2, i64));
|
||||
datapoint!("name", ("field1 name", 2, i64), ("field2 name", 2, i64),);
|
||||
datapoint!(
|
||||
"name",
|
||||
("field1 name", 2, i64),
|
||||
("field2 name", 2, i64),
|
||||
("field3 name", 3, i64)
|
||||
);
|
||||
datapoint!(
|
||||
"name",
|
||||
("field1 name", 2, i64),
|
||||
("field2 name", 2, i64),
|
||||
("field3 name", 3, i64),
|
||||
);
|
||||
|
||||
let point = datapoint!(@point "name", ("i64", 1, i64), ("String", "string".to_string(), String), ("f64", 12.34_f64, f64), ("bool", true, bool));
|
||||
assert_eq!(point.measurement, "name");
|
||||
assert!(matches!(
|
||||
point.fields.get("i64").unwrap(),
|
||||
influxdb::Value::Integer(1)
|
||||
));
|
||||
assert!(match point.fields.get("String").unwrap() {
|
||||
influxdb::Value::String(ref s) => {
|
||||
if s == "string" {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
});
|
||||
assert!(match point.fields.get("f64").unwrap() {
|
||||
influxdb::Value::Float(f) => {
|
||||
if *f == 12.34_f64 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
});
|
||||
assert!(matches!(
|
||||
point.fields.get("bool").unwrap(),
|
||||
influxdb::Value::Boolean(true)
|
||||
));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user