Skip to content

Add clear() function to CollectorRegistry. #1141

@EdgarMontelongo

Description

@EdgarMontelongo

Hello!
I was working with the prometheus_client python library and I noticed that there isn't a method for removing all collectors on a CollectorRegistry instance. Yet there is a method for removing single collectors from the registry.

I propose to add a function that removes all collectors from the registry.

This is an example code of the actual implementation:

from prometheus_client import Gauge, CollectorRegistry
# Create the registry
registry = CollectorRegistry()
# Create and set the gauges
g1 = Gauge("Metric1", "Description1", registry=registry)
g2 = Gauge("Metric2", "Description2", registry=registry)
g3 = Gauge("Metric3", "Description3", registry=registry)  
g1.set(42)
g2.set(43)
g3.set(44)
# Remove the gauges from the registry manually
registry.unregister(g1)
registry.unregister(g2)
registry.unregister(g3)

This is the code expected after this proposed enhancement:

# Create the registry
registry = CollectorRegistry()
# Create and set the gauges
g1 = Gauge("Metric1", "Description1", registry=registry)
g2 = Gauge("Metric2", "Description2", registry=registry)
g3 = Gauge("Metric3", "Description3", registry=registry)  
g1.set(42)
g2.set(43)
g3.set(44)
# Remove the gauges from the registry with one function call.
registry.clear()

I hope this addition will remove boilerplate when clearing registries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions