Skip to content

Commit 787788d

Browse files
fix: refactoring and review comments
1 parent b5e3150 commit 787788d

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

rs/ic_os/metrics_tool/src/lib.rs

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,24 @@ impl Metric {
4040
}
4141

4242
// TODO: formatting of floats
43-
pub fn to_string(&self) -> String {
43+
// Convert to prometheus exposition format
44+
pub fn to_prom_string(&self) -> String {
4445
let labels_str = if self.labels.is_empty() {
4546
String::new()
4647
} else {
47-
let labels: Vec<String> = self.labels.iter()
48+
let labels: Vec<String> = self
49+
.labels
50+
.iter()
4851
.map(|(k, v)| format!("{}=\"{}\"", k, v))
4952
.collect();
5053
format!("{{{}}}", labels.join(","))
5154
};
52-
format!("# HELP {} {}\n\
55+
format!(
56+
"# HELP {} {}\n\
5357
# TYPE {} counter\n\
54-
{}{} {}", self.name, self.annotation, self.name, self.name, labels_str, self.value)
58+
{}{} {}",
59+
self.name, self.annotation, self.name, self.name, labels_str, self.value
60+
)
5561
}
5662
}
5763

@@ -70,7 +76,7 @@ impl MetricsWriter {
7076
let path = Path::new(&self.file_path);
7177
let mut file = File::create(&path)?;
7278
for metric in metrics {
73-
writeln!(file, "{}", metric.to_string())?;
79+
writeln!(file, "{}", metric.to_prom_string())?;
7480
}
7581
Ok(())
7682
}
@@ -84,9 +90,12 @@ mod tests {
8490
let metric = Metric::new("test_metric", 123.45)
8591
.add_label("label1", "value1")
8692
.add_label("label2", "value2");
87-
assert_eq!(metric.to_string(), "# HELP test_metric Custom metric\n\
93+
assert_eq!(
94+
metric.to_prom_string(),
95+
"# HELP test_metric Custom metric\n\
8896
# TYPE test_metric counter\n\
89-
test_metric{label1=\"value1\",label2=\"value2\"} 123.45");
97+
test_metric{label1=\"value1\",label2=\"value2\"} 123.45"
98+
);
9099
}
91100

92101
#[test]
@@ -98,37 +107,49 @@ mod tests {
98107
let writer = MetricsWriter::new("/tmp/test_metrics.prom");
99108
writer.write_metrics(&metrics).unwrap();
100109
let content = std::fs::read_to_string("/tmp/test_metrics.prom").unwrap();
101-
assert!(content.contains("# HELP metric1 Custom metric\n\
110+
assert!(content.contains(
111+
"# HELP metric1 Custom metric\n\
102112
# TYPE metric1 counter\n\
103-
metric1 1"));
104-
assert!(content.contains("# HELP metric2 Custom metric\n\
113+
metric1 1"
114+
));
115+
assert!(content.contains(
116+
"# HELP metric2 Custom metric\n\
105117
# TYPE metric2 counter\n\
106-
metric2{label=\"value\"} 2"));
118+
metric2{label=\"value\"} 2"
119+
));
107120
}
108121

109122
#[test]
110123
fn test_metric_large_value() {
111124
let metric = Metric::new("large_value_metric", 1.0e64);
112-
assert_eq!(metric.to_string(), "# HELP large_value_metric Custom metric\n\
125+
assert_eq!(
126+
metric.to_prom_string(),
127+
"# HELP large_value_metric Custom metric\n\
113128
# TYPE large_value_metric counter\n\
114-
large_value_metric 10000000000000000000000000000000000000000000000000000000000000000");
129+
large_value_metric 10000000000000000000000000000000000000000000000000000000000000000"
130+
);
115131
}
116132

117-
118133
#[test]
119134
fn test_metric_without_labels() {
120135
let metric = Metric::new("no_label_metric", 42.0);
121-
assert_eq!(metric.to_string(), "# HELP no_label_metric Custom metric\n\
136+
assert_eq!(
137+
metric.to_prom_string(),
138+
"# HELP no_label_metric Custom metric\n\
122139
# TYPE no_label_metric counter\n\
123-
no_label_metric 42");
140+
no_label_metric 42"
141+
);
124142
}
125143

126144
#[test]
127145
fn test_metric_with_annotation() {
128146
let metric = Metric::with_annotation("annotated_metric", 99.9, "This is a test metric");
129-
assert_eq!(metric.to_string(), "# HELP annotated_metric This is a test metric\n\
147+
assert_eq!(
148+
metric.to_prom_string(),
149+
"# HELP annotated_metric This is a test metric\n\
130150
# TYPE annotated_metric counter\n\
131-
annotated_metric 99.9");
151+
annotated_metric 99.9"
152+
);
132153
}
133154

134155
#[test]
@@ -145,16 +166,22 @@ mod tests {
145166
let metric = Metric::new("multi_label_metric", 10.0)
146167
.add_label("foo", "bar")
147168
.add_label("version", "1.0.0");
148-
assert_eq!(metric.to_string(), "# HELP multi_label_metric Custom metric\n\
169+
assert_eq!(
170+
metric.to_prom_string(),
171+
"# HELP multi_label_metric Custom metric\n\
149172
# TYPE multi_label_metric counter\n\
150-
multi_label_metric{foo=\"bar\",version=\"1.0.0\"} 10");
173+
multi_label_metric{foo=\"bar\",version=\"1.0.0\"} 10"
174+
);
151175
}
152176

153177
#[test]
154178
fn test_metric_with_empty_annotation() {
155179
let metric = Metric::with_annotation("empty_annotation_metric", 5.5, "");
156-
assert_eq!(metric.to_string(), "# HELP empty_annotation_metric \n\
180+
assert_eq!(
181+
metric.to_prom_string(),
182+
"# HELP empty_annotation_metric \n\
157183
# TYPE empty_annotation_metric counter\n\
158-
empty_annotation_metric 5.5");
184+
empty_annotation_metric 5.5"
185+
);
159186
}
160-
}
187+
}

rs/ic_os/metrics_tool/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use anyhow::Result;
22
use clap::Parser;
33

4-
use std::path::Path;
54
use std::fs::File;
65
use std::io::{self, BufRead};
6+
use std::path::Path;
77

88
use ic_metrics_tool::{Metric, MetricsWriter};
99

1010
const INTERRUPT_FILTER: &str = "TLB shootdowns";
1111
const INTERRUPT_SOURCE: &str = "/proc/interrupts";
1212
const CUSTOM_METRICS_PROM: &str = "/run/node_exporter/collector_textfile/custom_metrics.prom";
1313
const TLB_SHOOTDOWN_METRIC_NAME: &str = "sum_tlb_shootdowns";
14+
const TLB_SHOOTDOWN_METRIC_ANNOTATION: &str = "Total TLB shootdowns";
1415

1516
#[derive(Parser)]
1617
struct MetricToolArgs {
@@ -34,8 +35,7 @@ fn get_sum_tlb_shootdowns() -> Result<u64> {
3435
for line in reader.lines() {
3536
let line = line?;
3637
if line.contains(INTERRUPT_FILTER) {
37-
let parts: Vec<&str> = line.split_whitespace().collect();
38-
for part in parts.iter().skip(1) {
38+
for part in line.split_whitespace().skip(1) {
3939
if let Ok(value) = part.parse::<u64>() {
4040
total_tlb_shootdowns += value;
4141
}
@@ -52,7 +52,8 @@ pub fn main() -> Result<()> {
5252
let tlb_shootdowns = get_sum_tlb_shootdowns()?;
5353

5454
let metrics = vec![
55-
Metric::new(TLB_SHOOTDOWN_METRIC_NAME, tlb_shootdowns as f64).add_annotation("Total TLB shootdowns"),
55+
Metric::new(TLB_SHOOTDOWN_METRIC_NAME, tlb_shootdowns as f64)
56+
.add_annotation(TLB_SHOOTDOWN_METRIC_ANNOTATION),
5657
];
5758
let writer = MetricsWriter::new(mpath.to_str().unwrap());
5859
writer.write_metrics(&metrics).unwrap();

0 commit comments

Comments
 (0)