Class PushGateway


  • public class PushGateway
    extends java.lang.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 class allows pushing the contents of a CollectorRegistry to a Pushgateway.

    Example usage:

     
       void executeBatchJob() throws Exception {
         CollectorRegistry registry = new CollectorRegistry();
         Gauge duration = Gauge.build()
             .name("my_batch_job_duration_seconds").help("Duration of my batch job in seconds.").register(registry);
         Gauge.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.build()
               .name("my_batch_job_last_success").help("Last time my batch job succeeded, in unixtime.").register(registry);
           lastSuccess.setToCurrentTime();
         } finally {
           durationTimer.setDuration();
           PushGateway pg = new PushGateway("127.0.0.1:9091");
           pg.pushAdd(registry, "my_batch_job");
         }
       }
     
     

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

    • Constructor Summary

      Constructors 
      Constructor Description
      PushGateway​(java.lang.String address)
      Construct a Pushgateway, with the given address.
      PushGateway​(java.net.URL serverBaseURL)
      Construct a Pushgateway, with the given URL.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      private static java.lang.String base64url​(java.lang.String v)  
      private static java.net.URL createURLSneakily​(java.lang.String urlString)
      Creates a URL instance from a String representation of a URL without throwing a checked exception.
      void delete​(java.lang.String job)
      Deletes metrics from the Pushgateway.
      void delete​(java.lang.String job, java.lang.String instance)
      Deprecated.
      void delete​(java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey)
      Deletes metrics from the Pushgateway.
      (package private) void doRequest​(CollectorRegistry registry, java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey, java.lang.String method)  
      static java.util.Map<java.lang.String,​java.lang.String> instanceIPGroupingKey()
      Returns a grouping key with the instance label set to the machine's IP address.
      void push​(CollectorRegistry registry, java.lang.String job)
      Pushes all metrics in a registry, replacing all those with the same job and no grouping key.
      void push​(CollectorRegistry registry, java.lang.String job, java.lang.String instance)
      void push​(CollectorRegistry registry, java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey)
      Pushes all metrics in a registry, replacing all those with the same job and grouping key.
      void push​(Collector collector, java.lang.String job)
      Pushes all metrics in a Collector, replacing all those with the same job and no grouping key.
      void push​(Collector collector, java.lang.String job, java.lang.String instance)
      void push​(Collector collector, java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey)
      Pushes all metrics in a Collector, replacing all those with the same job and grouping key.
      void pushAdd​(CollectorRegistry registry, java.lang.String job)
      Pushes all metrics in a registry, replacing only previously pushed metrics of the same name and job and no grouping key.
      void pushAdd​(CollectorRegistry registry, java.lang.String job, java.lang.String instance)
      void pushAdd​(CollectorRegistry registry, java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey)
      Pushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key.
      void pushAdd​(Collector collector, java.lang.String job)
      Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job and no grouping key.
      void pushAdd​(Collector collector, java.lang.String job, java.lang.String instance)
      void pushAdd​(Collector collector, java.lang.String job, java.util.Map<java.lang.String,​java.lang.String> groupingKey)
      Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key.
      private static java.lang.String readFromStream​(java.io.InputStream is)  
      void setConnectionFactory​(HttpConnectionFactory connectionFactory)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • MILLISECONDS_PER_SECOND

        private static final int MILLISECONDS_PER_SECOND
        See Also:
        Constant Field Values
      • gatewayBaseURL

        protected final java.lang.String gatewayBaseURL
    • Constructor Detail

      • PushGateway

        public PushGateway​(java.lang.String address)
        Construct a Pushgateway, with the given address.

        Parameters:
        address - host:port or ip:port of the Pushgateway.
      • PushGateway

        public PushGateway​(java.net.URL serverBaseURL)
        Construct a Pushgateway, with the given URL.

        Parameters:
        serverBaseURL - the base URL and optional context path of the Pushgateway server.
    • Method Detail

      • createURLSneakily

        private static java.net.URL createURLSneakily​(java.lang.String urlString)
        Creates a URL instance from a String representation of a URL without throwing a checked exception. Required because you can't wrap a call to another constructor in a try statement. TODO: Remove this along with other deprecated methods before version 1.0 is released.
        Parameters:
        urlString - the String representation of the URL.
        Returns:
        The URL instance.
      • push

        public void push​(CollectorRegistry registry,
                         java.lang.String job)
                  throws java.io.IOException
        Pushes all metrics in a registry, replacing all those with the same job and no grouping key.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • push

        public void push​(Collector collector,
                         java.lang.String job)
                  throws java.io.IOException
        Pushes all metrics in a Collector, replacing all those with the same job and no grouping key.

        This is useful for pushing a single Gauge.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • push

        public void push​(CollectorRegistry registry,
                         java.lang.String job,
                         java.util.Map<java.lang.String,​java.lang.String> groupingKey)
                  throws java.io.IOException
        Pushes all metrics in a registry, replacing all those with the same job and grouping key.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • push

        public void push​(Collector collector,
                         java.lang.String job,
                         java.util.Map<java.lang.String,​java.lang.String> groupingKey)
                  throws java.io.IOException
        Pushes all metrics in a Collector, replacing all those with the same job and grouping key.

        This is useful for pushing a single Gauge.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        public void pushAdd​(CollectorRegistry registry,
                            java.lang.String job)
                     throws java.io.IOException
        Pushes all metrics in a registry, replacing only previously pushed metrics of the same name and job and no grouping key.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        public void pushAdd​(Collector collector,
                            java.lang.String job)
                     throws java.io.IOException
        Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name and job and no grouping key.

        This is useful for pushing a single Gauge.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        public void pushAdd​(CollectorRegistry registry,
                            java.lang.String job,
                            java.util.Map<java.lang.String,​java.lang.String> groupingKey)
                     throws java.io.IOException
        Pushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        public void pushAdd​(Collector collector,
                            java.lang.String job,
                            java.util.Map<java.lang.String,​java.lang.String> groupingKey)
                     throws java.io.IOException
        Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name, job and grouping key.

        This is useful for pushing a single Gauge.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • delete

        public void delete​(java.lang.String job)
                    throws java.io.IOException
        Deletes metrics from the Pushgateway.

        Deletes metrics with no grouping key and the provided job. This uses the DELETE HTTP method.

        Throws:
        java.io.IOException
      • delete

        public void delete​(java.lang.String job,
                           java.util.Map<java.lang.String,​java.lang.String> groupingKey)
                    throws java.io.IOException
        Deletes metrics from the Pushgateway.

        Deletes metrics with the provided job and grouping key. This uses the DELETE HTTP method.

        Throws:
        java.io.IOException
      • push

        @Deprecated
        public void push​(CollectorRegistry registry,
                         java.lang.String job,
                         java.lang.String instance)
                  throws java.io.IOException
        Pushes all metrics in a registry, replacing all those with the same job and instance.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • push

        @Deprecated
        public void push​(Collector collector,
                         java.lang.String job,
                         java.lang.String instance)
                  throws java.io.IOException
        Pushes all metrics in a Collector, replacing all those with the same job and instance.

        This is useful for pushing a single Gauge.

        This uses the PUT HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        @Deprecated
        public void pushAdd​(CollectorRegistry registry,
                            java.lang.String job,
                            java.lang.String instance)
                     throws java.io.IOException
        Pushes all metrics in a registry, replacing only previously pushed metrics of the same name.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • pushAdd

        @Deprecated
        public void pushAdd​(Collector collector,
                            java.lang.String job,
                            java.lang.String instance)
                     throws java.io.IOException
        Pushes all metrics in a Collector, replacing only previously pushed metrics of the same name.

        This is useful for pushing a single Gauge.

        This uses the POST HTTP method.

        Throws:
        java.io.IOException
      • delete

        @Deprecated
        public void delete​(java.lang.String job,
                           java.lang.String instance)
                    throws java.io.IOException
        Deprecated.
        Deletes metrics from the Pushgateway.

        This uses the DELETE HTTP method.

        Throws:
        java.io.IOException
      • doRequest

        void doRequest​(CollectorRegistry registry,
                       java.lang.String job,
                       java.util.Map<java.lang.String,​java.lang.String> groupingKey,
                       java.lang.String method)
                throws java.io.IOException
        Throws:
        java.io.IOException
      • base64url

        private static java.lang.String base64url​(java.lang.String v)
      • instanceIPGroupingKey

        public static java.util.Map<java.lang.String,​java.lang.String> instanceIPGroupingKey()
                                                                                            throws java.net.UnknownHostException
        Returns a grouping key with the instance label set to the machine's IP address.

        This is a convenience function, and should only be used where you want to push per-instance metrics rather than cluster/job level metrics.

        Throws:
        java.net.UnknownHostException
      • readFromStream

        private static java.lang.String readFromStream​(java.io.InputStream is)
                                                throws java.io.IOException
        Throws:
        java.io.IOException