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: 2 additions & 0 deletions prometheus_client/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def unregister(self, collector: Collector) -> None:
for name in self._collector_to_names[collector]:
del self._names_to_collectors[name]
del self._collector_to_names[collector]
if collector in self._collectors_without_names:
self._collectors_without_names.remove(collector)

def _get_names(self, collector):
"""Get names of timeseries the collector produces and clashes with."""
Expand Down
15 changes: 15 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,21 @@ def test_unregister_works(self):
registry.unregister(s)
Gauge('s_count', 'help', registry=registry)

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

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

collector = NamelessCollector()
registry.register(collector)
registry.unregister(collector)
# A nameless collector must be removed from the collectors-without-names
# list too, otherwise a restricted registry keeps collecting it after it
# was unregistered.
self.assertEqual([], list(registry.restricted_registry(['foo']).collect()))

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