I was trying to write an exporter by following online instructions. While debugging an issue, noticed that 'collect' is executing twice for the below code. I am unable to figure out why.
1. When I start the collector, it logs once.
2018-02-20 05:26:39,670 - vnf_metrics.py - DEBUG - Starting script
2018-02-20 05:26:39,674 - vnf_metrics.py - DEBUG - inside collect
2018-02-20 05:26:39,674 - vnf_metrics.py - INFO - Metric samples count: 1
2. When I query 9503 port, I can see its executing twice. Is it the expected behavior or am I doing something wrong here?.
2018-02-20 05:27:33,735 - vnf_metrics.py - DEBUG - inside collect
2018-02-20 05:27:33,735 - vnf_metrics.py - INFO - Metric samples count: 1
2018-02-20 05:27:34,152 - vnf_metrics.py - DEBUG - inside collect
2018-02-20 05:27:34,152 - vnf_metrics.py - INFO - Metric samples count: 1
Code snippet:
---------------------
#!/usr/bin/env python
import os
import sys
import time
sys.path.append('/opt/vnf_metrics/lib')
import logger
import prometheus_client
from prometheus_client import start_http_server, Metric, REGISTRY
class DataPublisher(object):
def __init__(self):
pass
def collect(self):
log.debug("inside collect")
labels = {}
metric = Metric('test_metrics1', 'Test metrics 1', 'counter')
metric.add_sample('test_metrics1', value=float(1.00), labels=labels)
log.info("Metric samples count: %d" % (len(metric.samples)))
yield metric
def main():
'''Main'''
global log
exclude_metrics = None
log_dir = "/var/log"
debug = True
script_name = os.path.basename(sys.argv[0])
log_file = os.path.join(log_dir,script_name + ".log")
log = logger.setup_logger(script_name, log_file, debug)
log.debug("Starting script")
start_http_server(9503)
REGISTRY.register(DataPublisher())
while True: time.sleep(1)
if __name__ == '__main__':
main()