Custom exporter using prometheus_python client

720 views
Skip to first unread message

basi...@gmail.com

unread,
Feb 20, 2018, 12:38:12 AM2/20/18
to Prometheus Users
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()

Brian Brazil

unread,
Feb 20, 2018, 4:03:41 AM2/20/18
to basi...@gmail.com, Prometheus Users
On 20 February 2018 at 05:38, <basi...@gmail.com> wrote:
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?.

This is the expected behaviour, this is the auto-describe behaviour.  See the end of https://github.com/prometheus/client_python#custom-collectors

Brian
 
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 

--
You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/33326d52-bbf4-483f-b35a-37567abfd78e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Michael

unread,
Jun 23, 2018, 11:53:52 AM6/23/18
to Prometheus Users
I was wondering about the same issue and what I found was that when I was using chrome to scrape the exporter it was sending multiple get requests. Try hitting the exporter with curl and I think you'll see the collect method only executed once when you do a scrape.
Reply all
Reply to author
Forward
0 new messages