Skip to content

Commit

Permalink
enhancement: add new record_many method for histograms (#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz authored Oct 12, 2024
1 parent 06eab55 commit 81e6ee5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> where T: Recorder`). ([#512](https://github.com/metrics-rs/metrics/pull/512))
- Blanket implementations of `Recorder` over smart pointer representations (i.e. `Arc<T> 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

Expand Down
16 changes: 15 additions & 1 deletion metrics/src/handles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<T: IntoF64>(&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<T: IntoF64>(&self, value: T, count: usize) {
if let Some(ref inner) = self.inner {
inner.record_many(value.into_f64(), count)
}
}
}

impl<T> CounterFn for Arc<T>
Expand Down

0 comments on commit 81e6ee5

Please sign in to comment.