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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::prometheus_metric_builder::PrometheusMetricBuilder;
use crate::{MetricType, No, RenderToPrometheus};
#[derive(Debug)]
pub struct PrometheusMetric<'a> {
pub(crate) counter_name: &'a str,
pub(crate) counter_type: MetricType,
pub(crate) counter_help: &'a str,
pub(crate) rendered_instances: Vec<String>,
}
impl<'a> PrometheusMetric<'a> {
#[deprecated(since = "1.0.0", note = "Please use the build function instead")]
pub fn new(
counter_name: &'a str,
counter_type: MetricType,
counter_help: &'a str,
) -> PrometheusMetric<'a> {
PrometheusMetric {
counter_name,
counter_type,
counter_help,
rendered_instances: Vec::new(),
}
}
/// Call this function to construct a
/// [`PrometheusMetric`].
///
/// Example:
///
/// ```
/// use prometheus_exporter_base::prelude::*;
///
/// let prometheus_metric = PrometheusMetric::build()
/// .with_name("folder_size")
/// .with_metric_type(MetricType::Counter)
/// .with_help("Size of the folder")
/// .build();
/// ```
pub fn build() -> PrometheusMetricBuilder<'a, No, No, No> {
PrometheusMetricBuilder::new()
}
fn render_header(&self) -> String {
format!(
"# HELP {} {}\n# TYPE {} {}\n",
self.counter_name, self.counter_help, self.counter_name, self.counter_type
)
}
/// Call this function to add a [`PrometheusInstance`] rendered
/// String to the instances list. You can call this function as many
/// times as needed. It's up to you to give meaningful
/// labels however.
///
/// **Note**: the instance will be rendered immediately so you can
/// reuse the [`PrometheusInstance`] if needed.
///
/// [`PrometheusInstance`]: struct.PrometheusInstance.html
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use prometheus_exporter_base::prelude::*;
///
/// let mut pc = PrometheusMetric::build()
/// .with_name("folder_size")
/// .with_metric_type(MetricType::Counter)
/// .with_help("Size of the folder")
/// .build();
///
/// for folder in &vec!["/var/log", "/tmp"] {
/// pc.render_and_append_instance(
/// &PrometheusInstance::new()
/// .with_label("folder", folder.as_ref())
/// .with_value(500) // this is just an example!
/// .with_current_timestamp()
/// .expect("error getting the current UNIX epoch"),
/// );
/// }
///
/// let final_string = pc.render();
///
/// ```
pub fn render_and_append_instance(
&mut self,
rendereable_instance: &dyn RenderToPrometheus,
) -> &mut Self {
self.rendered_instances.push(rendereable_instance.render());
self
}
pub fn render(&self) -> String {
let mut s = self.render_header();
for rendered_instance in &self.rendered_instances {
s.push_str(&format!("{}{}", self.counter_name, rendered_instance));
s.push('\n');
}
s
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{MetricType, PrometheusInstance};
#[test]
fn test_header() {
let pc = PrometheusMetric::build()
.with_name("pippo_total")
.with_metric_type(MetricType::Counter)
.with_help("Number of pippos")
.build();
assert_eq!(
pc.render_header(),
"# HELP pippo_total Number of pippos\n# TYPE pippo_total counter\n"
);
}
#[test]
fn test_labels() {
let mut pc = PrometheusMetric::build()
.with_name("pippo_total")
.with_metric_type(MetricType::Counter)
.with_help("Number of pippos")
.build();
for number in 0..4 {
pc.render_and_append_instance(
&PrometheusInstance::new()
.with_label("food", "chicken")
.with_label("instance", &*number.to_string())
.with_value(number),
);
}
assert_eq!(
pc.render(),
"# HELP pippo_total Number of pippos\n\
# TYPE pippo_total counter\n\
pippo_total{food=\"chicken\",instance=\"0\"} 0\n\
pippo_total{food=\"chicken\",instance=\"1\"} 1\n\
pippo_total{food=\"chicken\",instance=\"2\"} 2\n\
pippo_total{food=\"chicken\",instance=\"3\"} 3\n"
);
}
#[test]
fn test_no_labels() {
let final_string = PrometheusMetric::build()
.with_name("gigino_total")
.with_metric_type(MetricType::Counter)
.with_help("Number of giginos")
.build()
.render_and_append_instance(&PrometheusInstance::new().with_value(100))
.render();
assert_eq!(
final_string,
"# HELP gigino_total Number of giginos\n\
# TYPE gigino_total counter\n\
gigino_total 100\n"
);
let final_string = PrometheusMetric::build()
.with_name("gigino_total")
.with_metric_type(MetricType::Counter)
.with_help("Number of giginos")
.build()
.render_and_append_instance(
&PrometheusInstance::new()
.with_value(100)
.with_timestamp(9223372036854775807),
)
.render();
assert_eq!(
final_string,
"# HELP gigino_total Number of giginos\n\
# TYPE gigino_total counter\n\
gigino_total 100 9223372036854775807\n"
);
}
}