forked from ava-labs/avalanche-types-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.rs
180 lines (162 loc) · 5.05 KB
/
metrics.rs
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
//! Support for Prometheus metrics.
use crate::proto::pb::io::prometheus::client::{
Bucket, Counter, Gauge, Histogram, LabelPair, Metric, MetricFamily, Quantile, Summary,
};
/// A list of LabelPair
#[derive(Clone, Debug)]
pub struct LabelPairs {
pub lps: Vec<LabelPair>,
}
impl From<&[prometheus::proto::LabelPair]> for LabelPairs {
fn from(item: &[prometheus::proto::LabelPair]) -> Self {
let mut lps = Vec::with_capacity(item.len());
for lp in item.iter() {
lps.push(LabelPair::from(lp));
}
Self { lps }
}
}
impl From<&prometheus::proto::LabelPair> for LabelPair {
fn from(item: &prometheus::proto::LabelPair) -> Self {
LabelPair {
name: Some(item.get_name().to_owned()),
value: Some(item.get_value().to_owned()),
}
}
}
impl From<&prometheus::proto::Gauge> for Gauge {
fn from(item: &prometheus::proto::Gauge) -> Self {
Gauge {
value: Some(item.get_value()),
}
}
}
impl From<&prometheus::proto::Counter> for Counter {
fn from(item: &prometheus::proto::Counter) -> Self {
Counter {
value: Some(item.get_value()),
exemplar: None,
}
}
}
impl From<&prometheus::proto::Histogram> for Histogram {
fn from(item: &prometheus::proto::Histogram) -> Self {
Histogram {
bucket: Buckets::from(item.get_bucket()).bs,
sample_count: Some(item.get_sample_count()),
sample_sum: Some(item.get_sample_sum()),
}
}
}
impl From<&prometheus::proto::Bucket> for Bucket {
fn from(item: &prometheus::proto::Bucket) -> Self {
Bucket {
cumulative_count: Some(item.get_cumulative_count()),
upper_bound: Some(item.get_upper_bound()),
exemplar: None,
}
}
}
/// A list of Bucket
#[derive(Clone, Debug)]
pub struct Buckets {
pub bs: Vec<Bucket>,
}
impl From<&[prometheus::proto::Bucket]> for Buckets {
fn from(item: &[prometheus::proto::Bucket]) -> Self {
let mut bs = Vec::with_capacity(item.len());
for b in item.iter() {
bs.push(Bucket::from(b));
}
Self { bs }
}
}
impl From<&prometheus::proto::Summary> for Summary {
fn from(item: &prometheus::proto::Summary) -> Self {
Summary {
sample_sum: Some(item.get_sample_sum()),
sample_count: Some(item.get_sample_count()),
quantile: Quantiles::from(item.get_quantile()).qs,
}
}
}
impl From<&prometheus::proto::Quantile> for Quantile {
fn from(item: &prometheus::proto::Quantile) -> Self {
Quantile {
quantile: Some(item.get_quantile()),
value: Some(item.get_value()),
}
}
}
/// A list of Quantile
#[derive(Clone, Debug)]
pub struct Quantiles {
pub qs: Vec<Quantile>,
}
impl From<&[prometheus::proto::Quantile]> for Quantiles {
fn from(item: &[prometheus::proto::Quantile]) -> Self {
let mut qs = Vec::with_capacity(item.len());
for q in item.iter() {
qs.push(Quantile::from(q));
}
Self { qs }
}
}
impl From<&prometheus::proto::Metric> for Metric {
fn from(item: &prometheus::proto::Metric) -> Self {
Metric {
label: LabelPairs::from(item.get_label()).lps,
counter: Some(Counter::from(item.get_counter())),
gauge: Some(Gauge::from(item.get_gauge())),
histogram: Some(Histogram::from(item.get_histogram())),
summary: Some(Summary::from(item.get_summary())),
timestamp_ms: Some(item.get_timestamp_ms()),
untyped: None, // deprecated
}
}
}
/// A list of MetricFamily
#[derive(Clone, Debug)]
pub struct MetricsFamilies {
pub mfs: Vec<MetricFamily>,
}
impl From<&Vec<prometheus::proto::MetricFamily>> for MetricsFamilies {
fn from(item: &Vec<prometheus::proto::MetricFamily>) -> Self {
let mut mfs = Vec::with_capacity(item.len());
for mf in item.iter() {
let mut metric = Vec::new();
for m in mf.get_metric().iter() {
metric.push(Metric::from(m));
}
mfs.push(MetricFamily {
name: Some(mf.get_name().to_owned()),
help: Some(mf.get_help().to_owned()),
r#type: Some(mf.get_field_type() as i32),
metric,
});
}
Self { mfs }
}
}
#[test]
fn test_gather_process() {
let families = vec![
"process_cpu_seconds_total",
"process_max_fds",
"process_open_fds",
"process_resident_memory_bytes",
"process_start_time_seconds",
"process_cpu_seconds_total",
"process_virtual_memory_bytes",
];
let metric_families = MetricsFamilies::from(&prometheus::gather()).mfs;
if !metric_families.is_empty() {
for (i, family) in families.iter().enumerate() {
if let Some(name) = &metric_families[i].name {
assert_eq!(name, family);
} else {
panic!("expected some {} found none", family)
}
}
}
}