Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(encoding): add impls for Encode*Value #173

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading