简体   繁体   English

仅当 Prometheus 指标不存在时才在 Python 中注册它

[英]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.我正在使用多进程 prometheus 收集器(在 python 中),但这个问题可能也存在于单进程中。

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'} ValueError: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.最终成为如何设置 MultiProcessCollector 的问题......确保在子流程级别使用 CollectorRegistry 而不是 Registry 进行初始化。

Proper Solution:正确的解决方案:

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

Incorrect Solution:不正确的解决方法:

prometheus_client.multiprocess.MultiProcessCollector(CollectorRegistry())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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