question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Duplicated timeseries in CollectorRegistry" when testing

See original GitHub issue

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.

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics

app1 = Flask(__name__)
metrics1 = PrometheusMetrics(app1)
# some test

app2 = Flask(__name__)
metrics2 = PrometheusMetrics(app2)
# some other test

The output:

    metrics2 = PrometheusMetrics(app2)
/usr/local/lib/python3.6/site-packages/prometheus_flask_exporter/__init__.py:115: in __init__
    self.init_app(app)
/usr/local/lib/python3.6/site-packages/prometheus_flask_exporter/__init__.py:137: in init_app
    self._defaults_prefix, app
/usr/local/lib/python3.6/site-packages/prometheus_flask_exporter/__init__.py:253: in export_defaults
    **buckets_as_kwargs
/usr/local/lib/python3.6/site-packages/prometheus_client/metrics.py:494: in __init__
    labelvalues=labelvalues,
/usr/local/lib/python3.6/site-packages/prometheus_client/metrics.py:103: in __init__
    registry.register(self)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <prometheus_client.registry.CollectorRegistry object at 0x7f425acb0d68>, collector = <prometheus_client.metrics.Histogram object at 0x7f424a45c3c8>

    def register(self, collector):
        '''Add a collector to the registry.'''
        with self._lock:
            names = self._get_names(collector)
            duplicates = set(self._names_to_collectors).intersection(names)
            if duplicates:
                raise ValueError(
                    'Duplicated timeseries in CollectorRegistry: {0}'.format(
>                       duplicates))
E               ValueError: Duplicated timeseries in CollectorRegistry: {'flask_http_request_duration_seconds_bucket', 'flask_http_request_duration_seconds_count', 'flask_http_request_duration_seconds_sum', 'flask_http_request_duration_seconds_created'}

I was able to get rid of the error by instantiating a new CollectorRegistry at every app instantiation.

from flask import Flask
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_client import CollectorRegistry

app1 = Flask(__name__)
metrics1 = PrometheusMetrics(app1, registry=CollectorRegistry())

app2 = Flask(__name__)
metrics2 = PrometheusMetrics(app2, registry=CollectorRegistry())

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
rycus86commented, Jan 12, 2019

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 CollectorRegistry for each test: https://github.com/rycus86/prometheus_flask_exporter/blob/master/tests/unittest_helper.py#L22

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!

1reaction
andersbogsnescommented, Sep 26, 2019

Sorry for necroing - just remembered that I hadn’t thanked you for putting together the test example!

Great work - much appreciated!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicated timeseries in CollectorRegistry when running ...
Apparently there are "duplicated time series" so it seems that the previous run left something not closed. If I close spyder, and open...
Read more >
Hi, "Duplicated timeseries in CollectorRegistry" issue. [in Python]
So, I'm using a while loop so the startserver will be open. In the while loop I got all the collectors and it...
Read more >
Duplicated timeseries in CollectorRegistry - Google Groups
Im currently facing an issue where I do get an error that is saying: ; ` ValueError: Duplicated timeseries in CollectorRegistry: {' ...
Read more >
[Example code]-Using Prometheus with Connexion - ValueError
Coding example for the question Using Prometheus with Connexion - ValueError: Duplicated timeseries in CollectorRegistry.
Read more >
Python Examples of prometheus_client.CollectorRegistry
def setUp(self): self.registry = prometheus_client.CollectorRegistry() self.some_gauge = prometheus_client.Gauge( "some_gauge", "Some gauge.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found