Update app_metric macro syntax

This commit is contained in:
Francis Lalonde 2018-01-15 15:32:57 -05:00
parent 2ea2dbd4b6
commit 0da03669bb
6 changed files with 20 additions and 74 deletions

View File

@ -9,8 +9,8 @@ use std::sync::Arc;
use std::sync::mpsc;
use std::thread;
mod_metric!(Aggregate, QUEUE_METRICS, DIPSTICK_METRICS.with_prefix("async_queue"));
mod_marker!(Aggregate, QUEUE_METRICS, SEND_FAILED: "send_failed");
mod_metric!(Aggregate, QUEUE_METRICS = DIPSTICK_METRICS.with_prefix("async_queue"));
mod_marker!(Aggregate, QUEUE_METRICS, { SEND_FAILED: "send_failed" });
/// Enqueue collected metrics for dispatch on background thread.
pub trait WithAsyncQueue

View File

@ -13,6 +13,14 @@ use std::fmt::Debug;
use socket::RetrySocket;
mod_metric!(Aggregate, GRAPHITE_METRICS = DIPSTICK_METRICS.with_prefix("graphite"));
mod_marker!(Aggregate, GRAPHITE_METRICS, {
SEND_ERR: "send_failed",
TRESHOLD_EXCEEDED: "bufsize_exceeded",
});
mod_counter!(Aggregate, GRAPHITE_METRICS, { SENT_BYTES: "sent_bytes" });
/// Send metrics to a graphite server at the address and port provided.
pub fn to_graphite<ADDR>(address: ADDR) -> error::Result<Chain<Graphite>>
where
@ -63,13 +71,6 @@ where
// TODO make configurable?
const BUFFER_FLUSH_THRESHOLD: usize = 65_536;
mod_metric!(Aggregate, GRAPHITE_METRICS, DIPSTICK_METRICS.with_prefix("graphite"));
mod_marker!(Aggregate, GRAPHITE_METRICS, {
SEND_ERR: "send_failed",
TRESHOLD_EXCEEDED: "bufsize_exceeded",
});
mod_counter!(Aggregate, GRAPHITE_METRICS, SENT_BYTES: "sent_bytes");
/// Key of a graphite metric.
#[derive(Debug, Clone)]
pub struct Graphite {

View File

@ -32,7 +32,6 @@ mod pcg32;
mod lru_cache;
pub mod error;
pub use error::*;
pub mod core;
pub use core::*;

View File

@ -23,7 +23,7 @@ macro_rules! time {
/// Define application-scoped metrics.
#[macro_export]
macro_rules! app_metric {
($type_param: ty, $metric_id: ident, $app_metrics: expr) => {
($type_param: ty, $metric_id: ident = $app_metrics: expr) => {
lazy_static! { pub static ref $metric_id: AppMetrics<$type_param> = $app_metrics; }
};
}
@ -34,9 +34,6 @@ macro_rules! app_marker {
($type_param: ty, $app_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(pub static ref $metric_id: AppMarker<$type_param> = $app_metrics.marker( $metric_name );)* }
};
($type_param: ty, $app_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { pub static ref $metric_id: AppMarker<$type_param> = $app_metrics.marker( $metric_name ); }
}
}
/// Define application-scoped counters.
@ -45,9 +42,6 @@ macro_rules! app_counter {
($type_param: ty, $app_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(pub static ref $metric_id: AppCounter<$type_param> = $app_metrics.counter( $metric_name );)* }
};
($type_param: ty, $app_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { pub static ref $metric_id: AppCounter<$type_param> = $app_metrics.counter( $metric_name ); }
}
}
/// Define application-scoped gauges.
@ -56,9 +50,6 @@ macro_rules! app_gauge {
($type_param: ty, $app_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(pub static ref $metric_id: AppGauge<$type_param> = $app_metrics.gauge( $metric_name );)* }
};
($type_param: ty, $app_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { pub static ref $metric_id: AppGauge<$type_param> = $app_metrics.gauge( $metric_name ); }
}
}
/// Define application-scoped timers.
@ -67,9 +58,6 @@ macro_rules! app_timer {
($type_param: ty, $app_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(pub static ref $metric_id: AppTimer<$type_param> = $app_metrics.timer( $metric_name );)* }
};
($type_param: ty, $app_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { pub static ref $metric_id: AppTimer<$type_param> = $app_metrics.timer( $metric_name ); }
}
}
@ -79,7 +67,7 @@ macro_rules! app_timer {
/// Define module-scoped metrics.
#[macro_export]
macro_rules! mod_metric {
($type_param: ty, $metric_id: ident, $mod_metrics: expr) => {
($type_param: ty, $metric_id: ident = $mod_metrics: expr) => {
lazy_static! { static ref $metric_id: AppMetrics<$type_param> = $mod_metrics; }
};
}
@ -90,9 +78,6 @@ macro_rules! mod_marker {
($type_param: ty, $mod_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(static ref $metric_id: AppMarker<$type_param> = $mod_metrics.marker( $metric_name );)* }
};
($type_param: ty, $mod_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { static ref $metric_id: AppMarker<$type_param> = $mod_metrics.marker( $metric_name ); }
}
}
/// Define module-scoped counters.
@ -101,9 +86,6 @@ macro_rules! mod_counter {
($type_param: ty, $mod_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(static ref $metric_id: AppCounter<$type_param> = $mod_metrics.counter( $metric_name );)* }
};
($type_param: ty, $mod_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { static ref $metric_id: AppCounter<$type_param> = $mod_metrics.counter( $metric_name ); }
}
}
/// Define module-scoped gauges.
@ -123,67 +105,48 @@ macro_rules! mod_timer {
($type_param: ty, $mod_metrics: expr, { $($metric_id: ident: $metric_name: expr),* $(,)* } ) => {
lazy_static! { $(static ref $metric_id: AppTimer<$type_param> = $mod_metrics.timer( $metric_name );)* }
};
($type_param: ty, $mod_metrics: expr, $metric_id: ident: $metric_name: expr) => {
lazy_static! { static ref $metric_id: AppTimer<$type_param> = $mod_metrics.timer( $metric_name ); }
}
}
#[cfg(test)]
mod test_app {
use self_metrics::*;
app_metric!(Aggregate, TEST_METRICS, DIPSTICK_METRICS.with_prefix("test_prefix"));
app_metric!(Aggregate, TEST_METRICS = DIPSTICK_METRICS.with_prefix("test_prefix"));
app_marker!(Aggregate, TEST_METRICS, {
M1: "failed",
M2: "success",
});
app_marker!(Aggregate, TEST_METRICS, M3: "warn");
app_counter!(Aggregate, TEST_METRICS, {
C1: "failed",
C2: "success",
});
app_counter!(Aggregate, TEST_METRICS, C3: "warn");
app_gauge!(Aggregate, TEST_METRICS, {
G1: "failed",
G2: "success",
});
app_gauge!(Aggregate, TEST_METRICS, G3: "warn");
app_timer!(Aggregate, TEST_METRICS, {
T1: "failed",
T2: "success",
});
app_timer!(Aggregate, TEST_METRICS, T3: "warn");
#[test]
fn call_macro_defined_metrics() {
M1.mark();
M2.mark();
M3.mark();
C1.count(1);
C2.count(2);
C3.count(3);
G1.value(1);
G2.value(2);
G3.value(3);
T1.interval_us(1);
T2.interval_us(2);
T3.interval_us(3);
}
}
@ -191,53 +154,40 @@ mod test_app {
mod test_mod {
use self_metrics::*;
mod_metric!(Aggregate, TEST_METRICS, DIPSTICK_METRICS.with_prefix("test_prefix"));
mod_metric!(Aggregate, TEST_METRICS = DIPSTICK_METRICS.with_prefix("test_prefix"));
mod_marker!(Aggregate, TEST_METRICS, {
M1: "failed",
M2: "success",
});
mod_marker!(Aggregate, TEST_METRICS, M3: "warn");
mod_counter!(Aggregate, TEST_METRICS, {
C1: "failed",
C2: "success",
});
mod_counter!(Aggregate, TEST_METRICS, C3: "warn");
mod_gauge!(Aggregate, TEST_METRICS, {
G1: "failed",
G2: "success",
});
mod_gauge!(Aggregate, TEST_METRICS, G3: "warn");
mod_timer!(Aggregate, TEST_METRICS, {
T1: "failed",
T2: "success",
});
mod_timer!(Aggregate, TEST_METRICS, T3: "warn");
#[test]
fn call_macro_defined_metrics() {
M1.mark();
M2.mark();
M3.mark();
C1.count(1);
C2.count(2);
C3.count(3);
G1.value(1);
G2.value(2);
G3.value(3);
T1.interval_us(1);
T2.interval_us(2);
T3.interval_us(3);
}
}

View File

@ -11,6 +11,7 @@ pub use namespace::*;
use output::to_void;
// TODO send to_dispatch()
fn build_aggregator() -> Chain<Aggregate> {
aggregate(summary, to_void())
}
@ -21,15 +22,10 @@ pub fn snapshot() -> Vec<ScoreSnapshot> {
}
fn build_self_metrics() -> AppMetrics<Aggregate> {
// TODO send to_map() when snapshot() is called
app_metrics(AGGREGATOR.clone()).with_prefix("dipstick")
}
lazy_static! {
static ref AGGREGATOR: Chain<Aggregate> = build_aggregator();
}
lazy_static! { static ref AGGREGATOR: Chain<Aggregate> = build_aggregator(); }
/// Application metrics are collected to the aggregator
app_metric!(Aggregate, DIPSTICK_METRICS, build_self_metrics());
app_metric!(Aggregate, DIPSTICK_METRICS = build_self_metrics());

View File

@ -9,9 +9,9 @@ use std::sync::{Arc, RwLock};
pub use std::net::ToSocketAddrs;
mod_metric!(Aggregate, STATSD_METRICS, DIPSTICK_METRICS.with_prefix("statsd"));
mod_marker!(Aggregate, STATSD_METRICS, SEND_ERR: "send_failed");
mod_counter!(Aggregate, STATSD_METRICS, SENT_BYTES: "sent_bytes");
mod_metric!(Aggregate, STATSD_METRICS = DIPSTICK_METRICS.with_prefix("statsd"));
mod_marker!(Aggregate, STATSD_METRICS, { SEND_ERR: "send_failed" });
mod_counter!(Aggregate, STATSD_METRICS, { SENT_BYTES: "sent_bytes" });
/// Send metrics to a statsd server at the address and port provided.
pub fn to_statsd<ADDR>(address: ADDR) -> error::Result<Chain<Statsd>>