use core::fmt::Write;
use crate::{
Counter, Gauge, Op, dynamic,
histograms::{self, BUCKET_BOUNDS_US, BUCKETS},
metrics,
};
pub fn dump_text() -> String {
let mut out = String::with_capacity(4096);
out.push_str("# HELP skeg_ops_total Total number of operations served.\n");
out.push_str("# TYPE skeg_ops_total counter\n");
for &op in &Op::ALL {
let total = metrics::op_total(op);
let _ = writeln!(&mut out, "skeg_ops_total{{op=\"{}\"}} {}", op.name(), total);
}
out.push_str("\n# HELP skeg_op_duration_seconds Operation duration in seconds.\n");
out.push_str("# TYPE skeg_op_duration_seconds histogram\n");
for &op in &Op::ALL {
let mut cumulative: u64 = 0;
for (b, &le_us) in BUCKET_BOUNDS_US.iter().enumerate() {
cumulative += histograms::bucket(op, b);
let le_str = if b == BUCKETS - 1 {
String::from("+Inf")
} else {
let secs = le_us as f64 / 1_000_000.0;
format!("{secs:.6}")
};
let _ = writeln!(
&mut out,
"skeg_op_duration_seconds_bucket{{op=\"{}\",le=\"{}\"}} {}",
op.name(),
le_str,
cumulative
);
}
let count = histograms::count(op);
let sum_secs = histograms::sum_us(op) as f64 / 1_000_000.0;
let _ = writeln!(
&mut out,
"skeg_op_duration_seconds_count{{op=\"{}\"}} {}",
op.name(),
count
);
let _ = writeln!(
&mut out,
"skeg_op_duration_seconds_sum{{op=\"{}\"}} {}",
op.name(),
sum_secs
);
}
out.push('\n');
for &c in &Counter::ALL {
let _ = writeln!(&mut out, "# TYPE {} counter", c.name());
let _ = writeln!(&mut out, "{} {}", c.name(), metrics::counter(c));
}
out.push('\n');
for &g in &Gauge::ALL {
let _ = writeln!(&mut out, "# TYPE {} gauge", g.name());
let _ = writeln!(&mut out, "{} {}", g.name(), metrics::gauge(g));
}
dynamic::dump_text(&mut out);
out
}