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

src/metrics/gauge: Implement Gauge::dec and Gauge::dec_by #30

Merged
merged 1 commit into from
Dec 29, 2021
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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.13.1] - unreleased
## [0.14.0] - unreleased

### Changed

- Update to `itoa` `v1`. See [PR 28].
- Update to `dtoa` `v1`. See [PR 27].

### Added

- Implement `Gauge::dec` and `Gauge::dec_by`. See [PR 30].

[PR 28]: https://github.com/mxinden/rust-open-metrics-client/pull/28
[PR 27]: https://github.com/mxinden/rust-open-metrics-client/pull/27
[PR 30]: https://github.com/mxinden/rust-open-metrics-client/pull/30

## [0.13.0] - 2021-11-21

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "open-metrics-client"
version = "0.13.1"
version = "0.14.0"
authors = ["Max Inden <[email protected]>"]
edition = "2018"
description = "Open Metrics client library allowing users to natively instrument applications."
Expand Down
57 changes: 55 additions & 2 deletions src/metrics/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ impl<N, A: Atomic<N>> Gauge<N, A> {
self.value.inc_by(v)
}

/// Decrease the [`Gauge`] by 1, returning the previous value.
pub fn dec(&self) -> N {
self.value.dec()
}

/// Decrease the [`Gauge`] by `v`, returning the previous value.
pub fn dec_by(&self, v: N) -> N {
self.value.dec_by(v)
}

/// Sets the [`Gauge`] to `v`, returning the previous value.
pub fn set(&self, v: N) -> N {
self.value.set(v)
Expand All @@ -94,6 +104,10 @@ pub trait Atomic<N> {

fn inc_by(&self, v: N) -> N;

fn dec(&self) -> N;

fn dec_by(&self, v: N) -> N;

fn set(&self, v: N) -> N;

fn get(&self) -> N;
Expand All @@ -108,6 +122,14 @@ impl Atomic<u64> for AtomicU64 {
self.fetch_add(v, Ordering::Relaxed)
}

fn dec(&self) -> u64 {
self.dec_by(1)
}

fn dec_by(&self, v: u64) -> u64 {
self.fetch_sub(v, Ordering::Relaxed)
}

fn set(&self, v: u64) -> u64 {
self.swap(v, Ordering::Relaxed)
}
Expand All @@ -126,6 +148,14 @@ impl Atomic<u32> for AtomicU32 {
self.fetch_add(v, Ordering::Relaxed)
}

fn dec(&self) -> u32 {
self.dec_by(1)
}

fn dec_by(&self, v: u32) -> u32 {
self.fetch_sub(v, Ordering::Relaxed)
}

fn set(&self, v: u32) -> u32 {
self.swap(v, Ordering::Relaxed)
}
Expand Down Expand Up @@ -155,6 +185,25 @@ impl Atomic<f64> for AtomicU64 {
old_f64
}

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

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

old_f64
}

fn set(&self, v: f64) -> f64 {
f64::from_bits(self.swap(f64::to_bits(v), Ordering::Relaxed))
}
Expand All @@ -173,11 +222,15 @@ mod tests {
use super::*;

#[test]
fn inc_and_get() {
fn inc_dec_and_get() {
let gauge: Gauge = Gauge::default();
assert_eq!(0, gauge.inc());
assert_eq!(1, gauge.get());
assert_eq!(1, gauge.set(10));

assert_eq!(1, gauge.dec());
assert_eq!(0, gauge.get());

assert_eq!(0, gauge.set(10));
assert_eq!(10, gauge.get());
}
}