Skip to content

Make clear() a no-op for label-less metrics (fixes #707)#1184

Merged
csmarchbanks merged 1 commit into
prometheus:masterfrom
ompathakbaap:fix-707-clear-labelless-counter
Jun 24, 2026
Merged

Make clear() a no-op for label-less metrics (fixes #707)#1184
csmarchbanks merged 1 commit into
prometheus:masterfrom
ompathakbaap:fix-707-clear-labelless-counter

Conversation

@ompathakbaap

@ompathakbaap ompathakbaap commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Fixes #707.

clear() unconditionally did with self._lock:, but self._lock
is only created in __init__ for metrics with labelnames. Calling
.clear() on a label-less metric (e.g. Counter('c', 'c')) raised
AttributeError: 'Counter' object has no attribute '_lock'.

Per the discussion in the issue, a label-less metric has no
labelsets to clear, so this makes clear() a no-op in that case
rather than raising consistent with the direction @csmarchbanks
and others converged on in the thread.

Added a regression test in tests/test_core.py::TestCounter.

@csmarchbanks would appreciate your review on this when you have a chance.

…uteError

Fixes prometheus#707. clear() unconditionally acquired self._lock, which is
never created for metrics with no labelnames, causing an
AttributeError. Per discussion in the issue (csmarchbanks,
roidelapluie, sdfordham, tomprince), a label-less metric has no
labelsets to clear, so clear() now treats this as a no-op rather
than raising.

Signed-off-by: Om Pathak <ompathak@gmail.com>

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The change in this PR does not match the description, pushed the wrong change perhaps?

@ompathakbaap ompathakbaap force-pushed the fix-707-clear-labelless-counter branch from d60a9ce to 0c4c677 Compare June 23, 2026 23:26
@ompathakbaap

Copy link
Copy Markdown
Contributor Author

I am so sorry about that, a branch mix up on my end (the WSL docs note and this fix ended up on the same branch pointer). Force pushed the corrected commit just now, the diff now shows the clear() change in metrics.py and regression test in test_core.py

@csmarchbanks csmarchbanks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@csmarchbanks csmarchbanks merged commit 7819d80 into prometheus:master Jun 24, 2026
12 checks passed
@ompathakbaap ompathakbaap deleted the fix-707-clear-labelless-counter branch June 24, 2026 02:49
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.

Calling clear() on Counter without labels fails

2 participants