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
122
123
124
use std::sync::Arc;
use std::collections::{HashMap};
use core::name::{NameParts, MetricName};
#[derive(Debug, Clone, Copy)]
pub enum Sampling {
Random(f64)
}
#[derive(Debug, Clone, Copy)]
pub enum Buffering {
BufferSize(usize),
Unlimited,
}
#[derive(Debug, Clone, Default)]
pub struct Attributes {
naming: NameParts,
sampling: Option<Sampling>,
buffering: Option<Buffering>,
}
pub trait WithAttributes: Clone {
fn get_attributes(&self) -> &Attributes;
fn mut_attributes(&mut self) -> &mut Attributes;
fn with_attributes<F: Fn(&mut Attributes)>(&self, edit: F) -> Self {
let mut cloned = self.clone();
(edit)(cloned.mut_attributes());
cloned
}
}
pub trait Prefixed {
fn get_prefixes(&self) -> &NameParts;
fn add_prefix<S: Into<String>>(&self, name: S) -> Self;
fn prefix_append<S: Into<MetricName>>(&self, name: S) -> MetricName {
name.into().append(self.get_prefixes().clone())
}
fn prefix_prepend<S: Into<MetricName>>(&self, name: S) -> MetricName {
name.into().prepend(self.get_prefixes().clone())
}
}
pub trait Label {
fn get_label(&self) -> &Arc<HashMap<String, String>>;
fn label(&self, name: &str) -> Self;
}
impl<T: WithAttributes> Prefixed for T {
fn get_prefixes(&self) -> &NameParts {
&self.get_attributes().naming
}
fn add_prefix<S: Into<String>>(&self, name: S) -> Self {
let name = name.into();
self.with_attributes(|new_attr| new_attr.naming.push_back(name.clone()))
}
}
pub trait Sampled: WithAttributes {
fn sampled(&self, sampling: Sampling) -> Self {
self.with_attributes(|new_attr| new_attr.sampling = Some(sampling))
}
fn get_sampling(&self) -> Option<Sampling> {
self.get_attributes().sampling
}
}
pub trait Buffered: WithAttributes {
fn buffered(&self, buffering: Buffering) -> Self {
self.with_attributes(|new_attr| new_attr.buffering = Some(buffering))
}
fn get_buffering(&self) -> Option<Buffering> {
self.get_attributes().buffering
}
}