Skip to content

Commit a1089eb

Browse files
committed
Use specific exception for duplicate timeseries
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. Signed-off-by: Takashi Kajinami <kajinamit@oss.nttdata.com>
1 parent 482656c commit a1089eb

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

prometheus_client/registry.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import copy
22
from threading import Lock
3-
from typing import Dict, Iterable, List, Optional, Protocol
3+
from typing import Dict, Iterable, List, Optional, Protocol, Set
44

55
from .metrics_core import Metric
66

@@ -15,6 +15,14 @@ def collect(self) -> Iterable[Metric]:
1515
return []
1616

1717

18+
class DuplicateTimeseries(ValueError):
19+
def __init__(self, duplicates: Set[str]):
20+
msg = 'Duplicated timeseries in CollectorRegistry: {}'.format(
21+
duplicates)
22+
super().__init__(msg)
23+
self.duplicates: Set[str] = duplicates
24+
25+
1826
class CollectorRegistry:
1927
"""Metric collector registry.
2028
@@ -40,9 +48,7 @@ def register(self, collector: Collector) -> None:
4048
names = self._get_names(collector)
4149
duplicates = set(self._names_to_collectors).intersection(names)
4250
if duplicates:
43-
raise ValueError(
44-
'Duplicated timeseries in CollectorRegistry: {}'.format(
45-
duplicates))
51+
raise DuplicateTimeseries(duplicates)
4652
for name in names:
4753
self._names_to_collectors[name] = collector
4854
self._collector_to_names[collector] = names

0 commit comments

Comments
 (0)