client_java
GitHubToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeToggle Dark/Light/Auto modeBack to homepage

Pushgateway

The Prometheus Pushgateway exists to allow ephemeral and batch jobs to expose their metrics to Prometheus. Since these kinds of jobs may not exist long enough to be scraped, they can instead push their metrics to a Pushgateway. The Pushgateway then exposes these metrics to Prometheus.

The PushGateway Java class allows you to push metrics to a Prometheus Pushgateway.

Example

implementation 'io.prometheus:prometheus-metrics-core:1.3.0'
implementation 'io.prometheus:prometheus-metrics-exporter-pushgateway:1.3.0'
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>prometheus-metrics-core</artifactId>
    <version>1.3.0</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>prometheus-metrics-exporter-pushgateway</artifactId>
    <version>1.3.0</version>
</dependency>
public class ExampleBatchJob {

    private static PushGateway pushGateway = PushGateway.builder()
            .address("localhost:9091") // not needed as localhost:9091 is the default
            .job("example")
            .build();

    private static Gauge dataProcessedInBytes = Gauge.builder()
            .name("data_processed")
            .help("data processed in the last batch job run")
            .unit(Unit.BYTES)
            .register();

    public static void main(String[] args) throws Exception {
        try {
            long bytesProcessed = processData();
            dataProcessedInBytes.set(bytesProcessed);
        } finally {
            pushGateway.push();
        }
    }

    public static long processData() {
        // Imagine a batch job here that processes data
        // and returns the number of Bytes processed.
        return 42;
    }
}

Basic Auth

The PushGateway supports basic authentication.

PushGateway pushGateway = PushGateway.builder()
    .job("example")
    .basicAuth("my_user", "my_password")
    .build();

The PushGatewayTestApp in integration-tests/it-pushgateway has a complete example of this.

Bearer token

The PushGateway supports Bearer token authentication.

PushGateway pushGateway = PushGateway.builder()
    .job("example")
    .bearerToken("my_token")
    .build();

The PushGatewayTestApp in integration-tests/it-pushgateway has a complete example of this.

SSL

The PushGateway supports SSL.

PushGateway pushGateway = PushGateway.builder()
    .job("example")
    .scheme(Scheme.HTTPS)
    .build();

However, this requires that the JVM can validate the server certificate.

If you want to skip certificate verification, you need to provide your own HttpConnectionFactory. The PushGatewayTestApp in integration-tests/it-pushgateway has a complete example of this.

Configuration Properties

The PushGateway supports a couple of properties that can be configured at runtime. See config.