diff --git a/CHANGELOG.md b/CHANGELOG.md index 77b475ff..27e59f06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Cargo.toml b/Cargo.toml index 807712f5..c3ea0be2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "open-metrics-client" -version = "0.13.1" +version = "0.14.0" authors = ["Max Inden "] edition = "2018" description = "Open Metrics client library allowing users to natively instrument applications." diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index 8c64bab6..bcbc11f7 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -70,6 +70,16 @@ impl> Gauge { 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) @@ -94,6 +104,10 @@ pub trait Atomic { 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; @@ -108,6 +122,14 @@ impl Atomic 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) } @@ -126,6 +148,14 @@ impl Atomic 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) } @@ -155,6 +185,25 @@ impl Atomic 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)) } @@ -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()); } }