Skip to content

fix: raise ValueError when clear() called on metric without labels#1174

Open
kimjune01 wants to merge 2 commits intoprometheus:masterfrom
kimjune01:fix/1140-clear-no-labels
Open

fix: raise ValueError when clear() called on metric without labels#1174
kimjune01 wants to merge 2 commits intoprometheus:masterfrom
kimjune01:fix/1140-clear-no-labels

Conversation

@kimjune01
Copy link
Copy Markdown

Summary

  • Fixes _lock not found on metric #1140 (also addresses duplicate Metrics without labels missing breaks some codepath #949)
  • clear() on a metric without labels raised AttributeError: 'Counter' object has no attribute '_lock' because _lock and _metrics are only initialized on parent metrics (those with labelnames but no labelvalues)
  • Added the same ValueError guard that remove() already uses: if not self._labelnames: raise ValueError(...)
  • Added tests for clear() and remove() on label-less metrics

Test plan

  • test_clear_no_labels_raises -- verifies clear() on a label-less metric raises ValueError
  • test_remove_no_labels_raises -- verifies remove() on a label-less metric raises ValueError
  • Existing test_clear still passes (labeled metrics work as before)
  • Full test_core.py suite passes (113 tests)

kimjune01 added 2 commits May 9, 2026 10:06
…rometheus#1140)

Metrics without labels never initialize _lock or _metrics (only parent
metrics with labelnames do). Calling clear() on such a metric raised an
unhelpful AttributeError. Guard with the same ValueError pattern that
remove() already uses, matching maintainer guidance that clear() is not
intended for label-less metrics.

Fixes prometheus#1140. Also addresses duplicate prometheus#949.
Codex flagged that parallel tests for remove() on label-less metrics
strengthen the fix. Also removed unnecessary test docstring.
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.

_lock not found on metric

1 participant