2017-09-02 05:16:39 +00:00
|
|
|
//! Dispatch metrics to multiple sinks.
|
|
|
|
|
2020-05-15 04:28:40 +00:00
|
|
|
use crate::attributes::{Attributes, MetricId, OnFlush, Prefixed, WithAttributes};
|
|
|
|
use crate::input::{Input, InputDyn, InputKind, InputMetric, InputScope};
|
|
|
|
use crate::name::MetricName;
|
|
|
|
use crate::Flush;
|
2018-09-14 10:47:31 +00:00
|
|
|
|
2018-03-26 16:57:40 +00:00
|
|
|
use std::sync::Arc;
|
2020-05-23 03:47:18 +00:00
|
|
|
use std::io;
|
2018-03-26 16:57:40 +00:00
|
|
|
|
2018-06-26 16:28:38 +00:00
|
|
|
/// Opens multiple scopes at a time from just as many outputs.
|
2019-04-16 21:29:55 +00:00
|
|
|
#[derive(Clone, Default)]
|
2018-06-26 20:41:05 +00:00
|
|
|
pub struct MultiInput {
|
2018-06-14 16:37:21 +00:00
|
|
|
attributes: Attributes,
|
2019-07-11 15:48:14 +00:00
|
|
|
inputs: Vec<Arc<dyn InputDyn + Send + Sync>>,
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|
2018-01-26 20:40:15 +00:00
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl Input for MultiInput {
|
|
|
|
type SCOPE = MultiInputScope;
|
2017-12-13 19:58:42 +00:00
|
|
|
|
2019-01-21 21:07:22 +00:00
|
|
|
fn metrics(&self) -> Self::SCOPE {
|
2019-04-16 21:29:55 +00:00
|
|
|
#[allow(clippy::redundant_closure)]
|
|
|
|
let scopes = self.inputs.iter().map(|input| input.input_dyn()).collect();
|
2018-06-26 20:41:05 +00:00
|
|
|
MultiInputScope {
|
2018-06-14 16:37:21 +00:00
|
|
|
attributes: self.attributes.clone(),
|
2018-06-26 16:28:38 +00:00
|
|
|
scopes,
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|
2017-12-13 19:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 20:40:15 +00:00
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl MultiInput {
|
2019-01-21 21:07:22 +00:00
|
|
|
/// Create a new multi-input dispatcher.
|
2019-04-09 11:55:15 +00:00
|
|
|
#[deprecated(since = "0.7.2", note = "Use new()")]
|
2019-01-21 21:07:22 +00:00
|
|
|
pub fn input() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
2018-07-03 15:44:29 +00:00
|
|
|
|
2019-01-21 21:07:22 +00:00
|
|
|
/// Create a new multi-input dispatcher.
|
|
|
|
pub fn new() -> Self {
|
2019-04-16 21:44:15 +00:00
|
|
|
Self::default()
|
2018-07-03 15:44:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-21 21:07:22 +00:00
|
|
|
/// Returns a clone of the dispatch with the new target added to the list.
|
2018-07-03 15:44:29 +00:00
|
|
|
pub fn add_target<OUT: Input + Send + Sync + 'static>(&self, out: OUT) -> Self {
|
2018-06-14 16:37:21 +00:00
|
|
|
let mut cloned = self.clone();
|
2019-04-16 21:29:55 +00:00
|
|
|
cloned.inputs.push(Arc::new(out));
|
2018-06-14 16:37:21 +00:00
|
|
|
cloned
|
2018-01-26 20:40:15 +00:00
|
|
|
}
|
2018-03-13 13:54:12 +00:00
|
|
|
}
|
2018-06-14 16:37:21 +00:00
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl WithAttributes for MultiInput {
|
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
|
|
|
|
}
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 16:28:38 +00:00
|
|
|
/// Dispatch metric values to a list of scopes.
|
2018-10-04 17:13:55 +00:00
|
|
|
#[derive(Clone, Default)]
|
2018-06-26 20:41:05 +00:00
|
|
|
pub struct MultiInputScope {
|
2018-06-14 16:37:21 +00:00
|
|
|
attributes: Attributes,
|
2019-07-11 15:48:14 +00:00
|
|
|
scopes: Vec<Arc<dyn InputScope + Send + Sync>>,
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl MultiInputScope {
|
2018-06-26 16:28:38 +00:00
|
|
|
/// Create a new multi scope dispatcher with no scopes.
|
2018-06-21 15:52:10 +00:00
|
|
|
pub fn new() -> Self {
|
2018-06-26 20:41:05 +00:00
|
|
|
MultiInputScope {
|
2018-06-21 15:52:10 +00:00
|
|
|
attributes: Attributes::default(),
|
2018-06-26 16:28:38 +00:00
|
|
|
scopes: vec![],
|
2018-06-21 15:52:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 21:07:22 +00:00
|
|
|
/// Add a target to the dispatch list.
|
|
|
|
/// Returns a clone of the original object.
|
2018-07-03 15:44:29 +00:00
|
|
|
pub fn add_target<IN: InputScope + Send + Sync + 'static>(&self, scope: IN) -> Self {
|
2018-06-21 15:52:10 +00:00
|
|
|
let mut cloned = self.clone();
|
2018-06-26 16:28:38 +00:00
|
|
|
cloned.scopes.push(Arc::new(scope));
|
2018-06-21 15:52:10 +00:00
|
|
|
cloned
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 15:44:29 +00:00
|
|
|
impl InputScope for MultiInputScope {
|
2018-10-26 01:20:47 +00:00
|
|
|
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric {
|
|
|
|
let name = &self.prefix_append(name);
|
2019-04-09 11:55:15 +00:00
|
|
|
let metrics: Vec<InputMetric> = self
|
|
|
|
.scopes
|
|
|
|
.iter()
|
2018-06-26 16:28:38 +00:00
|
|
|
.map(move |scope| scope.new_metric(name.clone(), kind))
|
2018-06-20 14:04:14 +00:00
|
|
|
.collect();
|
2019-05-17 19:43:30 +00:00
|
|
|
InputMetric::new(
|
|
|
|
MetricId::forge("multi", name.clone()),
|
|
|
|
move |value, labels| {
|
|
|
|
for metric in &metrics {
|
|
|
|
metric.write(value, labels.clone())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|
2018-06-24 04:31:34 +00:00
|
|
|
}
|
2018-06-14 16:37:21 +00:00
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl Flush for MultiInputScope {
|
2020-05-23 03:47:18 +00:00
|
|
|
fn flush(&self) -> io::Result<()> {
|
2019-03-08 21:59:51 +00:00
|
|
|
self.notify_flush_listeners();
|
2018-06-26 16:28:38 +00:00
|
|
|
for w in &self.scopes {
|
2018-06-14 16:37:21 +00:00
|
|
|
w.flush()?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 20:41:05 +00:00
|
|
|
impl WithAttributes for MultiInputScope {
|
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
|
|
|
|
}
|
2018-06-14 16:37:21 +00:00
|
|
|
}
|