Class MetricsHttpServer

java.lang.Object
com.loomcache.server.metrics.MetricsHttpServer
All Implemented Interfaces:
AutoCloseable

public class MetricsHttpServer extends Object implements AutoCloseable
Simple HTTP server that exposes Micrometer metrics in Prometheus text exposition format or JSON.

Starts an HTTP server on a configurable port (default 9090) and serves: - /metrics — all metrics (Prometheus text format by default, JSON with ?format=json) - /metrics/raft — Raft consensus metrics only - /metrics/network — TCP network metrics only - /metrics/datastructures — data structure operation counts only

Supports basic HTTP authentication via loomcache.metrics.auth.username/password config.

Thread-safe and AutoCloseable for easy resource management.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Default bind address is loopback-only to avoid exposing metrics off-host by default.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry)
    Create a MetricsHttpServer with default port 9090.
    MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port)
    Create a MetricsHttpServer with the given MeterRegistry on the specified port.
    MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port, @Nullable String authUsername, @Nullable String authPassword)
    Create a MetricsHttpServer with the given MeterRegistry on the specified port with auth credentials.
    MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port, String bindAddress, @Nullable String authUsername, @Nullable String authPassword)
    Create a MetricsHttpServer with the given MeterRegistry on the specified port and bind address.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the server (AutoCloseable implementation).
    boolean
    Check if the server is running.
    void
    Start the HTTP server.
    void
    Stop the HTTP server.

    Methods inherited from class Object

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

    • DEFAULT_BIND_ADDRESS

      public static final String DEFAULT_BIND_ADDRESS
      Default bind address is loopback-only to avoid exposing metrics off-host by default.
      See Also:
  • Constructor Details

    • MetricsHttpServer

      public MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port, String bindAddress, @Nullable String authUsername, @Nullable String authPassword) throws IOException
      Create a MetricsHttpServer with the given MeterRegistry on the specified port and bind address.
      Parameters:
      meterRegistry - the MeterRegistry to expose metrics from (must not be null)
      port - the port to listen on (must be > 0)
      bindAddress - the address to bind to (e.g. "0.0.0.0" for all interfaces, "localhost" for loopback only)
      authUsername - optional username for HTTP Basic Auth (null to disable)
      authPassword - optional password for HTTP Basic Auth (null to disable)
      Throws:
      IOException - if the HTTP server fails to bind to the port
      NullPointerException - if meterRegistry or bindAddress is null
      IllegalArgumentException - if port is invalid
    • MetricsHttpServer

      public MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port, @Nullable String authUsername, @Nullable String authPassword) throws IOException
      Create a MetricsHttpServer with the given MeterRegistry on the specified port with auth credentials. Binds to loopback only by default.
      Parameters:
      meterRegistry - the MeterRegistry to expose metrics from (must not be null)
      port - the port to listen on (must be > 0)
      authUsername - optional username for HTTP Basic Auth (null to disable)
      authPassword - optional password for HTTP Basic Auth (null to disable)
      Throws:
      IOException - if the HTTP server fails to bind to the port
      NullPointerException - if meterRegistry is null
      IllegalArgumentException - if port is invalid
    • MetricsHttpServer

      public MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry, int port) throws IOException
      Create a MetricsHttpServer with the given MeterRegistry on the specified port. Binds to loopback only by default, no authentication.
      Parameters:
      meterRegistry - the MeterRegistry to expose metrics from (must not be null)
      port - the port to listen on
      Throws:
      IOException - if the HTTP server fails to bind to the port
      NullPointerException - if meterRegistry is null
    • MetricsHttpServer

      public MetricsHttpServer(io.micrometer.core.instrument.MeterRegistry meterRegistry) throws IOException
      Create a MetricsHttpServer with default port 9090. Binds to loopback only by default, no authentication.
      Parameters:
      meterRegistry - the MeterRegistry to expose metrics from (must not be null)
      Throws:
      IOException - if the HTTP server fails to bind to the port
      NullPointerException - if meterRegistry is null
  • Method Details

    • start

      public void start()
      Start the HTTP server.
    • stop

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

      public boolean isRunning()
      Check if the server is running.
      Returns:
      true if the server is running
    • close

      public void close()
      Close the server (AutoCloseable implementation).
      Specified by:
      close in interface AutoCloseable