bump metrics-core to 0.2.0
This commit is contained in:
parent
d405cdeaf2
commit
74062e58c1
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "metrics-core"
|
||||
version = "0.1.2"
|
||||
version = "0.2.0"
|
||||
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
|
@ -33,29 +33,29 @@
|
|||
//! Histograms are a convenient way to measure behavior not only at the median, but at the edges of
|
||||
//! normal operating behavior.
|
||||
|
||||
/// A value that exports collected metrics.
|
||||
pub trait MetricsExporter {
|
||||
/// Exports a counter.
|
||||
/// A value that records metrics.
|
||||
pub trait MetricsRecorder {
|
||||
/// Records a counter.
|
||||
///
|
||||
/// From the perspective of an exportr, a counter and gauge are essentially identical, insofar
|
||||
/// From the perspective of an recorder, a counter and gauge are essentially identical, insofar
|
||||
/// as they are both a single value tied to a key. From the perspective of a collector,
|
||||
/// counters and gauges usually have slightly different modes of operation.
|
||||
///
|
||||
/// For the sake of flexibility on the exportr side, both are provided.
|
||||
fn export_counter<K: AsRef<str>>(&mut self, key: K, value: u64);
|
||||
fn record_counter<K: AsRef<str>>(&mut self, key: K, value: u64);
|
||||
|
||||
/// Exports a gauge.
|
||||
/// Records a gauge.
|
||||
///
|
||||
/// From the perspective of a exportr, a counter and gauge are essentially identical, insofar
|
||||
/// From the perspective of a recorder, a counter and gauge are essentially identical, insofar
|
||||
/// as they are both a single value tied to a key. From the perspective of a collector,
|
||||
/// counters and gauges usually have slightly different modes of operation.
|
||||
///
|
||||
/// For the sake of flexibility on the exportr side, both are provided.
|
||||
fn export_gauge<K: AsRef<str>>(&mut self, key: K, value: i64);
|
||||
fn record_gauge<K: AsRef<str>>(&mut self, key: K, value: i64);
|
||||
|
||||
/// Exports a histogram.
|
||||
/// Records a histogram.
|
||||
///
|
||||
/// Exporters are expected to tally their own histogram views, which means this method will be
|
||||
/// called for each observed value in the underlying histogram.
|
||||
fn export_histogram<K: AsRef<str>>(&mut self, key: K, value: u64);
|
||||
/// Recorders are expected to tally their own histogram views, so this will be called with all
|
||||
/// of the underlying observed values, and callers will need to process them accordingly.
|
||||
fn record_histogram<K: AsRef<str>>(&mut self, key: K, values: &[u64]);
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ opt-level = 3
|
|||
lto = true
|
||||
|
||||
[dependencies]
|
||||
metrics-core = "^0.1"
|
||||
metrics-core = "^0.2"
|
||||
crossbeam-channel = "^0.3"
|
||||
parking_lot = "^0.7"
|
||||
fnv = "^1.0"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use super::histogram::HistogramSnapshot;
|
||||
use metrics_core::MetricsExporter;
|
||||
use metrics_core::MetricsRecorder;
|
||||
use std::fmt::Display;
|
||||
|
||||
/// A typed metric measurement, used in snapshots.
|
||||
|
@ -62,21 +62,17 @@ impl Snapshot {
|
|||
.push(TypedMeasurement::ValueHistogram(key.to_string(), h));
|
||||
}
|
||||
|
||||
/// Exports this [`Snapshot`] to the provided [`MetricsExporter`].
|
||||
pub fn export<M: MetricsExporter>(&self, exporter: &mut M) {
|
||||
/// Records this [`Snapshot`] to the provided [`MetricsRecorder`].
|
||||
pub fn record<R: MetricsRecorder>(&self, recorder: &mut R) {
|
||||
for measurement in &self.measurements {
|
||||
match measurement {
|
||||
TypedMeasurement::Counter(key, value) => exporter.export_counter(key, *value),
|
||||
TypedMeasurement::Gauge(key, value) => exporter.export_gauge(key, *value),
|
||||
TypedMeasurement::Counter(key, value) => recorder.record_counter(key, *value),
|
||||
TypedMeasurement::Gauge(key, value) => recorder.record_gauge(key, *value),
|
||||
TypedMeasurement::TimingHistogram(key, hs) => {
|
||||
for value in hs.values() {
|
||||
exporter.export_histogram(key, *value);
|
||||
}
|
||||
recorder.record_histogram(key, hs.values().as_slice());
|
||||
}
|
||||
TypedMeasurement::ValueHistogram(key, hs) => {
|
||||
for value in hs.values() {
|
||||
exporter.export_histogram(key, *value);
|
||||
}
|
||||
recorder.record_histogram(key, hs.values().as_slice());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -90,17 +86,17 @@ impl Snapshot {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{HistogramSnapshot, MetricsExporter, Snapshot, TypedMeasurement};
|
||||
use super::{HistogramSnapshot, MetricsRecorder, Snapshot, TypedMeasurement};
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default)]
|
||||
struct MockExporter {
|
||||
struct MockRecorder {
|
||||
counter: HashMap<String, u64>,
|
||||
gauge: HashMap<String, i64>,
|
||||
histogram: HashMap<String, u64>,
|
||||
}
|
||||
|
||||
impl MockExporter {
|
||||
impl MockRecorder {
|
||||
pub fn get_counter_value(&self, key: &String) -> Option<&u64> {
|
||||
self.counter.get(key)
|
||||
}
|
||||
|
@ -114,18 +110,18 @@ mod tests {
|
|||
}
|
||||
}
|
||||
|
||||
impl MetricsExporter for MockExporter {
|
||||
fn export_counter<K: AsRef<str>>(&mut self, key: K, value: u64) {
|
||||
impl MetricsRecorder for MockRecorder {
|
||||
fn record_counter<K: AsRef<str>>(&mut self, key: K, value: u64) {
|
||||
let entry = self.counter.entry(key.as_ref().to_owned()).or_insert(0);
|
||||
*entry += value;
|
||||
}
|
||||
|
||||
fn export_gauge<K: AsRef<str>>(&mut self, key: K, value: i64) {
|
||||
fn record_gauge<K: AsRef<str>>(&mut self, key: K, value: i64) {
|
||||
let entry = self.gauge.entry(key.as_ref().to_owned()).or_insert(0);
|
||||
*entry += value;
|
||||
}
|
||||
|
||||
fn export_histogram<K: AsRef<str>>(&mut self, key: K, value: u64) {
|
||||
fn record_histogram<K: AsRef<str>>(&mut self, key: K, value: u64) {
|
||||
let entry = self.histogram.entry(key.as_ref().to_owned()).or_insert(0);
|
||||
*entry += value;
|
||||
}
|
||||
|
@ -145,7 +141,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_snapshot_exporter() {
|
||||
fn test_snapshot_recorder() {
|
||||
let key = "ok".to_owned();
|
||||
let mut snapshot = Snapshot::default();
|
||||
snapshot.set_count(key.clone(), 7);
|
||||
|
@ -155,11 +151,11 @@ mod tests {
|
|||
let histogram = HistogramSnapshot::new(hvalues);
|
||||
snapshot.set_timing_histogram(key.clone(), histogram);
|
||||
|
||||
let mut exporter = MockExporter::default();
|
||||
snapshot.export(&mut exporter);
|
||||
let mut recorder = MockRecorder::default();
|
||||
snapshot.export(&mut recorder);
|
||||
|
||||
assert_eq!(exporter.get_counter_value(&key), Some(&7));
|
||||
assert_eq!(exporter.get_gauge_value(&key), Some(&42));
|
||||
assert_eq!(exporter.get_histogram_value(&key), Some(&174));
|
||||
assert_eq!(recorder.get_counter_value(&key), Some(&7));
|
||||
assert_eq!(recorder.get_gauge_value(&key), Some(&42));
|
||||
assert_eq!(recorder.get_histogram_value(&key), Some(&174));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue