I have written a small script that instruments a Flask application and I want to write unit tests where every test can write requests against a mock-up Flask app and test metrics without having to deal with metrics/requests from previous test methods like so:
def test_grouped_codes():
app = create_app()
instrument(app)
# test stuff
But I am not able to "reset" the registry and so I get the error "Duplicated timeseries in CollectorRegistry" all the time.
How can I reset the registry (or set it to an empty registry) of the Prometheus Python client library during run-time?
Among other things I have tried the following, but it does not work:
def create_app():
app = Flask(__name__)
registry = CollectorRegistry() # Create new registry.
prometheus_client.REGISTRY = registry # Try to override global registry.
prometheus_client.registry.REGISTRY = registry # Try to override global registry.
@app.route("/")
def home():
return "Hello World!"
# More functions ...
@app.route("/metrics")
@FlaskInstrumentator.do_not_track()
def metrics():
data = generate_latest(registry)
headers = {
"Content-Type": CONTENT_TYPE_LATEST,
"Content-Length": str(len(data))}
return data, 200, headers
return app
I have found the following qa on stack overflow here. @brian-brazil recommends to declare the metrics on the module-level, but then I would have to hard-code label names which I wanted to avoid. Some use handler
, the others method
or path
so I want to keep that customizable.