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:
0ValueError: Duplicated timeseries in CollectorRegistry: {'test', 'test_total', 'test_created'}
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.