Skip to content

Commit

Permalink
Use specific exception for duplicate timeseries
Browse files Browse the repository at this point in the history
Use sub-class of ValueError instead of ValueError, so that we can
distinguish issues caused by wrong input (like invalid name format)
from duplicate metrics being registered into the same registry.
  • Loading branch information
kajinamit committed Nov 22, 2024
1 parent c89624f commit 6e89bc8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions prometheus_client/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def collect(self) -> Iterable[Metric]:
return []


class DuplicateTimeseries(ValueError):
def __init__(self, duplicates):
msg = 'Duplicated timeseries in CollectorRegistry: {}'.format(
duplicates)
super().__init__(msg)
self.duplicates = duplicates


class CollectorRegistry(Collector):
"""Metric collector registry.
Expand All @@ -40,9 +48,7 @@ def register(self, collector: Collector) -> None:
names = self._get_names(collector)
duplicates = set(self._names_to_collectors).intersection(names)
if duplicates:
raise ValueError(
'Duplicated timeseries in CollectorRegistry: {}'.format(
duplicates))
raise DuplicateTimeseries(duplicates)
for name in names:
self._names_to_collectors[name] = collector
self._collector_to_names[collector] = names
Expand Down

0 comments on commit 6e89bc8

Please sign in to comment.