dipstick/src/stats.rs

95 lines
3.2 KiB
Rust
Raw Normal View History

2020-05-15 04:28:40 +00:00
//! Definitions of standard aggregated statistic types and functions
2018-10-26 01:20:47 +00:00
2020-05-15 04:28:40 +00:00
use crate::input::InputKind;
use crate::name::MetricName;
use crate::MetricValue;
2018-10-26 01:20:47 +00:00
/// Possibly aggregated scores.
#[derive(Debug, Clone, Copy)]
pub enum ScoreType {
/// Number of times the metric was used.
2018-12-18 21:06:03 +00:00
Count(isize),
2018-10-26 01:20:47 +00:00
/// Sum of metric values reported.
2018-12-18 21:06:03 +00:00
Sum(isize),
2018-12-20 21:19:03 +00:00
/// Biggest value observed.
2018-12-18 21:06:03 +00:00
Max(isize),
2018-12-20 21:19:03 +00:00
/// Smallest value observed.
2018-12-18 21:06:03 +00:00
Min(isize),
2018-10-26 01:20:47 +00:00
/// Average value (hit count / sum, non-atomic)
Mean(f64),
/// Mean rate (hit count / period length in seconds, non-atomic)
Rate(f64),
}
/// A predefined export strategy reporting all aggregated stats for all metric types.
/// Resulting stats are named by appending a short suffix to each metric's name.
#[allow(dead_code)]
2019-03-18 01:42:00 +00:00
pub fn stats_all(
kind: InputKind,
name: MetricName,
score: ScoreType,
) -> Option<(InputKind, MetricName, MetricValue)> {
2018-10-26 01:20:47 +00:00
match score {
ScoreType::Count(hit) => Some((InputKind::Counter, name.make_name("count"), hit)),
ScoreType::Sum(sum) => Some((kind, name.make_name("sum"), sum)),
ScoreType::Mean(mean) => Some((kind, name.make_name("mean"), mean.round() as MetricValue)),
ScoreType::Max(max) => Some((InputKind::Gauge, name.make_name("max"), max)),
ScoreType::Min(min) => Some((InputKind::Gauge, name.make_name("min"), min)),
2019-03-18 01:42:00 +00:00
ScoreType::Rate(rate) => Some((
InputKind::Gauge,
name.make_name("rate"),
rate.round() as MetricValue,
)),
2018-10-26 01:20:47 +00:00
}
}
/// A predefined export strategy reporting the average value for every non-marker metric.
/// Marker metrics export their hit count instead.
/// Since there is only one stat per metric, there is no risk of collision
/// and so exported stats copy their metric's name.
#[allow(dead_code)]
2019-03-18 01:42:00 +00:00
pub fn stats_average(
kind: InputKind,
name: MetricName,
score: ScoreType,
) -> Option<(InputKind, MetricName, MetricValue)> {
2018-10-26 01:20:47 +00:00
match kind {
InputKind::Marker => match score {
ScoreType::Count(count) => Some((InputKind::Counter, name, count)),
_ => None,
},
_ => match score {
ScoreType::Mean(avg) => Some((InputKind::Gauge, name, avg.round() as MetricValue)),
_ => None,
},
}
}
/// A predefined single-stat-per-metric export strategy:
/// - Timers and Counters each export their sums
/// - Markers each export their hit count
/// - Gauges each export their average
/// Since there is only one stat per metric, there is no risk of collision
/// and so exported stats copy their metric's name.
#[allow(dead_code)]
2019-03-18 01:42:00 +00:00
pub fn stats_summary(
kind: InputKind,
name: MetricName,
score: ScoreType,
) -> Option<(InputKind, MetricName, MetricValue)> {
2018-10-26 01:20:47 +00:00
match kind {
InputKind::Marker => match score {
ScoreType::Count(count) => Some((InputKind::Counter, name, count)),
_ => None,
},
InputKind::Counter | InputKind::Timer => match score {
ScoreType::Sum(sum) => Some((kind, name, sum)),
_ => None,
},
2018-12-20 21:19:03 +00:00
InputKind::Gauge | InputKind::Level => match score {
2018-10-26 01:20:47 +00:00
ScoreType::Mean(mean) => Some((InputKind::Gauge, name, mean.round() as MetricValue)),
_ => None,
},
}
2019-03-18 01:42:00 +00:00
}