fix(metrics): use Prometheus-compatible metric names (#2312) (#2317)

Signed-off-by: Shunchao Hu <ankohuu@gmail.com>
This commit is contained in:
ankohuu
2026-04-05 21:39:10 +08:00
committed by GitHub
parent 77229fe426
commit 0e5fc4bec1
6 changed files with 54 additions and 22 deletions
+1 -1
View File
@@ -123,7 +123,7 @@ mod tests {
assert_eq!(metrics.len(), 8);
// Verify that metric names are properly generated from descriptors
assert!(metrics.iter().all(|m| m.name.starts_with("gauge.rustfs_system_cpu_")));
assert!(metrics.iter().all(|m| m.name.starts_with("rustfs_system_cpu_")));
}
#[test]
@@ -122,7 +122,7 @@ mod tests {
report_metrics(&metrics);
assert_eq!(metrics.len(), 8);
assert!(metrics.iter().all(|m| m.name.starts_with("gauge.rustfs_system_memory_")));
assert!(metrics.iter().all(|m| m.name.starts_with("rustfs_system_memory_")));
}
#[test]
@@ -51,14 +51,13 @@ impl MetricDescriptor {
}
}
/// Get the full metric name, including the prefix and formatting path
/// Get the full metric name in Prometheus style: <namespace>_<subsystem>_<name>
#[allow(dead_code)]
pub fn get_full_metric_name(&self) -> String {
let prefix = self.metric_type.as_prom();
let namespace = self.namespace.as_str();
let formatted_subsystem = self.subsystem.as_str();
format!("{}{}_{}_{}", prefix, namespace, formatted_subsystem, self.name.as_str())
format!("{}_{}_{}", namespace, formatted_subsystem, self.name.as_str())
}
/// check whether the label is in the label set
@@ -79,3 +78,36 @@ impl MetricDescriptor {
self.label_set.as_ref().unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn full_metric_name_uses_prometheus_convention_without_type_prefix() {
let descriptor = MetricDescriptor::new(
MetricName::ApiRequestsTotal,
MetricType::Counter,
"test help".to_string(),
vec![],
MetricNamespace::RustFS,
MetricSubsystem::ApiRequests,
);
assert_eq!(descriptor.get_full_metric_name(), "rustfs_api_requests_total");
}
#[test]
fn full_metric_name_formats_custom_subsystems_without_type_prefix() {
let descriptor = MetricDescriptor::new(
MetricName::Custom("latency_seconds".to_string()),
MetricType::Histogram,
"test help".to_string(),
vec![],
MetricNamespace::RustFS,
MetricSubsystem::new("/custom/path-metrics"),
);
assert_eq!(descriptor.get_full_metric_name(), "rustfs_custom_path_metrics_latency_seconds");
}
}
+2 -2
View File
@@ -110,7 +110,7 @@ mod tests {
assert_eq!(histogram_md.subsystem, MetricSubsystem::ApiRequests);
// Verify that the full metric name generated is formatted correctly
assert_eq!(histogram_md.get_full_metric_name(), "histogram.rustfs_api_requests_seconds_distribution");
assert_eq!(histogram_md.get_full_metric_name(), "rustfs_api_requests_seconds_distribution");
// Tests use custom subsystems
let custom_histogram_md = new_histogram_md(
@@ -123,7 +123,7 @@ mod tests {
// Verify the custom name and subsystem
assert_eq!(
custom_histogram_md.get_full_metric_name(),
"histogram.rustfs_custom_path_metrics_custom_latency_distribution"
"rustfs_custom_path_metrics_custom_latency_distribution"
);
}
}
@@ -233,7 +233,7 @@ mod tests {
MetricSubsystem::ApiRequests,
);
assert_eq!(md.get_full_metric_name(), "counter.rustfs_api_requests_total");
assert_eq!(md.get_full_metric_name(), "rustfs_api_requests_total");
let custom_md = MetricDescriptor::new(
MetricName::Custom("test_metric".to_string()),
@@ -244,6 +244,6 @@ mod tests {
MetricSubsystem::new("/custom/path-with-dash"),
);
assert_eq!(custom_md.get_full_metric_name(), "gauge.rustfs_custom_path_with_dash_test_metric");
assert_eq!(custom_md.get_full_metric_name(), "rustfs_custom_path_with_dash_test_metric");
}
}