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
@@ -91,7 +91,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "gauge_rustfs_process_uptime_seconds{job=~\"$job\"}",
"expr": "rustfs_system_process_uptime_seconds{job=~\"$job\"}",
"legendFormat": "__auto",
"range": true,
"refId": "A"
@@ -223,7 +223,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum(gauge_rustfs_cluster_buckets_total{job=~\"$job\"})",
"expr": "sum(rustfs_cluster_buckets_total{job=~\"$job\"})",
"legendFormat": "__auto",
"range": true,
"refId": "A"
@@ -289,7 +289,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum(gauge_rustfs_cluster_objects_total{job=~\"$job\"})",
"expr": "sum(rustfs_cluster_objects_total{job=~\"$job\"})",
"legendFormat": "__auto",
"range": true,
"refId": "A"
@@ -427,7 +427,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum(gauge_rustfs_cluster_capacity_used_bytes{job=~\"$job\"})",
"expr": "sum(rustfs_cluster_capacity_used_bytes{job=~\"$job\"})",
"legendFormat": "Used",
"range": true,
"refId": "A"
@@ -438,7 +438,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum(gauge_rustfs_cluster_capacity_raw_total_bytes{job=~\"$job\"})",
"expr": "sum(rustfs_cluster_capacity_raw_total_bytes{job=~\"$job\"})",
"hide": false,
"legendFormat": "Total",
"range": true,
@@ -450,7 +450,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum(gauge_rustfs_cluster_capacity_used_bytes{job=~\"$job\"}) / sum(gauge_rustfs_cluster_capacity_raw_total_bytes{job=~\"$job\"})",
"expr": "sum(rustfs_cluster_capacity_used_bytes{job=~\"$job\"}) / sum(rustfs_cluster_capacity_raw_total_bytes{job=~\"$job\"})",
"hide": false,
"instant": false,
"legendFormat": "Percent",
@@ -1971,7 +1971,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (drive) (gauge_rustfs_node_disk_used_bytes{job=~\"$job\", drive=~\"$drive\"})",
"expr": "sum by (drive) (rustfs_node_disk_used_bytes{job=~\"$job\", drive=~\"$drive\"})",
"legendFormat": "{{drive}} (bytes)",
"range": true,
"refId": "A"
@@ -1982,7 +1982,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (drive) (gauge_rustfs_node_disk_used_bytes{job=~\"$job\", drive=~\"$drive\"}) / sum by (drive)(gauge_rustfs_node_disk_total_bytes{job=~\"$job\", drive=~\"$drive\"})",
"expr": "sum by (drive) (rustfs_node_disk_used_bytes{job=~\"$job\", drive=~\"$drive\"}) / sum by (drive)(rustfs_node_disk_total_bytes{job=~\"$job\", drive=~\"$drive\"})",
"hide": false,
"instant": false,
"legendFormat": "{{drive}} (percent)",
@@ -2473,7 +2473,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (job) (gauge_rustfs_process_cpu_usage{job=~\"$job\"})",
"expr": "sum by (job) (rustfs_system_process_cpu_usage{job=~\"$job\"})",
"legendFormat": "{{job}}",
"range": true,
"refId": "A"
@@ -2572,7 +2572,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (job) (gauge_rustfs_process_resident_memory_bytes{job=~\"$job\"})",
"expr": "sum by (job) (rustfs_system_process_resident_memory_bytes{job=~\"$job\"})",
"legendFormat": "{{job}}",
"range": true,
"refId": "B"
@@ -2670,7 +2670,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (job) (rate(gauge_rustfs_process_network_io{job=~\"$job\", direction=\"received\"}[5m]))",
"expr": "sum by (job) (rate(rustfs_system_process_network_io{job=~\"$job\", direction=\"received\"}[5m]))",
"legendFormat": "RX - {{job}}",
"range": true,
"refId": "C"
@@ -2681,7 +2681,7 @@
"uid": "${datasource}"
},
"editorMode": "code",
"expr": "sum by (job) (rate(gauge_rustfs_process_network_io{job=~\"$job\", direction=\"transmitted\"}[5m]))",
"expr": "sum by (job) (rate(rustfs_system_process_network_io{job=~\"$job\", direction=\"transmitted\"}[5m]))",
"legendFormat": "TX - {{job}}",
"range": true,
"refId": "D"
@@ -4922,4 +4922,4 @@
"title": "RustFS",
"uid": "rustfs-s3",
"version": 12
}
}
+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");
}
}