Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions prometheus_client/metrics.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import os
from threading import Lock
import time
Expand Down Expand Up @@ -780,6 +781,10 @@ def __init__(self,
raise ValueError(f'Overlapping labels for Enum metric: {name}')
if not states:
raise ValueError(f'No states provided for Enum metric: {name}')
# A repeated state is exposed as two samples with the same labels.
duplicates = sorted(s for s, count in collections.Counter(states).items() if count > 1)
if duplicates:
raise ValueError(f'Duplicate states for Enum metric {name}: {duplicates!r}')
super().__init__(
name=name,
documentation=documentation,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,12 @@ def test_overlapping_labels(self):
with pytest.raises(ValueError):
Enum('e', 'help', registry=None, labelnames=['e'])

def test_duplicate_states(self):
registry = CollectorRegistry()
with self.assertRaisesRegex(ValueError, r"^Duplicate states for Enum metric e: \['a'\]$"):
Enum('e', 'help', states=['a', 'b', 'a'], registry=registry)
self.assertEqual(list(registry.collect()), [])

def test_failed_init_does_not_pollute_registry(self):
registry = CollectorRegistry()
# A validation failure in __init__ must not leave a half-built collector
Expand Down
Loading