Duplicated timeseries in CollectorRegistry when running prometheus (python)

Viewed 8463

I am trying to follow the readme of the client python project. So I have the following code

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

I am still trying to understand how to use prometheus here, but my question is a bit different. I am using anaconda, and I have created a conda enviorment. Inside this environment I have installed spyder to use as my IDE. So I put this code in a file and I run it.

The first time there is no problem (and I could do some monitoring with prometheus but that is another story). However, when I want to stop this, I press Ctrl-C and as expected the program exits.

However if I run it a second time I got the following error:

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

and I cannot run the python program again. Apparently there are "duplicated time series" so it seems that the previous run left something not closed.

If I close spyder, and open it again I can run it again the first time.

Can someone help me on what is happening here?

1 Answers
Related