diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fa5ea8..448d179 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 diff --git a/src/collector.rs b/src/collector.rs index eb5bdeb..66270c4 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.rs b/src/encoding.rs index 3099399..c206a1c 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -563,6 +563,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>); @@ -619,6 +631,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>); @@ -658,6 +682,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)) diff --git a/src/encoding/text.rs b/src/encoding/text.rs index bf65aa0..216ddde 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 d2bb197..c7dec3b 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",