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