From 770b6a94a7ccb0b2d80b9cd1b47dfc30e35720f3 Mon Sep 17 00:00:00 2001 From: Pengfei Zhang Date: Sat, 2 Mar 2024 23:17:45 -0500 Subject: [PATCH] Add sanity check for label value Signed-off-by: Pengfei Zhang --- .gitignore | 3 ++- prometheus_client/metrics.py | 2 ++ tests/test_core.py | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 0ba244f6..69cf1d3c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ dist .coverage .tox .*cache -htmlcov \ No newline at end of file +htmlcov +.vs/ \ No newline at end of file diff --git a/prometheus_client/metrics.py b/prometheus_client/metrics.py index 91cd9ecf..dfa38b78 100644 --- a/prometheus_client/metrics.py +++ b/prometheus_client/metrics.py @@ -706,6 +706,8 @@ def info(self, val: Dict[str, str]) -> None: raise ValueError('Overlapping labels for Info metric, metric: {} child: {}'.format( self._labelnames, val)) with self._lock: + if any( i is None for i in val.values() ): + raise ValueError('Label value cannot be None') self._value = dict(val) def _child_samples(self) -> Iterable[Sample]: diff --git a/tests/test_core.py b/tests/test_core.py index 30f9e0ad..8a54a02d 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -534,6 +534,7 @@ def test_info(self): def test_labels(self): self.assertRaises(ValueError, self.labels.labels('a').info, {'l': ''}) + self.assertRaises(ValueError, self.labels.labels('a').info, {'il': None}) self.labels.labels('a').info({'foo': 'bar'}) self.assertEqual(1, self.registry.get_sample_value('il_info', {'l': 'a', 'foo': 'bar'}))