简体   繁体   中英

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'}

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM