Skip to content

Accept label keyword arguments in remove() - #1227

Open
Thanatos9404 wants to merge 1 commit into
prometheus:masterfrom
Thanatos9404:remove-label-kwargs
Open

Thanatos9404 wants to merge 1 commit into
prometheus:masterfrom
Thanatos9404:remove-label-kwargs

Conversation

@Thanatos9404

Copy link
Copy Markdown

Fixes #820

@csmarchbanks, as discussed in the issue: labels() takes the label values positionally or as keyword arguments, but remove() only took them positionally, so a child created with c.labels(method='get', endpoint='/') had to be removed with c.remove('get', '/'), in labelnames order.

remove() now takes keyword arguments with the same rules as labels():

c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])
c.labels(method='post', endpoint='/submit').inc()
c.remove(method='post', endpoint='/submit')
  • every label name must be given, and the values are matched by name, not position;
  • positional and keyword values can't be mixed (ValueError, same message as labels());
  • unknown or missing names raise ValueError.

This is different from remove_by_labels(), which removes every labelset that partially matches a dict (so leaving a label out widens the match) and scans all children. remove() with keywords still deletes exactly one labelset and checks that every label name is given, like labels().

I also moved the remove() docstring to the top of the function: it sat after the first statements, so it wasn't the function's docstring. The labels docs now show the keyword form.

Tests in tests/test_core.py: removal by keyword, matching by name when the keyword order differs from labelnames, the ValueError cases (mixed, missing, unknown and extra names), str() coercion (remove(l=None)), and the keyword form in test_recreate_child_after_removal. The 5 new or extended tests fail on master with TypeError: ... got an unexpected keyword argument.

Run locally: the full pytest suite on Linux with Python 3.12 (431 passed, 14 skipped), tox -e mypy's command (mypy 0.991 on Python 3.9: no issues), and the flake8 and isort --check commands from tox.ini.

labels() takes the label values positionally or as keyword arguments,
but remove() only took them positionally, so a child created with
c.labels(method='get', endpoint='/') had to be removed with the values
in labelnames order.

Let remove() take keyword arguments with the same rules as labels():
one for every label name, matched by name, and not mixed with
positional values. Move its docstring to the top of the function, where
it was not picked up as a docstring.

Fixes prometheus#820

Signed-off-by: Yashvardhan Thanvi <yashvt9404@gmail.com>

This branch has not been deployed

No deployments
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.

Add support for passing in label kwargs to remove() function

1 participant