Reliable Insights

A blog on monitoring, scale and operational sanity

Writing a Jenkins exporter in Python

I previously talked about writing JSON exporters in Python for Prometheus. Since then, the API for custom collectors in the Python client has been improved. So let’s see how easy it is to create a new exporter!

The new API removes repetitive code and handles the structure of metrics for you. A minimal Jenkins exporter would expose the last time each job succeeded, giving you just enough information to alert if it was too long ago:

import json
import time
import urllib2
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY

class JenkinsCollector(object):
  def collect(self):
    metric = GaugeMetricFamily(
        'Jenkins build timestamp in unixtime for lastSuccessfulBuild',

    result = json.load(urllib2.urlopen(
        + "jobs[name,lastSuccessfulBuild[timestamp]]"))

    for job in result['jobs']:
      name = job['name']
      # If there's a null result, we want to export a zero.
      status = job['lastSuccessfulBuild'] or {}
      metric.add_metric([name], status.get('timestamp', 0) / 1000.0)

    yield metric

if __name__ == "__main__":
  while True: time.sleep(1)

The core here is the GaugeMetricFamily which creates a Gauge and specifies labels, add_metric which adds a sample and then the yield to return the metric. I avoid calling my label job, as that label already has a meaning for Prometheus.


This can be expanded with more metrics. A full version of this is on Github. To run it:

pip install prometheus_client
python http://jenkins:8080/

Point the exporter at your actual jenkins server. You should now be able to see your metrics at http://localhost:9118/metrics.


This exporter took me about an hour to write. Most of that time was spent figuring out which data was available, and how to get it efficiently out of Jenkins. Writing the code was the easy part!


Update: Vanesa from Lovoo has taken this example exporter and expanded it  be more fully-fledged and to cover more metrics, you can find it at 

Brian BrazilWriting a Jenkins exporter in Python
Share this post

Related Posts