Skip to content

Commit 26c0fca

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 <[email protected]>
1 parent f48aea4 commit 26c0fca

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

prometheus_client/registry.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from abc import ABC, abstractmethod
22
import copy
33
from threading import Lock
4-
from typing import Dict, Iterable, List, Optional
4+
from typing import Dict, Iterable, List, Optional, Set
55

66
from .metrics_core import Metric
77

@@ -18,6 +18,14 @@ def collect(self) -> Iterable[Metric]:
1818
return []
1919

2020

21+
class DuplicateTimeseries(ValueError):
22+
def __init__(self, duplicates: Set[str]):
23+
msg = 'Duplicated timeseries in CollectorRegistry: {}'.format(
24+
duplicates)
25+
super().__init__(msg)
26+
self.duplicates: Set[str] = duplicates
27+
28+
2129
class CollectorRegistry(Collector):
2230
"""Metric collector registry.
2331
@@ -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)