From 72317c15d681ff4c4c092e4da71b57e03d45dc86 Mon Sep 17 00:00:00 2001 From: jxpp Date: Sat, 29 Feb 2020 16:12:36 +0100 Subject: [PATCH] Raise exception when logging to non-observable Enum (#501) (#520) Signed-off-by: jxpp --- prometheus_client/metrics.py | 1 + tests/test_core.py | 1 + 2 files changed, 2 insertions(+) diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 4094449f..b34ca817 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -656,6 +656,7 @@ def _metric_init(self): def state(self, state): """Set enum metric state.""" + self._raise_if_not_observable() with self._lock: self._value = self._states.index(state) diff --git a/tests/test_core.py b/tests/test_core.py index 2d6f83f4..f0b856a5 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -428,6 +428,7 @@ def test_labels(self): self.assertEqual(0, self.registry.get_sample_value('el', {'l': 'a', 'el': 'a'})) self.assertEqual(0, self.registry.get_sample_value('el', {'l': 'a', 'el': 'b'})) self.assertEqual(1, self.registry.get_sample_value('el', {'l': 'a', 'el': 'c'})) + self.assertRaises(ValueError, self.labels.state, 'a') def test_overlapping_labels(self): with pytest.raises(ValueError):