Skip to content

Commit d284cbe

Browse files
committed
Make the new support for collectors without names be explicit.
This adds a parameters to the constructor of CollectorRegistry to allow that new behavior rather than make it be the default. Signed-off-by: Mathias Kende <[email protected]>
1 parent 792e478 commit d284cbe

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

docs/content/multiprocess/_index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This comes with a number of limitations:
1313
- Registries can not be used as normal
1414
- Registering metrics to a registry later used by a `MultiProcessCollector`
1515
may cause duplicate metrics to be exported
16-
- Filtering on metrics works but is inefficient
16+
- Filtering on metrics works but might be inefficient
1717
- Custom collectors do not work (e.g. cpu and memory metrics)
1818
- Gauges cannot use `set_function`
1919
- Info and Enum metrics do not work
@@ -50,7 +50,7 @@ MY_COUNTER = Counter('my_counter', 'Description of my counter')
5050

5151
# Expose metrics.
5252
def app(environ, start_response):
53-
registry = CollectorRegistry()
53+
registry = CollectorRegistry(support_collectors_without_names=True)
5454
multiprocess.MultiProcessCollector(registry)
5555
data = generate_latest(registry)
5656
status = '200 OK'

prometheus_client/registry.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ class CollectorRegistry(Collector):
2626
exposition formats.
2727
"""
2828

29-
def __init__(self, auto_describe: bool = False, target_info: Optional[Dict[str, str]] = None):
29+
def __init__(self, auto_describe: bool = False, target_info: Optional[Dict[str, str]] = None,
30+
support_collectors_without_names: bool = False):
3031
self._collector_to_names: Dict[Collector, List[str]] = {}
3132
self._names_to_collectors: Dict[str, Collector] = {}
3233
self._auto_describe = auto_describe
3334
self._lock = Lock()
3435
self._target_info: Optional[Dict[str, str]] = {}
36+
self._support_collectors_without_names = support_collectors_without_names
37+
self._collectors_without_names: List[Collector] = []
3538
self.set_target_info(target_info)
36-
self._collectors_with_no_names: List[Collector] = []
3739

3840
def register(self, collector: Collector) -> None:
3941
"""Add a collector to the registry."""
@@ -47,8 +49,8 @@ def register(self, collector: Collector) -> None:
4749
for name in names:
4850
self._names_to_collectors[name] = collector
4951
self._collector_to_names[collector] = names
50-
if not names:
51-
self._collectors_with_no_names.append(collector)
52+
if self._support_collectors_without_names and not names:
53+
self._collectors_without_names.append(collector)
5254

5355
def unregister(self, collector: Collector) -> None:
5456
"""Remove a collector from the registry."""
@@ -151,7 +153,7 @@ def __init__(self, names: Iterable[str], registry: CollectorRegistry):
151153
self._registry = registry
152154

153155
def collect(self) -> Iterable[Metric]:
154-
collectors = set(self._registry._collectors_with_no_names)
156+
collectors = set(self._registry._collectors_without_names)
155157
target_info_metric = None
156158
with self._registry._lock:
157159
if 'target_info' in self._name_set and self._registry._target_info:

tests/test_core.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,13 +1024,22 @@ def test_restricted_registry_does_not_call_extra(self):
10241024
self.assertEqual([m], list(registry.restricted_registry(['s_sum']).collect()))
10251025
mock_collector.collect.assert_not_called()
10261026

1027-
def test_restricted_registry_collects_no_names_collectors(self):
1027+
def test_restricted_registry_ignore_no_names_collectors(self):
10281028
from unittest.mock import MagicMock
10291029
registry = CollectorRegistry()
10301030
mock_collector = MagicMock()
10311031
mock_collector.describe.return_value = []
10321032
registry.register(mock_collector)
10331033
self.assertEqual(list(registry.restricted_registry(['metric']).collect()), [])
1034+
mock_collector.collect.assert_not_called()
1035+
1036+
def test_restricted_registry_collects_no_names_collectors(self):
1037+
from unittest.mock import MagicMock
1038+
registry = CollectorRegistry(support_collectors_without_names=True)
1039+
mock_collector = MagicMock()
1040+
mock_collector.describe.return_value = []
1041+
registry.register(mock_collector)
1042+
self.assertEqual(list(registry.restricted_registry(['metric']).collect()), [])
10341043
mock_collector.collect.assert_called()
10351044

10361045
def test_restricted_registry_does_not_yield_while_locked(self):

tests/test_multiprocess.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def setUp(self):
5252
self.tempdir = tempfile.mkdtemp()
5353
os.environ['PROMETHEUS_MULTIPROC_DIR'] = self.tempdir
5454
values.ValueClass = MultiProcessValue(lambda: 123)
55-
self.registry = CollectorRegistry()
55+
self.registry = CollectorRegistry(support_collectors_without_names=True)
5656
self.collector = MultiProcessCollector(self.registry)
5757

5858
@property

0 commit comments

Comments
 (0)