java.lang.Object
io.prometheus.metrics.exporter.pushgateway.PushGateway

public class PushGateway extends Object
Export metrics via the Prometheus 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. This Java class allows pushing the contents of a PrometheusRegistry to a Pushgateway.

Example usage:


 void executeBatchJob() throws Exception {
     PrometheusRegistry registry = new PrometheusRegistry();
     Gauge duration = Gauge.builder()
             .name("my_batch_job_duration_seconds")
             .help("Duration of my batch job in seconds.")
             .register(registry);
     Timer durationTimer = duration.startTimer();
     try {
         // Your code here.

         // This is only added to the registry after success,
         // so that a previous success in the Pushgateway isn't overwritten on failure.
         Gauge lastSuccess = Gauge.builder()
                 .name("my_batch_job_last_success")
                 .help("Last time my batch job succeeded, in unixtime.")
                 .register(registry);
         lastSuccess.set(System.currentTimeMillis());
     } finally {
         durationTimer.observeDuration();
         PushGateway pg = PushGateway.builder()
                 .address("127.0.0.1:9091")
                 .job("my_batch_job")
                 .registry(registry)
                 .build();
         pg.pushAdd();
     }
 }
 

See https://github.com/prometheus/pushgateway.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Method Summary

    Modifier and Type
    Method
    Description
     
    builder(io.prometheus.metrics.config.PrometheusProperties config)
    The PrometheusProperties will be used to override what is set in the PushGateway.Builder.
    void
    Deletes metrics from the Pushgateway.
    void
    Push all metrics.
    void
    push(io.prometheus.metrics.model.registry.Collector collector)
    Push a single metric.
    void
    push(io.prometheus.metrics.model.registry.MultiCollector collector)
    Push a single collector.
    void
    Like push(), but only metrics with the same name as the newly pushed metrics are replaced.
    void
    pushAdd(io.prometheus.metrics.model.registry.Collector collector)
    Like push(Collector), but only the specified metric will be replaced.
    void
    pushAdd(io.prometheus.metrics.model.registry.MultiCollector collector)
    Like push(MultiCollector), but only the metrics from the collector will be replaced.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • push

      public void push() throws IOException
      Push all metrics. All metrics with the same job and grouping key are replaced.

      This uses the PUT HTTP method.

      Throws:
      IOException
    • push

      public void push(io.prometheus.metrics.model.registry.Collector collector) throws IOException
      Push a single metric. All metrics with the same job and grouping key are replaced.

      This is useful for pushing a single Gauge.

      This uses the PUT HTTP method.

      Throws:
      IOException
    • push

      public void push(io.prometheus.metrics.model.registry.MultiCollector collector) throws IOException
      Push a single collector. All metrics with the same job and grouping key are replaced.

      This uses the PUT HTTP method.

      Throws:
      IOException
    • pushAdd

      public void pushAdd() throws IOException
      Like push(), but only metrics with the same name as the newly pushed metrics are replaced.

      This uses the POST HTTP method.

      Throws:
      IOException
    • pushAdd

      public void pushAdd(io.prometheus.metrics.model.registry.Collector collector) throws IOException
      Like push(Collector), but only the specified metric will be replaced.

      This uses the POST HTTP method.

      Throws:
      IOException
    • pushAdd

      public void pushAdd(io.prometheus.metrics.model.registry.MultiCollector collector) throws IOException
      Like push(MultiCollector), but only the metrics from the collector will be replaced.

      This uses the POST HTTP method.

      Throws:
      IOException
    • delete

      public void delete() throws IOException
      Deletes metrics from the Pushgateway.

      This uses the DELETE HTTP method.

      Throws:
      IOException
    • builder

      public static PushGateway.Builder builder()
    • builder

      public static PushGateway.Builder builder(io.prometheus.metrics.config.PrometheusProperties config)
      The PrometheusProperties will be used to override what is set in the PushGateway.Builder.