Registering a Prometheus Metric in Python Only If It Doesn't Already Exist

I'm making use of the multiprocess prometheus collector (in python), but likely this problem would exist for single process as well.

Is there a way to check if a Counter Metric already exists, to prevent the attempt of registering a duplicate ? Multiple calls to the below code generate the appended error. Or alternatively to check if a given metric exists, and get it.

Some kind of look up in the Registry ? The docs seems rather lacking.

Code:

logging_counter = prometheus_client.Counter('test', 'test')

Error generated:

ValueError: Duplicated timeseries in CollectorRegistry: {'test', 'test_total', 'test_created'}

0

2 Answers

Ended up being an issue with how MultiProcessCollector was setup ... make sure that at the subprocess level you initialize with CollectorRegistry and not Registry.

Proper Solution:

prometheus_client.multiprocess.MultiProcessCollector(prometheus_client.registry.REGISTRY)

Incorrect Solution:

prometheus_client.multiprocess.MultiProcessCollector(CollectorRegistry())

This might be a very hacky way to do this but this is how I made it work for a single process;

self.registry = CollectorRegistry()

def collect_metrics(self):
    sorted_keys = sorted(self.stat_msg_dict.keys())
    for key in sorted_keys:
        ..... getting metric_key, metric_name, current_value .....
        gauge = self.registry._names_to_collectors.get(metric_key, None)
        if gauge is None:
            gauge = Gauge(metric_key, metric_name, registry=self.registry)
        gauge.set(current_value)
        

In this particular instance all my collectors are gauges.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest