use crate::MetricType;
use crate::metrics::report::PrometheusMetric;
use crate::telemetry::dial9::{is_configured, is_enabled, is_supported, runtime_stats_snapshot};
#[derive(Debug, Clone, Default)]
pub struct Dial9Stats {
pub errors_total: u64,
pub disk_usage_bytes: u64,
pub active_sessions: u64,
}
pub fn collect_dial9_metrics(stats: &Dial9Stats) -> Vec<PrometheusMetric> {
let mut metrics = vec![
PrometheusMetric::new(
"rustfs_dial9_supported",
MetricType::Gauge,
"Whether this binary was compiled with dial9 telemetry support (1) or not (0)",
bool_gauge(is_supported()),
),
PrometheusMetric::new(
"rustfs_dial9_configured",
MetricType::Gauge,
"Whether dial9 telemetry is requested via environment configuration (1) or not (0)",
bool_gauge(is_configured()),
),
];
if !is_enabled() {
return metrics;
}
metrics.extend([
PrometheusMetric::new(
"rustfs_dial9_active_sessions",
MetricType::Gauge,
"Number of active dial9 telemetry sessions",
stats.active_sessions as f64,
),
PrometheusMetric::new(
"rustfs_dial9_disk_usage_bytes",
MetricType::Gauge,
"Current disk usage by dial9 trace files",
stats.disk_usage_bytes as f64,
),
PrometheusMetric::new(
"rustfs_dial9_errors_total",
MetricType::Counter,
"Total number of dial9 telemetry setup errors",
stats.errors_total as f64,
),
]);
metrics
}
fn bool_gauge(value: bool) -> f64 {
if value { 1.0 } else { 0.0 }
}
pub fn collect_current_dial9_metrics() -> Vec<PrometheusMetric> {
let snapshot = runtime_stats_snapshot();
let stats = Dial9Stats {
errors_total: snapshot.errors_total,
disk_usage_bytes: snapshot.disk_usage_bytes,
active_sessions: snapshot.active_sessions,
};
collect_dial9_metrics(&stats)
}
pub fn is_dial9_enabled() -> bool {
is_enabled()
}
#[cfg(test)]
mod tests {
use super::*;
fn metric_names(metrics: &[PrometheusMetric]) -> Vec<String> {
metrics.iter().map(|m| m.name.to_string()).collect()
}
#[test]
fn always_reports_support_and_intent() {
let names = metric_names(&collect_dial9_metrics(&Dial9Stats::default()));
assert!(names.iter().any(|n| n == "rustfs_dial9_supported"));
assert!(names.iter().any(|n| n == "rustfs_dial9_configured"));
}
#[test]
fn omits_session_metrics_when_telemetry_is_not_running() {
if is_enabled() {
return;
}
let metrics = collect_dial9_metrics(&Dial9Stats::default());
assert_eq!(metrics.len(), 2);
let names = metric_names(&metrics);
assert!(!names.iter().any(|n| n == "rustfs_dial9_disk_usage_bytes"));
assert!(!names.iter().any(|n| n == "rustfs_dial9_active_sessions"));
}
#[test]
fn supported_gauge_tracks_compile_time_feature() {
let metrics = collect_dial9_metrics(&Dial9Stats::default());
let supported = metrics
.iter()
.find(|m| m.name == "rustfs_dial9_supported")
.expect("supported gauge is always exported");
assert_eq!(supported.value, bool_gauge(cfg!(feature = "dial9")));
}
#[test]
fn bool_gauge_maps_to_one_and_zero() {
assert_eq!(bool_gauge(true), 1.0);
assert_eq!(bool_gauge(false), 0.0);
}
}