Skip to content

Commit

Permalink
feat(metrics): add support of f32 for Gauge and Counter (#216)
Browse files Browse the repository at this point in the history
Signed-off-by: Léo Gillot-Lamure <[email protected]>
Signed-off-by: Max Inden <[email protected]>
Co-authored-by: Max Inden <[email protected]>
  • Loading branch information
navaati and mxinden authored Jul 17, 2024
1 parent 4216fa6 commit aeca8d8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
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

## [0.22.3]

Expand Down
6 changes: 5 additions & 1 deletion src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,9 +717,10 @@ 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 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 @@ -783,6 +784,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 gauge_i32 = Gauge::<i32, AtomicI32>::default();
registry.register("i32_gauge", "Gauge::<i32, AtomicU32>", gauge_i32);

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

0 comments on commit aeca8d8

Please sign in to comment.