Skip to content

Conversation

@cratelyn
Copy link
Contributor

this commit introduces a small accessor to the Family<S, M, C> metric family type. this new contains() method allows callers to check whether or not a metric with the provided set of labels exists.

if no metric has been created via e.g. get_or_create(), this method will return false.

cratelyn added a commit to cratelyn/prometheus-client that referenced this pull request Nov 18, 2024
this commit introduces a `len()` method to `Family<S, M, C>`, which
returns the number of series within a metric family.

see also prometheus#245, which allows callers to check if a family `contains()` a
given label set.

Signed-off-by: katelyn martin <me+cratelyn@katelyn.world>
@cratelyn
Copy link
Contributor Author

cratelyn commented Sep 2, 2025

my understanding is that this could be added behind the test-util flag proposed in #283.

Copy link
Member

@mxinden mxinden left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move forward here once #242 merged.

cratelyn added a commit to cratelyn/prometheus-client that referenced this pull request Jan 22, 2026
this commit introduces a `len()` method to `Family<S, M, C>`, which
returns the number of series within a metric family.

see also prometheus#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>
/// assert!(family.contains(&get), "a `method=\"GET\"`-labeled metric exists");
/// assert!(!family.contains(&post), "a `method=\"POST\"`-labeled metric does NOT exist");
/// ```
#[cfg(any(test, feature = "test-util"))]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've rebased this branch, and added the conditional test-util feature flag introduced in #242!

this commit introduces a small accessor to the `Family<S, M, C>` metric
family type. this new `contains()` method allows callers to check
whether or not a metric with the provided set of labels exists.

if no metric has been created via e.g. `get_or_create()`, this method
will return `false`.

Signed-off-by: katelyn martin <git@katelyn.world>
cratelyn added a commit to cratelyn/prometheus-client that referenced this pull request Jan 22, 2026
this commit introduces a `len()` method to `Family<S, M, C>`, which
returns the number of series within a metric family.

see also prometheus#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>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i've also added changelog notes summarizing this change.

Signed-off-by: Max Inden <mail@max-inden.de>
@mxinden mxinden added this pull request to the merge queue Jan 23, 2026
Merged via the queue into prometheus:master with commit bffe835 Jan 23, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants