Skip to main content

confluent_kafka/common/metrics/stats/
min.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! A sampled min (`org.apache.kafka.common.metrics.stats.Min`).
16
17use crate::common::metrics::stats::sampled_stat::{Sample, SampledStat, SampledStatKind};
18use crate::common::metrics::{Measurable, MetricConfig, Stat};
19
20/// A [`SampledStat`] that gives the min over its samples.
21#[derive(Debug)]
22#[doc(alias = "org.apache.kafka.common.metrics.stats.Min")]
23pub struct Min {
24    inner: SampledStat,
25}
26
27struct MinKind;
28
29impl SampledStatKind for MinKind {
30    fn update(&self, sample: &mut Sample, _config: &MetricConfig, value: f64, _now: i64) {
31        sample.value = sample.value.min(value);
32    }
33
34    fn combine(&self, samples: &[Sample], _config: &MetricConfig, _now: i64) -> f64 {
35        // Java seeds with Double.MAX_VALUE (not +inf) and uses Math.min.
36        let mut min = f64::MAX;
37        let mut count: i64 = 0;
38        for sample in samples {
39            min = min.min(sample.value);
40            count += sample.event_count;
41        }
42        if count == 0 { f64::NAN } else { min }
43    }
44}
45
46impl Min {
47    /// Create a `Min`.
48    #[doc(alias = "org.apache.kafka.common.metrics.stats.Min#Min")]
49    pub fn new() -> Self {
50        // Java seeds the initial value with Double.MAX_VALUE.
51        Self { inner: SampledStat::new(f64::MAX, Box::new(MinKind)) }
52    }
53}
54
55impl Default for Min {
56    fn default() -> Self {
57        Self::new()
58    }
59}
60
61impl Stat for Min {
62    fn record(&self, config: &MetricConfig, value: f64, time_ms: i64) {
63        self.inner.record(config, value, time_ms);
64    }
65}
66
67impl Measurable for Min {
68    fn measure(&self, config: &MetricConfig, now: i64) -> f64 {
69        self.inner.measure(config, now)
70    }
71}
72
73impl std::fmt::Display for Min {
74    /// Java's `SampledStat.toString()`, which `Min` inherits.
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        std::fmt::Display::fmt(&self.inner, f)
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn to_string_follows_java() {
86        assert_eq!(
87            Min::new().to_string(),
88            "SampledStat(initialValue=1.7976931348623157E308, current=0, samples=[])"
89        );
90    }
91
92    #[test]
93    fn min_of_records() {
94        let config = MetricConfig::new();
95        let min = Min::new();
96        for i in 0..10 {
97            min.record(&config, i as f64, 0);
98        }
99        assert_eq!(0.0, min.measure(&config, 0));
100    }
101
102    #[test]
103    fn nan_when_no_events() {
104        let config = MetricConfig::new();
105        let min = Min::new();
106        assert!(min.measure(&config, 0).is_nan());
107    }
108}