diff --git a/metrics/CHANGELOG.md b/metrics/CHANGELOG.md index 64089104..55a20f35 100644 --- a/metrics/CHANGELOG.md +++ b/metrics/CHANGELOG.md @@ -11,7 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Added `Debug` derive to numerous types. ([#504](https://github.com/metrics-rs/metrics/pull/504)) -- Blanket implementations of `Recorder` over smart pointer representations (i.e. `Arc where T: Recorder`). ([#512](https://github.com/metrics-rs/metrics/pull/512)) +- Blanket implementations of `Recorder` over smart pointer representations (i.e. `Arc where T: Recorder`). + ([#512](https://github.com/metrics-rs/metrics/pull/512)) +- Added a new method, `record_many`, to `Histogram` and `HistogramFn`, for recording a single value multiple times. This + method is backwards compatible as `HistogramFn` provides a default implementation. ([#531](https://github.com/metrics-rs/metrics/pull/531)) ### Changed diff --git a/metrics/src/handles.rs b/metrics/src/handles.rs index ccc7e6ac..33123aa7 100644 --- a/metrics/src/handles.rs +++ b/metrics/src/handles.rs @@ -36,6 +36,13 @@ pub trait GaugeFn { pub trait HistogramFn { /// Records a value into the histogram. fn record(&self, value: f64); + + /// Records a value into the histogram multiple times. + fn record_many(&self, value: f64, count: usize) { + for _ in 0..count { + self.record(value); + } + } } /// A counter. @@ -156,12 +163,19 @@ impl Histogram { Self { inner: Some(a) } } - /// Records a value in the histogram. + /// Records a value into the histogram. pub fn record(&self, value: T) { if let Some(ref inner) = self.inner { inner.record(value.into_f64()) } } + + /// Records a value into the histogram multiple times. + pub fn record_many(&self, value: T, count: usize) { + if let Some(ref inner) = self.inner { + inner.record_many(value.into_f64(), count) + } + } } impl CounterFn for Arc