Pushgateway
The Pushgateway allows ephemeral and batch jobs to expose their metrics to Prometheus.
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)
A separate registry is used, as the default registry may contain other metrics such as those from the Process Collector.
Pushgateway functions take a grouping key. push_to_gateway
replaces metrics
with the same grouping key, pushadd_to_gateway
only replaces metrics with the
same name and grouping key and delete_from_gateway
deletes metrics with the
given job and grouping key. See the
Pushgateway documentation
for more information.
instance_ip_grouping_key
returns a grouping key with the instance label set
to the host’s IP address.
If the push gateway you are connecting to is protected with HTTP Basic Auth, you can use a special handler to set the Authorization header.
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client.exposition import basic_auth_handler
def my_auth_handler(url, method, timeout, headers, data):
username = 'foobar'
password = 'secret123'
return basic_auth_handler(url, method, timeout, headers, data, username, password)
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, handler=my_auth_handler)
TLS Auth is also supported when using the push gateway with a special handler.
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
from prometheus_client.exposition import tls_auth_handler
def my_auth_handler(url, method, timeout, headers, data):
certfile = 'client-crt.pem'
keyfile = 'client-key.pem'
return tls_auth_handler(url, method, timeout, headers, data, certfile, keyfile)
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, handler=my_auth_handler)