Skip to content

Commit 3942d06

Browse files
authored
feat(metrics/family): 🍭 len() returns the number of metrics (#246)
feat(metrics/family): `len()` returns the number of metrics this commit introduces a `len()` method to `Family<S, M, C>`, which returns the number of series within a metric family. see also #245, which allows callers to check if a family `contains()` a given label set. ```shell $ cargo clippy --all-features --all-targets --tests Compiling prometheus-client v0.23.0 (/path/to/prometheus-client) warning: struct `Family` has a public `len` method, but no `is_empty` method --> src/metrics/family.rs:309:5 | 309 | pub fn len(&self) -> usize { | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#len_without_is_empty = note: `#[warn(clippy::len_without_is_empty)]` on by default warning: `prometheus-client` (lib) generated 1 warning warning: `prometheus-client` (lib test) generated 1 warning (1 duplicate) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.63s ``` Signed-off-by: katelyn martin <git@katelyn.world>
1 parent bffe835 commit 3942d06

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
feature is enabled. See [PR 242].
1818
- `Family` now exposes a `contains()` method when the `test-util` feature
1919
is enabled. See [PR 245].
20+
- `Family` now exposes `len()` and `is_empty()` methods when the
21+
`test-util` feature is enabled. See [PR 246].
2022

2123
[PR 279]: https://github.com/prometheus/client_rust/pull/279
2224
[PR 281]: https://github.com/prometheus/client_rust/pull/281
2325
[PR 242]: https://github.com/prometheus/client_rust/pull/242
2426
[PR 245]: https://github.com/prometheus/client_rust/pull/245
27+
[PR 246]: https://github.com/prometheus/client_rust/pull/246
2528

2629
## [0.24.0]
2730

src/metrics/family.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,35 @@ impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C
362362
self.metrics.read().get(label_set).is_some()
363363
}
364364

365+
/// Returns the number of metrics in this family.
366+
///
367+
/// ```
368+
/// # use prometheus_client::metrics::counter::{Atomic, Counter};
369+
/// # use prometheus_client::metrics::family::Family;
370+
/// #
371+
/// let family = Family::<Vec<(String, String)>, Counter>::default();
372+
/// assert_eq!(family.len(), 0);
373+
///
374+
/// // Will create the metric with label `method="GET"` on first call and
375+
/// // return a reference.
376+
/// family.get_or_create(&vec![("method".to_owned(), "GET".to_owned())]).inc();
377+
/// assert_eq!(family.len(), 1);
378+
///
379+
/// // Clear the family of all label sets.
380+
/// family.clear();
381+
/// assert_eq!(family.len(), 0);
382+
/// ```
383+
#[cfg(any(test, feature = "test-util"))]
384+
pub fn len(&self) -> usize {
385+
self.metrics.read().len()
386+
}
387+
388+
/// Returns `true` if the family contains no metrics.
389+
#[cfg(any(test, feature = "test-util"))]
390+
pub fn is_empty(&self) -> bool {
391+
self.metrics.read().is_empty()
392+
}
393+
365394
pub(crate) fn read(&self) -> RwLockReadGuard<'_, HashMap<S, M>> {
366395
self.metrics.read()
367396
}

0 commit comments

Comments
 (0)