'I get the ValueError: Duplicated timeseries in CollectorRegistry error, when i try to import the Dict where the childs are stored

I created a Python project for monitoring with prometheus. But when i try to get the dictionary to my file where the flask server rund i get a ValueError: Duplicated timeseries in CollectorRegistry: error. I don't know where it comes from.

i import the dictionary like this:

import actions.actions as a

i need to reload the import so that the data gets synced between both files:

from imp import reload

the haul webserver file looks like this:

import sys
from flask import Flask, Response
import prometheus_client
from prometheus_client import Summary, Counter, Histogram, Gauge
import actions.actions as a
from imp import reload




#################### Monitoring ####################

app = Flask(__name__)

@app.route("/metrics")
def requests_count():
data = a.graphs
    reload(a)
    res = []
    for k,v in data.items():
        res.append(prometheus_client.generate_latest(v))
    return Response(res, mimetype="text/plain")

 if __name__ == '__main__':
    app.run(host='0.0.0.0', port='5000', debug=True)

in the other file i use prometheus like this:

graphs = {}
graphs['helpful'] = Counter('python_request_helpful_total', 'the total of helpful interactions')
graphs['nothelpful'] = Counter('python_request_nothelpful_total', 'the total of not helpful interactions')
graphs['othersasked'] = Counter('python_request_othersasked_total', 'The total of fallbacks in others Asked')
graphs['fallback'] = Counter('python_request_fallback_total', 'The total number of total fallbacks')


Solution 1:[1]

Per this link https://github.com/prometheus/client_python/issues/626 have you tried creating a separate registry instead of the default one?

 registry = CollectorRegistry()
 graphs = {}
 graphs['helpful'] = Counter('python_request_helpful_total', 'the total of helpful interactions', registry=registry)
 graphs['nothelpful'] = Counter('python_request_nothelpful_total', 'the total of not helpful interactions', registry=registry)
 graphs['othersasked'] = Counter('python_request_othersasked_total', 'The total of fallbacks in others Asked', registry=registry)
 graphs['fallback'] = Counter('python_request_fallback_total', 'The total number of total fallbacks', registry=registry)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amro Younes