Reputation: 8109
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.
Upvotes: 16
Views: 42126
Reputation: 695
You can use GaugeMetricFamily
class. you can take the following steps:
prometheus_client
:pip install prometheus_client
from prometheus_client import CollectorRegistry, push_to_gateway
from prometheus_client.core import GaugeMetricFamily
class CustomCollector(object):
# make sure you define collect method
def collect():
# float metric you want to push
metric = "<some_float_value>"
# create gauge metric, define label names
metric_gauge = GaugeMetricFamily("metric_name", "metric_description", labels=["label1", "label2"])
# add metric value and label values
metric_gauge.add_metric(["label1_value", "label2_value"], metric)
yield metric_gauge
registry = CollectorRegistry()
registry.register(CustomCollector())
# Push metrics to pushgateway (modify the url accordingly)
push_to_gateway('localhost:9091', job='job_name', registry=registry)
Upvotes: 1
Reputation: 467
In one command and without any scripts:
echo "some_metric 3.14" | curl --data-binary @- http://pushgateway.example.org:9091/metrics/job/some_job
Upvotes: 0
Reputation: 1698
If you can't use prometheus_client
, here is short version of requests
:
import requests
headers = {'X-Requested-With': 'Python requests', 'Content-type': 'text/xml'}
url = "https://pushgateway.example.com/metrics/job/job_name/instance/instance_name"
data = "websites_offline{website=\"example.com\"} 0\n"
r = requests.post(url, headers=headers, data=data)
print(r.reason)
print(r.status_code)
More items can be added after \n
(new line) in a data variable.
Upvotes: 8
Reputation: 281
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)
Upvotes: 14
Reputation: 34112
This is documented for the Python client: https://github.com/prometheus/client_python#exporting-to-a-pushgateway
Upvotes: 7
Reputation: 8109
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)
Upvotes: 6