Skip to content

Commit

Permalink
chore(portability): Support u32 for Counters
Browse files Browse the repository at this point in the history
Signed-off-by: Léo Gillot-Lamure <[email protected]>
  • Loading branch information
navaati committed Jul 2, 2024
1 parent 4b78df1 commit cbc406f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Support all platforms with 32 bit atomics lacking 64 bit atomics.
See [PR 203].
- Added `Counter<u32, AtomicU32>` implementation.
See [PR 206].

[PR 203]: https://github.com/prometheus/client_rust/pull/203
[PR 206]: https://github.com/prometheus/client_rust/pull/206

## [0.22.2]

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
10 changes: 10 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@ pub trait EncodeCounterValue {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error>;
}

impl EncodeCounterValue for u32 {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_u32(*self)
}
}

impl EncodeCounterValue for u64 {
fn encode(&self, encoder: &mut CounterValueEncoder) -> Result<(), std::fmt::Error> {
encoder.encode_u64(*self)
Expand All @@ -630,6 +636,10 @@ enum CounterValueEncoderInner<'a> {
}

impl<'a> CounterValueEncoder<'a> {
fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
for_both_mut!(self, CounterValueEncoderInner, e, e.encode_u32(v))
}

fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
for_both_mut!(self, CounterValueEncoderInner, e, e.encode_f64(v))
}
Expand Down
4 changes: 4 additions & 0 deletions src/encoding/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ pub(crate) struct CounterValueEncoder<'a> {
}

impl<'a> CounterValueEncoder<'a> {
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
self.encode_u64(v as u64)
}

pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
*self.value = openmetrics_data_model::counter_value::Total::DoubleValue(v);
Ok(())
Expand Down
11 changes: 10 additions & 1 deletion src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,12 @@ impl<'a> std::fmt::Debug for CounterValueEncoder<'a> {
}

impl<'a> CounterValueEncoder<'a> {
pub fn encode_u32(&mut self, v: u32) -> Result<(), std::fmt::Error> {
self.writer.write_str(" ")?;
self.writer.write_str(itoa::Buffer::new().format(v))?;
Ok(())
}

pub fn encode_f64(&mut self, v: f64) -> Result<(), std::fmt::Error> {
self.writer.write_str(" ")?;
self.writer.write_str(dtoa::Buffer::new().format(v))?;
Expand Down Expand Up @@ -579,6 +585,9 @@ mod tests {
let mut registry = Registry::default();
registry.register("my_counter", "My counter", counter);

let counter_u32 = Counter::<u32, AtomicU32>::default();
registry.register("u32_counter", "Counter::<u32, AtomicU32>", counter_u32);

let mut encoded = String::new();

encode(&mut encoded, &registry).unwrap();
Expand Down Expand Up @@ -876,7 +885,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 cbc406f

Please sign in to comment.