diff --git a/CHANGELOG.md b/CHANGELOG.md index 448d179f..3930fccb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,15 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [0.23.0] - unreleased +### Changed + +- `ConstCounter::new` now requires specifying the type of literal arguments, like this: `ConstCounter::new(42u64);`. + See [PR 173]. + - Update `prost` dependencies to `v0.12`. See [PR 198]. [PR 198]: https://github.com/prometheus/client_rust/pull/198 -- Add `EncodeGaugeValue` `i32` and `f32`, `EncodeCounterValue` `u32` and `f32` and `EncodeExemplarValue` `f32` and `u32` implementations. - See [PR 173]. +### Added + +- Support `i32`/`f32` for `Gauge` and `u32`/`f32` for `Counter`/`CounterWithExemplar`. + See [PR 173] and [PR 216]. [PR 173]: https://github.com/prometheus/client_rust/pull/173 +[PR 216]: https://github.com/prometheus/client_rust/pull/216 ## [0.22.3] diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 7cf22e4f..955396a6 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -717,9 +717,10 @@ mod tests { let mut registry = Registry::default(); registry.register("my_counter", "My counter", counter); + let counter_f32 = Counter::::default(); + registry.register("f32_counter", "Counter::", counter_f32); let counter_u32 = Counter::::default(); registry.register("u32_counter", "Counter::", counter_u32); - let mut encoded = String::new(); encode(&mut encoded, ®istry).unwrap(); @@ -783,6 +784,9 @@ mod tests { let gauge = Gauge::::default(); registry.register("u32_gauge", "Gauge::", gauge); + let gauge_f32 = Gauge::::default(); + registry.register("f32_gauge", "Gauge::", gauge_f32); + let gauge_i32 = Gauge::::default(); registry.register("i32_gauge", "Gauge::", gauge_i32); diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index 63d84ffc..d1fee30f 100644 --- a/src/metrics/counter.rs +++ b/src/metrics/counter.rs @@ -169,6 +169,31 @@ impl Atomic for AtomicU64 { } } +impl Atomic for AtomicU32 { + fn inc(&self) -> f32 { + self.inc_by(1.0) + } + + fn inc_by(&self, v: f32) -> f32 { + let mut old_u32 = self.load(Ordering::Relaxed); + let mut old_f32; + loop { + old_f32 = f32::from_bits(old_u32); + let new = f32::to_bits(old_f32 + v); + match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) { + Ok(_) => break, + Err(x) => old_u32 = x, + } + } + + old_f32 + } + + fn get(&self) -> f32 { + f32::from_bits(self.load(Ordering::Relaxed)) + } +} + impl TypedMetric for Counter { const TYPE: MetricType = MetricType::Counter; } diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index fcf7e6a7..5c8bef9b 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -262,6 +262,54 @@ impl Atomic for AtomicU64 { } } +impl Atomic for AtomicU32 { + fn inc(&self) -> f32 { + self.inc_by(1.0) + } + + fn inc_by(&self, v: f32) -> f32 { + let mut old_u32 = self.load(Ordering::Relaxed); + let mut old_f32; + loop { + old_f32 = f32::from_bits(old_u32); + let new = f32::to_bits(old_f32 + v); + match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) { + Ok(_) => break, + Err(x) => old_u32 = x, + } + } + + old_f32 + } + + fn dec(&self) -> f32 { + self.dec_by(1.0) + } + + fn dec_by(&self, v: f32) -> f32 { + let mut old_u32 = self.load(Ordering::Relaxed); + let mut old_f32; + loop { + old_f32 = f32::from_bits(old_u32); + let new = f32::to_bits(old_f32 - v); + match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) { + Ok(_) => break, + Err(x) => old_u32 = x, + } + } + + old_f32 + } + + fn set(&self, v: f32) -> f32 { + f32::from_bits(self.swap(f32::to_bits(v), Ordering::Relaxed)) + } + + fn get(&self) -> f32 { + f32::from_bits(self.load(Ordering::Relaxed)) + } +} + impl TypedMetric for Gauge { const TYPE: MetricType = MetricType::Gauge; }