运行 prometheus 时 CollectorRegistry 中出现重复的时间序列 (python)

Kan*_*bot 6 python spyder prometheus

我正在尝试遵循客户端 python 项目的自述文件。所以我有以下代码

from prometheus_client import start_http_server, Summary
import random
import time

# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(t):
    """A dummy function that takes some time."""
    time.sleep(t)

if __name__ == '__main__':
    # Start up the server to expose the metrics.
    start_http_server(8000)
    # Generate some requests.
    while True:
        process_request(random.random())
Run Code Online (Sandbox Code Playgroud)

我仍在尝试了解如何在这里使用普罗米修斯,但我的问题有点不同。我正在使用 anaconda,并且创建了 conda 环境。在这个环境中,我安装了spyder作为我的IDE。所以我把这段代码放在一个文件中并运行它。

第一次没有问题(我可以用普罗米修斯做一些监控,但那是另一个故事了)。但是,当我想停止此操作时,我按 Ctrl-C 并按预期程序退出。

但是,如果我第二次运行它,我会收到以下错误:

Traceback (most recent call last):

  File "C:\Users\user\Prometheus\firstPro\primer.py", line 13, in <module>
    REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

  File "C:\Users\user\anaconda3\envs\firstPro\lib\site-packages\prometheus_client\metrics.py", line 121, in __init__
    registry.register(self)

  File "C:\Users\user\anaconda3\envs\firstPro\lib\site-packages\prometheus_client\registry.py", line 31, in register
    duplicates))

ValueError: Duplicated timeseries in CollectorRegistry: {'request_processing_seconds_created', 'request_processing_seconds_sum', 'request_processing_seconds_count', 'request_processing_seconds'}
Run Code Online (Sandbox Code Playgroud)

我无法再次运行 python 程序。显然存在“重复的时间序列”,因此上次运行似乎留下了一些未关闭的内容。

如果我关闭spyder,然后再次打开它,我可以第一次再次运行它。

有人可以帮助我了解这里发生的事情吗?

小智 6

您可能正在双重注册收集器。 https://github.com/prometheus/client_python/issues/468