-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Duplicated timeseries in CollectorRegistry" when testing #17
Comments
Hi, Thanks a lot for reporting this! If this is strictly a testing related problem for you, I've solved it exactly the way you described it, by using a new There's also a way more hacky approach, where you can unregister all the metrics collectors before each tests, like this: https://github.com/rycus86/webhook-proxy/blob/master/tests/unittest_helper.py#L39 Does this help? Thanks! |
Thanks for your reply! I ended unregistering the metrics collectors in my test fixture, as you suggested. |
Hi, I'm having the same issue as maxime, but the solution provided doesn't work for me. I am using pytest and Gunicorn and using the app_factory approach My pytest fixture looks like this:
My app factory registers extensions like this:
Versions: When I try that, I still get the Duplicated Timeseries, also when I tried using the Any ideas on why it doesn't work? |
Can you try overwriting the registry first then creating the app here?
I think maybe the problem happens when you instantiate the |
I changed my testfixture to the following, but unfortunately it still fails... I can get it to pass by changing the scope to session, but it's not ideal. Not sure how I would make sure that the module-level metrics object is reinstantiated, I would have to patch it out and replicate all the functionality...
|
@andersbogsnes have a look at https://github.com/rycus86/prometheus_flask_exporter/blob/master/examples/pytest-app-factory/test/test_example.py where I tried to (mostly) replicate your use-case. I ended up having to recreate the metrics object (I used the internal Gunicorn one, but shouldn't matter), then it was working OK: @pytest.fixture()
def app() -> Flask:
app = create_app('myapp.config.TestConfig')
prometheus_client.REGISTRY = prometheus_client.CollectorRegistry(auto_describe=True)
myapp_extensions.metrics = GunicornInternalPrometheusMetrics(app=None, group_by="endpoint")
ctx = app.app_context()
ctx.push()
yield app
ctx.pop() Does this help? |
Sorry for necroing - just remembered that I hadn't thanked you for putting together the test example! Great work - much appreciated! |
Thanks a lot for coming back to say this. :) |
Hi,
Thanks for this awesome module!
When testing my flask application, I instantiate a new instance of my application for every test using fixtures. For the first test, everything goes right, but on subsequent tests, I get a "Duplicated timeseries" error. My understanding is that
PrometheusMetrics
uses the same registry for every application. Is this the expected behavior?Here's a minimal snippet that reproduces this behaviour.
The output:
I was able to get rid of the error by instantiating a new CollectorRegistry at every app instantiation.
Thanks!
The text was updated successfully, but these errors were encountered: