pythonprometheusprometheus-pushgateway

How to push metrics with Python and Prometheus Pushgateway


I wish to push a multi-labeled metric into Prometheus using the Pushgateway. The documentation offer a curl example but I need it sent via Python. In addition, I'd like to embed multiple labels into the metric.


Solution

  • 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)