mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-30 16:59:52 +00:00
fix(obs): export resettable metrics as gauges (#4432)
Co-authored-by: heihutu <heihutu@gmail.com>
This commit is contained in:
@@ -38,6 +38,18 @@ fn into_static_str(cache: &OnceLock<Mutex<HashMap<String, &'static str>>>, value
|
||||
intern_string(cache, value)
|
||||
}
|
||||
|
||||
fn counter_value_from_f64(value: f64) -> Option<u64> {
|
||||
if !value.is_finite() || value < 0.0 {
|
||||
return None;
|
||||
}
|
||||
|
||||
if value >= u64::MAX as f64 {
|
||||
Some(u64::MAX)
|
||||
} else {
|
||||
Some(value as u64)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn report_metrics(metrics: &[PrometheusMetric]) {
|
||||
for metric in metrics {
|
||||
let name = into_static_str(&NAME_CACHE, &metric.name);
|
||||
@@ -53,8 +65,10 @@ pub fn report_metrics(metrics: &[PrometheusMetric]) {
|
||||
|
||||
match metric.metric_type {
|
||||
MetricType::Counter => {
|
||||
let counter = counter!(name, &labels);
|
||||
counter.absolute(metric.value as u64);
|
||||
if let Some(value) = counter_value_from_f64(metric.value) {
|
||||
let counter = counter!(name, &labels);
|
||||
counter.absolute(value);
|
||||
}
|
||||
}
|
||||
MetricType::Gauge => {
|
||||
let gauge = gauge!(name, &labels);
|
||||
@@ -168,4 +182,17 @@ mod tests {
|
||||
assert_eq!(metric.metric_type, metric_type);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counter_value_from_f64_rejects_negative_and_nan_inputs() {
|
||||
assert_eq!(counter_value_from_f64(-1.0), None);
|
||||
assert_eq!(counter_value_from_f64(f64::NAN), None);
|
||||
assert_eq!(counter_value_from_f64(f64::NEG_INFINITY), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn counter_value_from_f64_clamps_large_values() {
|
||||
assert_eq!(counter_value_from_f64(42.0), Some(42));
|
||||
assert_eq!(counter_value_from_f64(u64::MAX as f64 * 2.0), Some(u64::MAX));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user