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

Added Counter<u32, AtomicU32> implementation #206

Closed
wants to merge 1 commit into from
Closed
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
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
Loading