use prometheus_client::{metrics::MetricType, registry::Unit};
#[derive(Debug)]
pub struct MetricDescriptor {
pub name: &'static str,
pub field_name: &'static str,
pub metric_type: MetricType,
pub unit: Option<Unit>,
pub help: &'static str,
}
impl MetricDescriptor {
pub(crate) fn full_name(&self) -> String {
if let Some(unit) = &self.unit {
format!("{}_{}", self.name, unit.as_str())
} else {
self.name.to_owned()
}
}
}
#[derive(Debug)]
pub struct MetricGroupDescriptor {
pub crate_name: &'static str,
pub crate_version: &'static str,
pub module_path: &'static str,
pub name: &'static str,
pub line: u32,
pub metrics: &'static [MetricDescriptor],
}
#[derive(Debug, Clone, Copy)]
pub struct FullMetricDescriptor {
pub group: &'static MetricGroupDescriptor,
pub metric: &'static MetricDescriptor,
}
impl FullMetricDescriptor {
pub(crate) fn new(
group: &'static MetricGroupDescriptor,
metric: &'static MetricDescriptor,
) -> Self {
Self { group, metric }
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use assert_matches::assert_matches;
use super::*;
use crate::{metrics::Metrics, tests::TestMetrics};
#[test]
fn describing_metrics() {
let descriptor = TestMetrics::DESCRIPTOR;
assert_eq!(descriptor.crate_name, "vise");
assert_eq!(descriptor.module_path, "vise::tests");
assert_eq!(descriptor.name, "TestMetrics");
let metric_descriptors: HashMap<_, _> = descriptor
.metrics
.iter()
.map(|descriptor| (descriptor.field_name, descriptor))
.collect();
let gauge_descriptor = metric_descriptors["gauge"];
assert_matches!(gauge_descriptor.metric_type, MetricType::Gauge);
assert_matches!(gauge_descriptor.unit, Some(Unit::Bytes));
assert_eq!(gauge_descriptor.help, "");
let histogram_descriptor = metric_descriptors["histogram"];
assert_matches!(histogram_descriptor.metric_type, MetricType::Histogram);
assert_matches!(histogram_descriptor.unit, None);
assert_eq!(
histogram_descriptor.help,
"Histogram with inline bucket specification"
);
}
}