From 53d02ba3f3f7fdbddb45988356236b98a2cf24b5 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Sat, 28 Oct 2023 06:35:46 +0100 Subject: [PATCH 1/3] Fix missing trait implementations for value encode. Fixes #172. Signed-off-by: Orne Brocaar --- src/encoding.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/encoding.rs b/src/encoding.rs index a8ad8615..70f87f74 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -500,6 +500,18 @@ impl EncodeGaugeValue for f64 { } } +impl EncodeGaugeValue for i32 { + fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode_i64(*self as i64) + } +} + +impl EncodeGaugeValue for f32 { + fn encode(&self, encoder: &mut GaugeValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode_f64(*self as f64) + } +} + /// Encoder for a gauge value. #[derive(Debug)] pub struct GaugeValueEncoder<'a>(GaugeValueEncoderInner<'a>); @@ -552,6 +564,18 @@ impl EncodeCounterValue for f64 { } } +impl EncodeCounterValue for u32 { + fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode_u64(*self as u64) + } +} + +impl EncodeCounterValue for f32 { + fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode_f64(*self as f64) + } +} + /// Encoder for a counter value. #[derive(Debug)] pub struct CounterValueEncoder<'a>(CounterValueEncoderInner<'a>); @@ -591,6 +615,18 @@ impl EncodeExemplarValue for u64 { } } +impl EncodeExemplarValue for f32 { + fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode(*self as f64) + } +} + +impl EncodeExemplarValue for u32 { + fn encode(&self, mut encoder: ExemplarValueEncoder) -> Result<(), std::fmt::Error> { + encoder.encode(*self as f64) + } +} + impl<'a> From> for CounterValueEncoder<'a> { fn from(e: text::CounterValueEncoder<'a>) -> Self { CounterValueEncoder(CounterValueEncoderInner::Text(e)) From b1951add6d9b1e1fcbf4eb3e42dbc23396dcc800 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 17 Jul 2024 09:04:53 +0200 Subject: [PATCH 2/3] Add changelog entry Signed-off-by: Max Inden --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fa5ea86..448d179f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ 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]. + +[PR 173]: https://github.com/prometheus/client_rust/pull/173 + ## [0.22.3] ### Added From f5f4c7cd926a9852cf021a430e0dc983857ea5be Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 17 Jul 2024 09:23:27 +0200 Subject: [PATCH 3/3] Specify u64 explicitly Signed-off-by: Max Inden --- src/collector.rs | 2 +- src/encoding/text.rs | 2 +- src/registry.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/collector.rs b/src/collector.rs index eb5bdeb0..66270c41 100644 --- a/src/collector.rs +++ b/src/collector.rs @@ -23,7 +23,7 @@ use crate::encoding::DescriptorEncoder; /// /// impl Collector for MyCollector { /// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { -/// let counter = ConstCounter::new(42); +/// let counter = ConstCounter::new(42u64); /// let metric_encoder = encoder.encode_descriptor( /// "my_counter", /// "some help", diff --git a/src/encoding/text.rs b/src/encoding/text.rs index bf65aa06..216ddde0 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -1014,7 +1014,7 @@ mod tests { &self, mut encoder: crate::encoding::DescriptorEncoder, ) -> Result<(), std::fmt::Error> { - let counter = crate::metrics::counter::ConstCounter::new(42); + let counter = crate::metrics::counter::ConstCounter::new(42u64); let metric_encoder = encoder.encode_descriptor( &self.name, "some help", diff --git a/src/registry.rs b/src/registry.rs index d2bb1972..c7dec3ba 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -185,7 +185,7 @@ impl Registry { /// /// impl Collector for MyCollector { /// fn encode(&self, mut encoder: DescriptorEncoder) -> Result<(), std::fmt::Error> { - /// let counter = ConstCounter::new(42); + /// let counter = ConstCounter::new(42u64); /// let metric_encoder = encoder.encode_descriptor( /// "my_counter", /// "some help",