-
Notifications
You must be signed in to change notification settings - Fork 826
Add remove_matching() method for metric label deletion #1121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Signed-off-by: Hazel <hazel@hazel.localdomain>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! A couple comments/questions to start with
prometheus_client/metrics.py
Outdated
@@ -203,6 +203,41 @@ def remove(self, *labelvalues: Any) -> None: | |||
if labelvalues in self._metrics: | |||
del self._metrics[labelvalues] | |||
|
|||
def remove_matching(self, partial: dict[str, str]) -> int: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would you think of remove_partial_match
for the name of this function? When I first see matching I think that all labelnames/values need to match to be removed not a partial. The name of the variable helps, but could also then be labels
instead of partial
which helps with confusion around thinking it could be a partial function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the suggestion! I’ve renamed the method to remove_by_labels
and the argument to labels.
I also added a short docstring to clarify that this removes series where the provided (key, value) pairs are a partial match of the labelset.
This should reduce confusion with full-match semantics.
prometheus_client/metrics.py
Outdated
|
||
pos_filter = {self._labelnames.index(k): str(v) for k, v in partial.items()} | ||
|
||
deleted = 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reasoning for returning the number of deleted items? We don't do that in remove today, but if there is a good use case I am not opposed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point on consistency. The initial version returned the number of deleted series, but I’ve updated it to return None, matching the behavior of remove(): idempotent and a no-op if nothing matches.
If there’s a strong use case for counts in the future, I’m happy to follow up with a separate helper that reports the number of removed series, without changing the main API.
Signed-off-by: Hazel <hazel@hazel.localdomain>
Signed-off-by: Hazel <hazel@hazel.localdomain>
213940c
to
fc3fc1e
Compare
Description
Fixes #1118
This PR introduces a new
remove_by_labels()
method to allow removing metric samples by matching a set of label values.Changes
remove_by_labels()
toMetricWrapperBase
for selective deletion of metrics by label.remove()
: returns None, and acts as a no-op if no series match.Motivation
Currently, the Python client does not support removing metrics with specific label values without clearing the whole metric. This feature enables more granular control, which can be useful for scenarios such as:
Example Usage
@csmarchbanks Would you take a look when you have a chance?