1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::attributes::{Attributes, MetricId, OnFlush, Prefixed, WithAttributes};
use crate::input::{Input, InputDyn, InputKind, InputMetric, InputScope};
use crate::name::MetricName;
use crate::Flush;
use std::io;
use std::sync::Arc;
#[derive(Clone, Default)]
pub struct MultiInput {
attributes: Attributes,
inputs: Vec<Arc<dyn InputDyn + Send + Sync>>,
}
impl Input for MultiInput {
type SCOPE = MultiInputScope;
fn metrics(&self) -> Self::SCOPE {
#[allow(clippy::redundant_closure)]
let scopes = self.inputs.iter().map(|input| input.input_dyn()).collect();
MultiInputScope {
attributes: self.attributes.clone(),
scopes,
}
}
}
impl MultiInput {
#[deprecated(since = "0.7.2", note = "Use new()")]
pub fn input() -> Self {
Self::new()
}
pub fn new() -> Self {
Self::default()
}
pub fn add_target<OUT: Input + Send + Sync + 'static>(&self, out: OUT) -> Self {
let mut cloned = self.clone();
cloned.inputs.push(Arc::new(out));
cloned
}
}
impl WithAttributes for MultiInput {
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
}
#[derive(Clone, Default)]
pub struct MultiInputScope {
attributes: Attributes,
scopes: Vec<Arc<dyn InputScope + Send + Sync>>,
}
impl MultiInputScope {
pub fn new() -> Self {
MultiInputScope {
attributes: Attributes::default(),
scopes: vec![],
}
}
pub fn add_target<IN: InputScope + Send + Sync + 'static>(&self, scope: IN) -> Self {
let mut cloned = self.clone();
cloned.scopes.push(Arc::new(scope));
cloned
}
}
impl InputScope for MultiInputScope {
fn new_metric(&self, name: MetricName, kind: InputKind) -> InputMetric {
let name = &self.prefix_append(name);
let metrics: Vec<InputMetric> = self
.scopes
.iter()
.map(move |scope| scope.new_metric(name.clone(), kind))
.collect();
InputMetric::new(
MetricId::forge("multi", name.clone()),
move |value, labels| {
for metric in &metrics {
metric.write(value, labels.clone())
}
},
)
}
}
impl Flush for MultiInputScope {
fn flush(&self) -> io::Result<()> {
self.notify_flush_listeners();
for w in &self.scopes {
w.flush()?;
}
Ok(())
}
}
impl WithAttributes for MultiInputScope {
fn get_attributes(&self) -> &Attributes {
&self.attributes
}
fn mut_attributes(&mut self) -> &mut Attributes {
&mut self.attributes
}
}