Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion prometheus_client/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def register(self, collector: Collector) -> None:
for name in names:
self._names_to_collectors[name] = collector
self._collector_to_names[collector] = names
if self._support_collectors_without_names and not names:
if self._support_collectors_without_names and not names and collector not in self._collectors_without_names:
self._collectors_without_names.append(collector)

def unregister(self, collector: Collector) -> None:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,27 @@ def collect(self):
# was unregistered.
self.assertEqual([], list(registry.restricted_registry(['foo']).collect()))

def test_register_no_names_collector_twice(self):
registry = CollectorRegistry(support_collectors_without_names=True)

class NamelessCollector:
def collect(self):
return [GaugeMetricFamily('foo', 'help', value=42)]

collector = NamelessCollector()
restricted = registry.restricted_registry(['foo'])
registry.register(collector)
registry.register(collector)
self.assertEqual(1, len(list(registry.collect())))
self.assertEqual(1, len(list(restricted.collect())))
registry.unregister(collector)
self.assertEqual([], list(registry.collect()))
self.assertEqual([], list(restricted.collect()))
registry.register(collector)
self.assertEqual(1, len(list(restricted.collect())))
registry.unregister(collector)
self.assertEqual([], list(restricted.collect()))

def custom_collector(self, metric_family, registry):
class CustomCollector:
def collect(self):
Expand Down