From 5860f71f9ffe4db15d54442350a8c6556c6a8a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gillot-Lamure?= Date: Tue, 2 Jul 2024 12:09:25 +0200 Subject: [PATCH 1/5] feat(portability): Support Counter:: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Léo Gillot-Lamure --- src/encoding/text.rs | 3 +++ src/metrics/counter.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 216ddde..ed51fba 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -717,6 +717,9 @@ 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 mut encoded = String::new(); encode(&mut encoded, ®istry).unwrap(); diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index 63d84ff..d1fee30 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; } From 210478948b700f44bc41f1753c81827a14649231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gillot-Lamure?= Date: Wed, 17 Jul 2024 19:44:15 +0200 Subject: [PATCH 2/5] feat(portability): Support Gauge:: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Léo Gillot-Lamure --- src/encoding/text.rs | 3 +++ src/metrics/gauge.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/encoding/text.rs b/src/encoding/text.rs index ed51fba..0186bc4 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -783,6 +783,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 mut encoded = String::new(); encode(&mut encoded, ®istry).unwrap(); diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index fcf7e6a..5c8bef9 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; } From 3e25da082ad9a1e8b4a21055469a072a5eaec15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gillot-Lamure?= Date: Wed, 17 Jul 2024 20:30:25 +0200 Subject: [PATCH 3/5] chore(changelog): Add changelog for breaking change in #173 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Léo Gillot-Lamure --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 448d179..9933b32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ 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]. From d8234398e403ae9609d15885aa3d44007e7b8669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Gillot-Lamure?= Date: Wed, 17 Jul 2024 20:30:59 +0200 Subject: [PATCH 4/5] chore(changelog): Add changelog for new 32-bit types support MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Léo Gillot-Lamure --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9933b32..3930fcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,10 +16,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [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] From 8dbc774af9d712c86568ff69739dd3a9b43dbd83 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 17 Jul 2024 20:56:08 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Signed-off-by: Max Inden --- src/encoding/text.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/encoding/text.rs b/src/encoding/text.rs index 3c14492..955396a 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -719,10 +719,8 @@ mod tests { 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();