dipstick/src/output/map.rs

100 lines
2.9 KiB
Rust
Raw Normal View History

2019-04-09 11:55:15 +00:00
use core::attributes::{Attributes, OnFlush, Prefixed, WithAttributes};
2018-10-26 01:20:47 +00:00
use core::input::InputKind;
2019-04-09 11:55:15 +00:00
use core::input::{Input, InputMetric, InputScope};
2018-10-26 01:20:47 +00:00
use core::name::MetricName;
2019-04-09 11:55:15 +00:00
use core::{Flush, MetricValue};
2018-09-14 10:47:31 +00:00
2018-06-11 18:56:40 +00:00
use std::collections::BTreeMap;
2019-03-08 21:59:51 +00:00
use std::error::Error;
2018-06-11 18:56:40 +00:00
2019-04-08 12:54:46 +00:00
use std::sync::{Arc, RwLock};
2019-04-09 11:55:15 +00:00
use {OutputMetric, OutputScope};
2019-04-08 12:54:46 +00:00
/// A BTreeMap wrapper to receive metrics or stats values.
2018-06-11 18:56:40 +00:00
/// Every received value for a metric replaces the previous one (if any).
2018-10-05 14:26:33 +00:00
#[derive(Clone, Default)]
2018-06-11 18:56:40 +00:00
pub struct StatsMap {
2019-03-11 18:22:55 +00:00
attributes: Attributes,
2018-06-11 18:56:40 +00:00
}
2019-03-11 18:22:55 +00:00
impl WithAttributes for StatsMap {
2019-04-09 11:55:15 +00:00
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
2019-03-11 18:22:55 +00:00
}
2019-04-08 12:54:46 +00:00
impl Input for StatsMap {
type SCOPE = StatsMapScope;
fn metrics(&self) -> Self::SCOPE {
StatsMapScope {
attributes: self.attributes.clone(),
inner: Arc::new(RwLock::new(BTreeMap::new())),
}
}
}
/// A BTreeMap wrapper to receive metrics or stats values.
/// Every received value for a metric replaces the previous one (if any).
#[derive(Clone, Default)]
pub struct StatsMapScope {
attributes: Attributes,
inner: Arc<RwLock<BTreeMap<String, MetricValue>>>,
}
impl WithAttributes for StatsMapScope {
2019-04-09 11:55:15 +00:00
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
2019-04-08 12:54:46 +00:00
}
impl InputScope for StatsMapScope {
fn new_metric(&self, name: MetricName, _kind: InputKind) -> InputMetric {
let name = self.prefix_append(name);
let write_to = self.inner.clone();
let name: String = name.join(".");
InputMetric::new(move |value, _labels| {
let _previous = write_to.write().expect("Lock").insert(name.clone(), value);
})
}
}
impl OutputScope for StatsMapScope {
2018-10-26 01:20:47 +00:00
fn new_metric(&self, name: MetricName, _kind: InputKind) -> OutputMetric {
2019-03-11 18:22:55 +00:00
let name = self.prefix_append(name);
2018-06-11 18:56:40 +00:00
let write_to = self.inner.clone();
let name: String = name.join(".");
2018-10-11 16:32:01 +00:00
OutputMetric::new(move |value, _labels| {
2019-04-08 12:54:46 +00:00
let _previous = write_to.write().expect("Lock").insert(name.clone(), value);
2018-06-11 18:56:40 +00:00
})
}
}
2019-04-08 12:54:46 +00:00
impl Flush for StatsMapScope {
2019-03-08 21:59:51 +00:00
fn flush(&self) -> Result<(), Box<Error + Send + Sync>> {
2019-03-11 18:22:55 +00:00
self.notify_flush_listeners();
2019-03-08 21:59:51 +00:00
Ok(())
}
}
2018-06-24 04:31:34 +00:00
2019-04-08 12:54:46 +00:00
impl From<StatsMapScope> for BTreeMap<String, MetricValue> {
fn from(map: StatsMapScope) -> Self {
// FIXME this is is possibly a full map copy, for no reason.
2018-06-26 15:46:55 +00:00
// into_inner() is what we'd really want here but would require some `unsafe`? don't know how to do this yet.
2019-04-08 12:54:46 +00:00
map.inner.read().unwrap().clone()
}
}
impl StatsMapScope {
/// Extract the backing BTreeMap.
pub fn into_map(self) -> BTreeMap<String, MetricValue> {
self.into()
2018-06-11 18:56:40 +00:00
}
2018-06-21 15:52:10 +00:00
}