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

Add support of f32 for Gauge and Counter #216

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.23.0] - unreleased

### Changed

- `ConstCounter::new` now requires specifying the type of literal arguments, like this: `ConstCounter::new(42u64);`.
See [PR 173].

- Update `prost` dependencies to `v0.12`.
See [PR 198].

[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].
### Added

- Support `i32`/`f32` for `Gauge` and `u32`/`f32` for `Counter`/`CounterWithExemplar`.
See [PR 173] and [PR 216].

[PR 173]: https://github.com/prometheus/client_rust/pull/173
[PR 216]: https://github.com/prometheus/client_rust/pull/216
Comment on lines +9 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


## [0.22.3]

Expand Down
6 changes: 6 additions & 0 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ mod tests {
let mut registry = Registry::default();
registry.register("my_counter", "My counter", counter);

let counter_f32 = Counter::<f32, AtomicU32>::default();
registry.register("f32_counter", "Counter::<f32, AtomicU32>", counter_f32);

let mut encoded = String::new();

encode(&mut encoded, &registry).unwrap();
Expand Down Expand Up @@ -780,6 +783,9 @@ mod tests {
let gauge = Gauge::<u32, AtomicU32>::default();
registry.register("u32_gauge", "Gauge::<u32, AtomicU32>", gauge);

let gauge_f32 = Gauge::<f32, AtomicU32>::default();
registry.register("f32_gauge", "Gauge::<f32, AtomicU32>", gauge_f32);

let mut encoded = String::new();

encode(&mut encoded, &registry).unwrap();
Expand Down
25 changes: 25 additions & 0 deletions src/metrics/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,31 @@ impl Atomic<f64> for AtomicU64 {
}
}

impl Atomic<f32> for AtomicU32 {
fn inc(&self) -> f32 {
self.inc_by(1.0)
}

fn inc_by(&self, v: f32) -> f32 {
let mut old_u32 = self.load(Ordering::Relaxed);
let mut old_f32;
loop {
old_f32 = f32::from_bits(old_u32);
let new = f32::to_bits(old_f32 + v);
match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) {
Ok(_) => break,
Err(x) => old_u32 = x,
}
}

old_f32
}

fn get(&self) -> f32 {
f32::from_bits(self.load(Ordering::Relaxed))
}
}

impl<N, A> TypedMetric for Counter<N, A> {
const TYPE: MetricType = MetricType::Counter;
}
Expand Down
48 changes: 48 additions & 0 deletions src/metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,54 @@ impl Atomic<f64> for AtomicU64 {
}
}

impl Atomic<f32> for AtomicU32 {
fn inc(&self) -> f32 {
self.inc_by(1.0)
}

fn inc_by(&self, v: f32) -> f32 {
let mut old_u32 = self.load(Ordering::Relaxed);
let mut old_f32;
loop {
old_f32 = f32::from_bits(old_u32);
let new = f32::to_bits(old_f32 + v);
match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) {
Ok(_) => break,
Err(x) => old_u32 = x,
}
}

old_f32
}

fn dec(&self) -> f32 {
self.dec_by(1.0)
}

fn dec_by(&self, v: f32) -> f32 {
let mut old_u32 = self.load(Ordering::Relaxed);
let mut old_f32;
loop {
old_f32 = f32::from_bits(old_u32);
let new = f32::to_bits(old_f32 - v);
match self.compare_exchange_weak(old_u32, new, Ordering::Relaxed, Ordering::Relaxed) {
Ok(_) => break,
Err(x) => old_u32 = x,
}
}

old_f32
}

fn set(&self, v: f32) -> f32 {
f32::from_bits(self.swap(f32::to_bits(v), Ordering::Relaxed))
}

fn get(&self) -> f32 {
f32::from_bits(self.load(Ordering::Relaxed))
}
}

impl<N, A> TypedMetric for Gauge<N, A> {
const TYPE: MetricType = MetricType::Gauge;
}
Expand Down
Loading