Class Health

java.lang.Object
com.jsgalactic.axiom.observability.Health

public final class Health extends Object
Liveness and readiness for orchestrators and load balancers.

Liveness answers "should this process be restarted?". With no liveness checks it is up whenever the process can answer. Readiness answers "should this instance receive traffic?". It is down while the application is not running, once beginDrain() was called, and while any readiness check is unhealthy. A prober should stop sending traffic on a failing readiness probe and restart only on a failing liveness probe.


 var health = Health.builder(app)
         .readiness("database", () -> pool.isValid())
         .build();
 health.register(app, security.hasRole("ops"));   // GET /health/live and GET /health/ready
 // on SIGTERM: health.beginDrain(); wait for the balancer to notice; app.close();
 

Exposure. The routes answer without authentication unless the middleware passed to register(com.jsgalactic.axiom.routing.RouteGroup, com.jsgalactic.axiom.context.Middleware...) requires it. Check names and states are operational detail: do not expose them to the public internet without security middleware or a private listener.

Startup. The startup probe answers "has this instance finished starting?". It is DOWN until the application is running and every startup check passes, then UP for good: once it has succeeded no startup check runs again, so an expensive warm-up check costs nothing afterwards. An orchestrator holds back its liveness and readiness probes until it succeeds.

Draining. Building a Health registers beginDrain() with the application (see Application.onDrain(java.lang.Runnable)), so a JVM shutdown hook opted into with Application.closeOnJvmShutdown(Duration) turns readiness DOWN first, waits the drain delay for the balancer to notice, and only then closes the application and its listeners.

Caching. Health.Builder.cacheFor(java.time.Duration) keeps each check's result for a short time, so a frequently polled expensive check runs at most once per interval however many probes arrive.

An instance is immutable apart from the drain flag, the startup latch and the result caches, and is thread-safe.

  • Field Details

    • LIVE_PATH

      public static final String LIVE_PATH
      Path of the liveness route.
      See Also:
    • READY_PATH

      public static final String READY_PATH
      Path of the readiness route.
      See Also:
    • STARTUP_PATH

      public static final String STARTUP_PATH
      Path of the startup route.
      See Also:
    • DEFAULT_TIMEOUT

      public static final Duration DEFAULT_TIMEOUT
      Probe timeout when none is configured.
    • MAX_TIMEOUT

      public static final Duration MAX_TIMEOUT
      Longest probe timeout.
    • MAX_CHECKS

      public static final int MAX_CHECKS
      Most checks per probe.
      See Also:
    • MAX_CACHE

      public static final Duration MAX_CACHE
      Longest time a check result may be cached.
  • Method Details

    • builder

      public static Health.Builder builder(Application application)
      Starts building health for an application.
      Parameters:
      application - the application whose lifecycle readiness follows
      Returns:
      a builder
    • startup

      public Health.Report startup()
      Evaluates the startup probe: UP once the application is running and every startup check passes, and UP from then on without running the checks again. While the application is still being configured, or has closed before it ever started, the probe is DOWN without running them.
      Returns:
      the report; its checks are those of the last evaluation that ran them
    • beginDrain

      public void beginDrain()
      Makes readiness report DOWN from now on, without running readiness checks. Call it when shutdown begins, before closing the application or listener, so a balancer stops sending traffic while running requests finish. It cannot be undone and is idempotent.
    • isDraining

      public boolean isDraining()
      Reports whether beginDrain() was called.
      Returns:
      true once draining
    • liveness

      public Health.Report liveness()
      Runs the liveness checks.
      Returns:
      UP when every check is up, or when there are none
    • readiness

      public Health.Report readiness()
      Evaluates readiness: DOWN without running checks when draining or when the application is not running, otherwise the readiness checks.
      Returns:
      the report
    • register

      public void register(RouteGroup routes, Middleware... middleware)
      Registers GET /health/startup, GET /health/live and GET /health/ready on an application or group (under its prefix). Each answers a JSON report with Cache-Control: no-store: 200 when up, 503 when down. HEAD works as for any GET route.
      Parameters:
      routes - where to register
      middleware - middleware applied to both routes, outermost first; pass authentication here
      Throws:
      IllegalStateException - if configuration has ended