Class HTTPServer

java.lang.Object
io.prometheus.client.exporter.HTTPServer
All Implemented Interfaces:
Closeable, AutoCloseable

public class HTTPServer extends Object implements Closeable
Expose Prometheus metrics using a plain Java HttpServer.

Example Usage:


HTTPServer server = new HTTPServer(1234);

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    We keep the original constructors of HTTPServer for compatibility, but new configuration parameters like sampleNameFilter must be configured using the Builder.
    static class 
    Handles Metrics collections from the given registry.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final ExecutorService
     
    protected final com.sun.net.httpserver.HttpServer
     
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    Stop the HTTPServer.
    int
    Gets the port number.
    protected static Set<String>
     
    protected static boolean
    shouldUseCompression(com.sun.net.httpserver.HttpExchange exchange)
     
    void
    Deprecated.
    renamed to close(), so that the HTTPServer can be used in try-with-resources.

    Methods inherited from class Object

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

    • server

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

      protected final ExecutorService executorService
  • Constructor Details

    • HTTPServer

      public HTTPServer(com.sun.net.httpserver.HttpServer httpServer, CollectorRegistry registry, boolean daemon) throws IOException
      Start an 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:
      IOException
    • HTTPServer

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

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

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

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

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

      public HTTPServer(String host, int port) throws IOException
      Start an HTTP server serving the default Prometheus registry using non-daemon threads.
      Throws:
      IOException
  • Method Details

    • shouldUseCompression

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

      protected static Set<String> parseQuery(String query) throws IOException
      Throws:
      IOException
    • stop

      public void stop()
      Deprecated.
      renamed to close(), so that the HTTPServer can be used in try-with-resources.
      Stop the HTTP server.
    • close

      public void close()
      Stop the HTTPServer.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getPort

      public int getPort()
      Gets the port number.