fix: raise ValueError when clear() called on metric without labels#1174
Open
kimjune01 wants to merge 2 commits intoprometheus:masterfrom
Open
fix: raise ValueError when clear() called on metric without labels#1174kimjune01 wants to merge 2 commits intoprometheus:masterfrom
kimjune01 wants to merge 2 commits intoprometheus:masterfrom
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
clear()on a metric without labels raisedAttributeError: 'Counter' object has no attribute '_lock'because_lockand_metricsare only initialized on parent metrics (those withlabelnamesbut nolabelvalues)ValueErrorguard thatremove()already uses:if not self._labelnames: raise ValueError(...)clear()andremove()on label-less metricsTest plan
test_clear_no_labels_raises-- verifiesclear()on a label-less metric raisesValueErrortest_remove_no_labels_raises-- verifiesremove()on a label-less metric raisesValueErrortest_clearstill passes (labeled metrics work as before)test_core.pysuite passes (113 tests)