Skip to content

Commit

Permalink
fix: refactoring and review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdullindfinity committed Oct 18, 2024
1 parent f8849ba commit 3c302a0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
73 changes: 50 additions & 23 deletions rs/ic_os/metrics_tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ impl Metric {
}

// TODO: formatting of floats
pub fn to_string(&self) -> String {
// Convert to prometheus exposition format
pub fn to_prom_string(&self) -> String {
let labels_str = if self.labels.is_empty() {
String::new()
} else {
let labels: Vec<String> = self.labels.iter()
let labels: Vec<String> = self
.labels
.iter()
.map(|(k, v)| format!("{}=\"{}\"", k, v))
.collect();
format!("{{{}}}", labels.join(","))
};
format!("# HELP {} {}\n\
format!(
"# HELP {} {}\n\
# TYPE {} counter\n\
{}{} {}", self.name, self.annotation, self.name, self.name, labels_str, self.value)
{}{} {}",
self.name, self.annotation, self.name, self.name, labels_str, self.value
)
}
}

Expand All @@ -70,7 +76,7 @@ impl MetricsWriter {
let path = Path::new(&self.file_path);
let mut file = File::create(&path)?;
for metric in metrics {
writeln!(file, "{}", metric.to_string())?;
writeln!(file, "{}", metric.to_prom_string())?;
}
Ok(())
}
Expand All @@ -84,9 +90,12 @@ mod tests {
let metric = Metric::new("test_metric", 123.45)
.add_label("label1", "value1")
.add_label("label2", "value2");
assert_eq!(metric.to_string(), "# HELP test_metric Custom metric\n\
assert_eq!(
metric.to_prom_string(),
"# HELP test_metric Custom metric\n\
# TYPE test_metric counter\n\
test_metric{label1=\"value1\",label2=\"value2\"} 123.45");
test_metric{label1=\"value1\",label2=\"value2\"} 123.45"
);
}

#[test]
Expand All @@ -98,37 +107,49 @@ mod tests {
let writer = MetricsWriter::new("/tmp/test_metrics.prom");
writer.write_metrics(&metrics).unwrap();
let content = std::fs::read_to_string("/tmp/test_metrics.prom").unwrap();
assert!(content.contains("# HELP metric1 Custom metric\n\
assert!(content.contains(
"# HELP metric1 Custom metric\n\
# TYPE metric1 counter\n\
metric1 1"));
assert!(content.contains("# HELP metric2 Custom metric\n\
metric1 1"
));
assert!(content.contains(
"# HELP metric2 Custom metric\n\
# TYPE metric2 counter\n\
metric2{label=\"value\"} 2"));
metric2{label=\"value\"} 2"
));
}

#[test]
fn test_metric_large_value() {
let metric = Metric::new("large_value_metric", 1.0e64);
assert_eq!(metric.to_string(), "# HELP large_value_metric Custom metric\n\
assert_eq!(
metric.to_prom_string(),
"# HELP large_value_metric Custom metric\n\
# TYPE large_value_metric counter\n\
large_value_metric 10000000000000000000000000000000000000000000000000000000000000000");
large_value_metric 10000000000000000000000000000000000000000000000000000000000000000"
);
}


#[test]
fn test_metric_without_labels() {
let metric = Metric::new("no_label_metric", 42.0);
assert_eq!(metric.to_string(), "# HELP no_label_metric Custom metric\n\
assert_eq!(
metric.to_prom_string(),
"# HELP no_label_metric Custom metric\n\
# TYPE no_label_metric counter\n\
no_label_metric 42");
no_label_metric 42"
);
}

#[test]
fn test_metric_with_annotation() {
let metric = Metric::with_annotation("annotated_metric", 99.9, "This is a test metric");
assert_eq!(metric.to_string(), "# HELP annotated_metric This is a test metric\n\
assert_eq!(
metric.to_prom_string(),
"# HELP annotated_metric This is a test metric\n\
# TYPE annotated_metric counter\n\
annotated_metric 99.9");
annotated_metric 99.9"
);
}

#[test]
Expand All @@ -145,16 +166,22 @@ mod tests {
let metric = Metric::new("multi_label_metric", 10.0)
.add_label("foo", "bar")
.add_label("version", "1.0.0");
assert_eq!(metric.to_string(), "# HELP multi_label_metric Custom metric\n\
assert_eq!(
metric.to_prom_string(),
"# HELP multi_label_metric Custom metric\n\
# TYPE multi_label_metric counter\n\
multi_label_metric{foo=\"bar\",version=\"1.0.0\"} 10");
multi_label_metric{foo=\"bar\",version=\"1.0.0\"} 10"
);
}

#[test]
fn test_metric_with_empty_annotation() {
let metric = Metric::with_annotation("empty_annotation_metric", 5.5, "");
assert_eq!(metric.to_string(), "# HELP empty_annotation_metric \n\
assert_eq!(
metric.to_prom_string(),
"# HELP empty_annotation_metric \n\
# TYPE empty_annotation_metric counter\n\
empty_annotation_metric 5.5");
empty_annotation_metric 5.5"
);
}
}
}
9 changes: 5 additions & 4 deletions rs/ic_os/metrics_tool/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use anyhow::Result;
use clap::Parser;

use std::path::Path;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;

use ic_metrics_tool::{Metric, MetricsWriter};

const INTERRUPT_FILTER: &str = "TLB shootdowns";
const INTERRUPT_SOURCE: &str = "/proc/interrupts";
const CUSTOM_METRICS_PROM: &str = "/run/node_exporter/collector_textfile/custom_metrics.prom";
const TLB_SHOOTDOWN_METRIC_NAME: &str = "sum_tlb_shootdowns";
const TLB_SHOOTDOWN_METRIC_ANNOTATION: &str = "Total TLB shootdowns";

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

let metrics = vec![
Metric::new(TLB_SHOOTDOWN_METRIC_NAME, tlb_shootdowns as f64).add_annotation("Total TLB shootdowns"),
Metric::new(TLB_SHOOTDOWN_METRIC_NAME, tlb_shootdowns as f64)
.add_annotation(TLB_SHOOTDOWN_METRIC_ANNOTATION),
];
let writer = MetricsWriter::new(mpath.to_str().unwrap());
writer.write_metrics(&metrics).unwrap();
Expand Down

0 comments on commit 3c302a0

Please sign in to comment.