Class HTTPServer


  • public class HTTPServer
    extends java.lang.Object
    Expose Prometheus metrics using a plain Java HttpServer.

    Example Usage:

     
     HTTPServer server = new HTTPServer(1234);
     
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.ExecutorService executorService  
      protected com.sun.net.httpserver.HttpServer server  
    • Constructor Summary

      Constructors 
      Constructor Description
      HTTPServer​(int port)
      Start a HTTP server serving the default Prometheus registry using non-daemon threads.
      HTTPServer​(int port, boolean daemon)
      Start a HTTP server serving the default Prometheus registry.
      HTTPServer​(com.sun.net.httpserver.HttpServer httpServer, CollectorRegistry registry, boolean daemon)
      Start a HTTP server serving Prometheus metrics from the given registry using the given HttpServer.
      HTTPServer​(java.lang.String host, int port)
      Start a HTTP server serving the default Prometheus registry using non-daemon threads.
      HTTPServer​(java.lang.String host, int port, boolean daemon)
      Start a HTTP server serving the default Prometheus registry.
      HTTPServer​(java.net.InetSocketAddress addr, CollectorRegistry registry)
      Start a HTTP server serving Prometheus metrics from the given registry using non-daemon threads.
      HTTPServer​(java.net.InetSocketAddress addr, CollectorRegistry registry, boolean daemon)
      Start a HTTP server serving Prometheus metrics from the given registry.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getPort()
      Gets the port number.
      protected static java.util.Set<java.lang.String> parseQuery​(java.lang.String query)  
      protected static boolean shouldUseCompression​(com.sun.net.httpserver.HttpExchange exchange)  
      private void start​(boolean daemon)
      Start a HTTP server by making sure that its background thread inherit proper daemon flag.
      void stop()
      Stop the HTTP server.
      • Methods inherited from class java.lang.Object

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

      • server

        protected final com.sun.net.httpserver.HttpServer server
      • executorService

        protected final java.util.concurrent.ExecutorService executorService
    • Constructor Detail

      • HTTPServer

        public HTTPServer​(com.sun.net.httpserver.HttpServer httpServer,
                          CollectorRegistry registry,
                          boolean daemon)
                   throws java.io.IOException
        Start a HTTP server serving Prometheus metrics from the given registry using the given HttpServer. The httpServer is expected to already be bound to an address
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(java.net.InetSocketAddress addr,
                          CollectorRegistry registry,
                          boolean daemon)
                   throws java.io.IOException
        Start a HTTP server serving Prometheus metrics from the given registry.
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(java.net.InetSocketAddress addr,
                          CollectorRegistry registry)
                   throws java.io.IOException
        Start a HTTP server serving Prometheus metrics from the given registry using non-daemon threads.
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(int port,
                          boolean daemon)
                   throws java.io.IOException
        Start a HTTP server serving the default Prometheus registry.
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(int port)
                   throws java.io.IOException
        Start a HTTP server serving the default Prometheus registry using non-daemon threads.
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(java.lang.String host,
                          int port,
                          boolean daemon)
                   throws java.io.IOException
        Start a HTTP server serving the default Prometheus registry.
        Throws:
        java.io.IOException
      • HTTPServer

        public HTTPServer​(java.lang.String host,
                          int port)
                   throws java.io.IOException
        Start a HTTP server serving the default Prometheus registry using non-daemon threads.
        Throws:
        java.io.IOException
    • Method Detail

      • shouldUseCompression

        protected static boolean shouldUseCompression​(com.sun.net.httpserver.HttpExchange exchange)
      • parseQuery

        protected static java.util.Set<java.lang.String> parseQuery​(java.lang.String query)
                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • start

        private void start​(boolean daemon)
        Start a HTTP server by making sure that its background thread inherit proper daemon flag.
      • stop

        public void stop()
        Stop the HTTP server.
      • getPort

        public int getPort()
        Gets the port number.