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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.

You Might Also Like