| 1 | --- |
| 2 | title: Pushgateway |
| 3 | weight: 3 |
| 4 | --- |
| 5 | |
| 6 | The [Pushgateway](https://github.com/prometheus/pushgateway) |
| 7 | allows ephemeral and batch jobs to expose their metrics to Prometheus. |
| 8 | |
| 9 | ```python |
| 10 | from prometheus_client import CollectorRegistry, Gauge, push_to_gateway |
| 11 | |
| 12 | registry = CollectorRegistry() |
| 13 | g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry) |
| 14 | g.set_to_current_time() |
| 15 | push_to_gateway('localhost:9091', job='batchA', registry=registry) |
| 16 | ``` |
| 17 | |
| 18 | A separate registry is used, as the default registry may contain other metrics |
| 19 | such as those from the Process Collector. |
| 20 | |
| 21 | Pushgateway functions take a grouping key. `push_to_gateway` replaces metrics |
| 22 | with the same grouping key, `pushadd_to_gateway` only replaces metrics with the |
| 23 | same name and grouping key and `delete_from_gateway` deletes metrics with the |
| 24 | given job and grouping key. See the |
| 25 | [Pushgateway documentation](https://github.com/prometheus/pushgateway/blob/master/README.md) |
| 26 | for more information. |
| 27 | |
| 28 | `instance_ip_grouping_key` returns a grouping key with the instance label set |
| 29 | to the host's IP address. |
| 30 | |
| 31 | # Handlers for authentication |
| 32 | |
| 33 | If the push gateway you are connecting to is protected with HTTP Basic Auth, |
| 34 | you can use a special handler to set the Authorization header. |
| 35 | |
| 36 | ```python |
| 37 | from prometheus_client import CollectorRegistry, Gauge, push_to_gateway |
| 38 | from prometheus_client.exposition import basic_auth_handler |
| 39 | |
| 40 | def my_auth_handler(url, method, timeout, headers, data): |
| 41 | username = 'foobar' |
| 42 | password = 'secret123' |
| 43 | return basic_auth_handler(url, method, timeout, headers, data, username, password) |
| 44 | registry = CollectorRegistry() |
| 45 | g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry) |
| 46 | g.set_to_current_time() |
| 47 | push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler) |
| 48 | ``` |
| 49 | |
| 50 | TLS Auth is also supported when using the push gateway with a special handler. |
| 51 | |
| 52 | ```python |
| 53 | from prometheus_client import CollectorRegistry, Gauge, push_to_gateway |
| 54 | from prometheus_client.exposition import tls_auth_handler |
| 55 | |
| 56 | |
| 57 | def my_auth_handler(url, method, timeout, headers, data): |
| 58 | certfile = 'client-crt.pem' |
| 59 | keyfile = 'client-key.pem' |
| 60 | return tls_auth_handler(url, method, timeout, headers, data, certfile, keyfile) |
| 61 | |
| 62 | registry = CollectorRegistry() |
| 63 | g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finished', registry=registry) |
| 64 | g.set_to_current_time() |
| 65 | push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler) |
| 66 | ``` |