#![allow(clippy::expect_used, dead_code)]
use prometheus_parse::{Sample, Scrape, Value};
use crate::telemetry::HistogramConfig;
pub(crate) fn scrape(text: &str) -> Scrape {
Scrape::parse(
text.lines()
.map(|line| Ok::<String, std::io::Error>(line.to_owned())),
)
.expect("the exposition text is parsed by prometheus-parse")
}
pub(crate) fn samples<'a>(scrape: &'a Scrape, metric: &str) -> Vec<&'a Sample> {
scrape
.samples
.iter()
.filter(|sample| sample.metric == metric)
.collect()
}
pub(crate) fn value(sample: &Sample) -> Option<f64> {
match sample.value {
Value::Counter(value) | Value::Gauge(value) | Value::Untyped(value) => Some(value),
Value::Histogram(_) | Value::Summary(_) => None,
}
}
pub(crate) fn scalar(scrape: &Scrape, metric: &str) -> Option<f64> {
samples(scrape, metric).into_iter().find_map(value)
}
pub(crate) fn histogram_buckets(scrape: &Scrape, metric: &str) -> Vec<(f64, f64)> {
samples(scrape, metric)
.into_iter()
.find_map(|sample| match &sample.value {
Value::Histogram(buckets) => Some(
buckets
.iter()
.map(|bucket| (bucket.less_than, bucket.count))
.collect(),
),
_ => None,
})
.unwrap_or_default()
}
pub(crate) fn histogram_config(tag: &str, yaml: &str) -> HistogramConfig {
let directory =
std::env::temp_dir().join(format!("metricsplay-tests-{}-{tag}", std::process::id()));
std::fs::create_dir_all(&directory).expect("the temporary directory");
std::fs::write(directory.join("config.yaml"), yaml).expect("the configuration file");
let loaded =
redup_config::load::<HistogramConfig>(&redup_config::Source::directory(&directory))
.expect("the configuration is accepted");
std::fs::remove_dir_all(&directory).ok();
loaded.value
}