custom collector with client_python successive collections

49 views
Skip to first unread message

Cory Robinson

unread,
Jun 20, 2020, 11:48:43 AM6/20/20
to Prometheus Users
Hi,

When implementing a simple custom collector with the python client, refreshing the browser will generate 2 successive collections.
This tends to cause "duplicate" unneeded successive, say, REST API calls for example,  when collecting metrics from REST API endpoints real-world scenarios.
How can this be avoided?

Here is some sample case below that exhibits this behavior...

import time
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY

class CustomCollector(object):
    def collect(self):
        c = GaugeMetricFamily('my_gauge''Help text'labels=['foo'])
        c.add_metric(['bar'], 1.7)
        print("{}{}".format(time.time(), c))
        yield c
    
    def describe(self):
        return []

if __name__ == '__main__':
    start_http_server(8000)
    REGISTRY.register(CustomCollector())
    while True:
        time.sleep(1)

Console output at webpage refresh on localhost:8000:
> python test.py
1592667742.5491908, Metric(my_gauge, Help text, gauge, , [Sample(name='my_gauge', labels={'foo': 'bar'}, value=1.7, timestamp=None, exemplar=None)])
1592667742.6191902, Metric(my_gauge, Help text, gauge, , [Sample(name='my_gauge', labels={'foo': 'bar'}, value=1.7, timestamp=None, exemplar=None)])

Notice the times come in very quick succession. When collecting real metrics from a REST API endpoint, the metrics displayed on the webpage will always be the first metric, not the 2nd succession.

Thanks,
Cory

Cory Robinson

unread,
Jun 27, 2020, 9:59:40 AM6/27/20
to Prometheus Users
Hi folks, any ideas on this issue I am facing with custom collectors using the Python client?

Thanks,
Cory
Reply all
Reply to author
Forward
0 new messages