FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Validate Enum arguments before registering the collector by sean-kim05 · Pull Request #1189 · prometheus/client_python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
8 changes: 4 additions & 4 deletions prometheus_client/metrics.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ def __init__(self,
_labelvalues: Optional[Sequence[str]] = None,
states: Optional[Sequence[str]] = None,
):
if name in labelnames:
raise ValueError(f'Overlapping labels for Enum metric: {name}')
if not states:
raise ValueError(f'No states provided for Enum metric: {name}')
super().__init__(
name=name,
documentation=documentation,
Expand All @@ -779,10 +783,6 @@ def __init__(self,
registry=registry,
_labelvalues=_labelvalues,
)
if name in labelnames:
raise ValueError(f'Overlapping labels for Enum metric: {name}')
if not states:
raise ValueError(f'No states provided for Enum metric: {name}')
self._kwargs['states'] = self._states = states

def _metric_init(self) -> None:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_core.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,19 @@ def test_overlapping_labels(self):
with pytest.raises(ValueError):
Enum('e', 'help', registry=None, labelnames=['e'])

def test_failed_init_does_not_pollute_registry(self):
registry = CollectorRegistry()
# A validation failure in __init__ must not leave a half-built collector
# registered: otherwise the name stays permanently taken and any later
# scrape of the registry crashes on the missing _states attribute.
with pytest.raises(ValueError):
Enum('task_state', 'help', states=None, registry=registry)
with pytest.raises(ValueError):
Enum('task_state', 'help', states=['a'], labelnames=['task_state'], registry=registry)
# The name is still free, so a correct definition registers and scrapes.
Enum('task_state', 'help', states=['a', 'b'], registry=registry)
self.assertEqual(1, registry.get_sample_value('task_state', {'task_state': 'a'}))


class TestMetricWrapper(unittest.TestCase):
def setUp(self):
Expand Down

Back | FazBrowse Home | New Git URL