How to push metrics with Python and Prometheus Pushgateway

Here's what I ended up doing - it took a while to get right. While ideally I would have used the Prometheus python client designed specifically for this purpose, it appears that it doesn't support multiple labels in some cases and the documentation is virtually non-existent - so I went with a home-brewed solution.

The code below uses gevent and supports multiple (comma-delimited) pushgateway urls (like "pushgateway1.my.com:9092, pushgateway2.my.com:9092").

import gevent
import requests

def _submit_wrapper(urls, job_name, metric_name, metric_value, dimensions):
    dim = ''
    headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
    for key, value in dimensions.iteritems():
        dim += '/%s/%s' % (key, value)
    for url in urls:
        requests.post('http://%s/metrics/job/%s%s' % (url, job_name, dim),
                      data='%s %s\n' % (metric_name, metric_value), headers=headers)


def submit_metrics(job_name, metric_name, metric_value, dimensions={}):
    from ..app import config
    cfg = config.init()
    urls = cfg['PUSHGATEWAY_URLS'].split(',')
    gevent.spawn(_submit_wrapper, urls, job_name, metric_name, metric_value, dimensions)

First step: Install the client:

pip install prometheus_client

Second step: Paste the following into a Python interpreter:

from prometheus_client import CollectorRegistry, Gauge, push_to_gateway

registry = CollectorRegistry()
g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry)
g.set_to_current_time()
push_to_gateway('localhost:9091', job='batchA', registry=registry)

This is documented for the Python client: https://github.com/prometheus/client_python#exporting-to-a-pushgateway