Skip to content

Commit

Permalink
feat(encoding): add impls for Encode*Value (#173)
Browse files Browse the repository at this point in the history
Signed-off-by: Orne Brocaar <[email protected]>
Signed-off-by: Max Inden <[email protected]>
Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
brocaar and mxinden authored Jul 17, 2024
1 parent 24a8ada commit 87442a2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>);
Expand Down Expand Up @@ -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>);
Expand Down Expand Up @@ -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<text::CounterValueEncoder<'a>> for CounterValueEncoder<'a> {
fn from(e: text::CounterValueEncoder<'a>) -> Self {
CounterValueEncoder(CounterValueEncoderInner::Text(e))
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 87442a2

Please sign in to comment.