-
Notifications
You must be signed in to change notification settings - Fork 829
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ: | ||
warnings.warn( | ||
"Removal of labels has not been implemented in multi-process mode yet.", | ||
UserWarning | ||
) | ||
|
||
if not self._labelnames: | ||
raise ValueError('No label names were set when constructing %s' % self) | ||
|
||
if not isinstance(partial, dict): | ||
raise TypeError("partial must be a dict of {label_name: label_value}") | ||
|
||
if not partial: | ||
return 0 | ||
|
||
invalid = [k for k in partial.keys() if k not in self._labelnames] | ||
if invalid: | ||
raise ValueError( | ||
'Unknown label names: %s; expected %s' % (invalid, self._labelnames) | ||
) | ||
|
||
pos_filter = {self._labelnames.index(k): str(v) for k, v in partial.items()} | ||
|
||
deleted = 0 | ||
|
||
with self._lock: | ||
for lv in list(self._metrics.keys()): | ||
if all(lv[pos] == want for pos, want in pos_filter.items()): | ||
if lv in self._metrics: | ||
del self._metrics[lv] | ||
deleted += 1 | ||
|
||
return deleted | ||
|
||
|
||
def clear(self) -> None: | ||
"""Remove all labelsets from the metric""" | ||
if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ: | ||
|
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 belabels
instead ofpartial
which helps with confusion around thinking it could be a partial function.Uh oh!
There was an error while loading. Please reload this page.
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.