confluent_kafka/common/metrics/stats/cumulative_count.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 non-sampled count maintained over all time
16//! (`org.apache.kafka.common.metrics.stats.CumulativeCount`).
17
18use crate::common::metrics::stats::CumulativeSum;
19use crate::common::metrics::{Measurable, MetricConfig, Stat};
20
21/// A non-sampled version of `WindowedCount` maintained over all time.
22///
23/// This is a special kind of [`CumulativeSum`] that always records `1` instead
24/// of the provided value. In other words, it counts the number of `record`
25/// invocations, instead of summing the recorded values.
26#[derive(Debug)]
27#[doc(alias = "org.apache.kafka.common.metrics.stats.CumulativeCount")]
28// Java extends `CumulativeSum` and overrides `record` to pass `1`; Rust
29// composes a `CumulativeSum` and forwards `1.0` on each record.
30pub struct CumulativeCount {
31 inner: CumulativeSum,
32}
33
34impl CumulativeCount {
35 /// Create a `CumulativeCount` initialized to `0`.
36 #[doc(alias = "org.apache.kafka.common.metrics.stats.CumulativeCount#CumulativeCount")]
37 pub fn new() -> Self {
38 Self { inner: CumulativeSum::new() }
39 }
40}
41
42impl Default for CumulativeCount {
43 fn default() -> Self {
44 Self::new()
45 }
46}
47
48impl Stat for CumulativeCount {
49 fn record(&self, config: &MetricConfig, _value: f64, time_ms: i64) {
50 // Always record 1, mirroring Java's super.record(config, 1, timeMs).
51 self.inner.record(config, 1.0, time_ms);
52 }
53}
54
55impl Measurable for CumulativeCount {
56 fn measure(&self, config: &MetricConfig, now: i64) -> f64 {
57 self.inner.measure(config, now)
58 }
59}
60
61impl std::fmt::Display for CumulativeCount {
62 /// Java's `CumulativeSum.toString()`, which `CumulativeCount` inherits.
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 std::fmt::Display::fmt(&self.inner, f)
65 }
66}
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn to_string_follows_java() {
74 assert_eq!(CumulativeCount::new().to_string(), "CumulativeSum(total=0.0)");
75 }
76
77 #[test]
78 fn test_cumulative_count() {
79 let config = MetricConfig::new();
80 let c = CumulativeCount::new();
81 assert_eq!(c.measure(&config, 0), 0.0);
82 // Value passed is ignored; each record increments by 1.
83 c.record(&config, 100.0, 0);
84 c.record(&config, 0.0, 1);
85 c.record(&config, -50.0, 2);
86 assert_eq!(c.measure(&config, 2), 3.0);
87 }
88}