Skip to content

Commit 77a034b

Browse files
authored
feat(metrics/histogram): 🍪 count() and sum() accessors (#242)
* feat: introduce `test-util` feature flag for background, see discussion in #242: > > Would you be amenable to feature-gating these public helpers behind > > a `test-util` feature? This is a pretty common approach we've seen > > in other crates. > > That sounds reasonable to me. Contribution welcome. Thanks. Please > point out very prominently that we might break any of those APIs in > a patch release. \- <#242 (comment)> this commit introduces a `test-util` feature flag. this feature flag will function as a gate for additional interfaces, such as accessors to read the value of a histogram, that facilitate integration tests inspecting metrics. comments note that any forthcoming interfaces included by this feature are not subject to stability guarantees. Signed-off-by: katelyn martin <[email protected]> * feat(metrics/histogram): `count()` and `sum()` accessors fixes #241. this commit introduces two new public methods to `Histogram`; `sum()` and `count()` return the sum of all observations and the number of observations made, respectively. Signed-off-by: katelyn martin <[email protected]> * chore: changelog entry Signed-off-by: katelyn martin <[email protected]> --------- Signed-off-by: katelyn martin <[email protected]>
1 parent e45cecf commit 77a034b

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
`EncodeGaugeValue` would not error when encoding some `u64`s that don't fit
1414
in a `i64`. See [PR 281].
1515
- Filter out empty metric families, to match the go client. See [PR 279].
16+
- `Histogram` now exposes `count()` and `sum()` methods when the `test-util`
17+
feature is enabled. See [PR 242].
1618

1719
[PR 279]: https://github.com/prometheus/client_rust/pull/279
1820
[PR 281]: https://github.com/prometheus/client_rust/pull/281
21+
[PR 242]: https://github.com/prometheus/client_rust/pull/242
1922

2023
## [0.24.0]
2124

Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ documentation = "https://docs.rs/prometheus-client"
1414
default = []
1515
protobuf = ["dep:prost", "dep:prost-types", "dep:prost-build"]
1616

17+
# This feature provides additional APIs for testing downstream code using
18+
# `prometheus-client`.
19+
#
20+
# Note: Interfaces gated by this feature flag are not subject to stability
21+
# guarantees and may be changed or removed in patch releases.
22+
test-util = []
23+
1724
[workspace]
1825
members = ["derive-encode"]
1926

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
//! ```
7878
//! See [examples] directory for more.
7979
//!
80+
//! # Features
81+
//!
82+
//! The `test-util` gates additional APIs, such as accessors, to facilitate integration and unit
83+
//! testing of metrics. Note that APIs gated by this feature flag are not subject to stability
84+
//! guarantees and may be changed or removed in patch releases.
85+
//!
8086
//! [examples]: https://github.com/prometheus/client_rust/tree/master/examples
8187
8288
pub mod collector;

src/metrics/histogram.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,18 @@ impl Histogram {
8181
self.observe_and_bucket(v);
8282
}
8383

84+
/// Returns the current sum of all observations.
85+
#[cfg(any(test, feature = "test-util"))]
86+
pub fn sum(&self) -> f64 {
87+
self.inner.read().sum
88+
}
89+
90+
/// Returns the current number of observations.
91+
#[cfg(any(test, feature = "test-util"))]
92+
pub fn count(&self) -> u64 {
93+
self.inner.read().count
94+
}
95+
8496
/// Observes the given value, returning the index of the first bucket the
8597
/// value is added to.
8698
///
@@ -212,4 +224,53 @@ mod tests {
212224
let res = exponential_buckets_range(0.0, 32.0, 6).collect::<Vec<_>>();
213225
assert!(res.is_empty());
214226
}
227+
228+
/// Checks that [`Histogram::count()`] works properly.
229+
#[test]
230+
fn count() {
231+
let histogram = Histogram::new([1.0_f64, 2.0, 3.0, 4.0, 5.0]);
232+
assert_eq!(
233+
histogram.count(),
234+
0,
235+
"histogram has zero observations when instantiated"
236+
);
237+
238+
histogram.observe(1.0);
239+
assert_eq!(histogram.count(), 1, "histogram has one observation");
240+
241+
histogram.observe(2.5);
242+
assert_eq!(histogram.count(), 2, "histogram has two observations");
243+
244+
histogram.observe(6.0);
245+
assert_eq!(histogram.count(), 3, "histogram has three observations");
246+
}
247+
248+
/// Checks that [`Histogram::sum()`] works properly.
249+
#[test]
250+
fn sum() {
251+
const BUCKETS: [f64; 3] = [10.0, 100.0, 1000.0];
252+
let histogram = Histogram::new(BUCKETS);
253+
assert_eq!(
254+
histogram.sum(),
255+
0.0,
256+
"histogram sum is zero when instantiated"
257+
);
258+
259+
histogram.observe(3.0); // 3 + 4 + 15 + 101 = 123
260+
histogram.observe(4.0);
261+
histogram.observe(15.0);
262+
histogram.observe(101.0);
263+
assert_eq!(
264+
histogram.sum(),
265+
123.0,
266+
"histogram sum records accurate sum of observations"
267+
);
268+
269+
histogram.observe(1111.0);
270+
assert_eq!(
271+
histogram.sum(),
272+
1234.0,
273+
"histogram sum records accurate sum of observations"
274+
);
275+
}
215276
}

0 commit comments

Comments
 (0)