|
| 1 | +--- |
| 2 | +title: Pushgateway |
| 3 | +weight: 5 |
| 4 | +--- |
| 5 | + |
| 6 | +The [Prometheus Pushgateway](https://github.com/prometheus/pushgateway) exists to allow ephemeral and batch jobs to expose their metrics to Prometheus. |
| 7 | +Since these kinds of jobs may not exist long enough to be scraped, they can instead push their metrics to a Pushgateway. |
| 8 | +The Pushgateway then exposes these metrics to Prometheus. |
| 9 | + |
| 10 | +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) Java class allows you to push metrics to a Prometheus Pushgateway. |
| 11 | + |
| 12 | +Example |
| 13 | +------- |
| 14 | + |
| 15 | +{{< tabs "uniqueid" >}} |
| 16 | +{{< tab "Gradle" >}} |
| 17 | +``` |
| 18 | +implementation 'io.prometheus:prometheus-metrics-core:1.3.0' |
| 19 | +implementation 'io.prometheus:prometheus-metrics-exporter-pushgateway:1.3.0' |
| 20 | +``` |
| 21 | +{{< /tab >}} |
| 22 | +{{< tab "Maven" >}} |
| 23 | +```xml |
| 24 | +<dependency> |
| 25 | + <groupId>io.prometheus</groupId> |
| 26 | + <artifactId>prometheus-metrics-core</artifactId> |
| 27 | + <version>1.3.0</version> |
| 28 | +</dependency> |
| 29 | +<dependency> |
| 30 | + <groupId>io.prometheus</groupId> |
| 31 | + <artifactId>prometheus-metrics-exporter-pushgateway</artifactId> |
| 32 | + <version>1.3.0</version> |
| 33 | +</dependency> |
| 34 | +``` |
| 35 | +{{< /tab >}} |
| 36 | +{{< /tabs >}} |
| 37 | + |
| 38 | +```java |
| 39 | +public class ExampleBatchJob { |
| 40 | + |
| 41 | + private static PushGateway pushGateway = PushGateway.builder() |
| 42 | + .address("localhost:9091") // not needed as localhost:9091 is the default |
| 43 | + .job("example") |
| 44 | + .build(); |
| 45 | + |
| 46 | + private static Gauge dataProcessedInBytes = Gauge.builder() |
| 47 | + .name("data_processed") |
| 48 | + .help("data processed in the last batch job run") |
| 49 | + .unit(Unit.BYTES) |
| 50 | + .register(); |
| 51 | + |
| 52 | + public static void main(String[] args) throws Exception { |
| 53 | + try { |
| 54 | + long bytesProcessed = processData(); |
| 55 | + dataProcessedInBytes.set(bytesProcessed); |
| 56 | + } finally { |
| 57 | + pushGateway.push(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static long processData() { |
| 62 | + // Imagine a batch job here that processes data |
| 63 | + // and returns the number of Bytes processed. |
| 64 | + return 42; |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Basic Auth |
| 70 | +---------- |
| 71 | + |
| 72 | +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports basic authentication. |
| 73 | + |
| 74 | +```java |
| 75 | +PushGateway pushGateway = PushGateway.builder() |
| 76 | + .job("example") |
| 77 | + .basicAuth("my_user", "my_password") |
| 78 | + .build(); |
| 79 | +``` |
| 80 | + |
| 81 | +The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this. |
| 82 | + |
| 83 | +SSL |
| 84 | +--- |
| 85 | + |
| 86 | +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports SSL. |
| 87 | + |
| 88 | +```java |
| 89 | +PushGateway pushGateway = PushGateway.builder() |
| 90 | + .job("example") |
| 91 | + .scheme(Scheme.HTTPS) |
| 92 | + .build(); |
| 93 | +``` |
| 94 | + |
| 95 | +However, this requires that the JVM can validate the server certificate. |
| 96 | + |
| 97 | +If you want to skip certificate verification, you need to provide your own [HttpConnectionFactory](/client_java/api/io/prometheus/metrics/exporter/pushgateway/HttpConnectionFactory.html). |
| 98 | +The `PushGatewayTestApp` in `integration-tests/it-pushgateway` has a complete example of this. |
| 99 | + |
| 100 | +Configuration Properties |
| 101 | +------------------------ |
| 102 | + |
| 103 | +The [PushGateway](/client_java/api/io/prometheus/metrics/exporter/pushgateway/PushGateway.html) supports a couple of properties that can be configured at runtime. See [config](../../config/config). |
0 commit comments