diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 4c53b26b..73469eaf 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -254,6 +254,8 @@ def remove_by_labels(self, labels: dict[str, str]) -> None: def clear(self) -> None: """Remove all labelsets from the metric""" + if not self._labelnames: + raise ValueError('No label names were set when constructing %s' % self) if 'prometheus_multiproc_dir' in os.environ or 'PROMETHEUS_MULTIPROC_DIR' in os.environ: warnings.warn( "Clearing labels has not been implemented in multi-process mode yet", diff --git a/tests/test_core.py b/tests/test_core.py index cee4bfb0..049ab3c0 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -620,6 +620,16 @@ def test_clear(self): self.assertEqual(None, self.registry.get_sample_value('c_total', {'l': 'x'})) self.assertEqual(None, self.registry.get_sample_value('c_total', {'l': 'y'})) + def test_clear_no_labels_raises(self): + no_labels = Counter('c_no_labels', 'help', registry=self.registry) + no_labels.inc() + self.assertRaises(ValueError, no_labels.clear) + + def test_remove_no_labels_raises(self): + no_labels = Counter('c_no_labels2', 'help', registry=self.registry) + no_labels.inc() + self.assertRaises(ValueError, no_labels.remove, 'x') + def test_incorrect_label_count_raises(self): self.assertRaises(ValueError, self.counter.labels) self.assertRaises(ValueError, self.counter.labels, 'a', 'b')