confluent_kafka/common/metrics/stats/
value.rs1use std::sync::atomic::{AtomicU64, Ordering};
18
19use crate::common::metrics::{Measurable, MetricConfig, Stat};
20
21#[derive(Debug)]
23#[doc(alias = "org.apache.kafka.common.metrics.stats.Value")]
24pub struct Value {
28 value: AtomicU64,
29}
30
31impl Value {
32 #[doc(alias = "org.apache.kafka.common.metrics.stats.Value#Value")]
34 pub fn new() -> Self {
35 Self { value: AtomicU64::new(0.0f64.to_bits()) }
36 }
37}
38
39impl Default for Value {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl Stat for Value {
46 fn record(&self, _config: &MetricConfig, value: f64, _time_ms: i64) {
47 self.value.store(value.to_bits(), Ordering::SeqCst);
48 }
49}
50
51impl Measurable for Value {
52 fn measure(&self, _config: &MetricConfig, _now: i64) -> f64 {
53 f64::from_bits(self.value.load(Ordering::SeqCst))
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_value_records_last() {
63 let config = MetricConfig::new();
64 let v = Value::new();
65 assert_eq!(v.measure(&config, 0), 0.0);
66 v.record(&config, 1.0, 0);
67 assert_eq!(v.measure(&config, 0), 1.0);
68 v.record(&config, 42.5, 1);
69 assert_eq!(v.measure(&config, 1), 42.5);
70 v.record(&config, -3.0, 2);
72 assert_eq!(v.measure(&config, 2), -3.0);
73 }
74}