Class HealthCheckResult

java.lang.Object
com.tnsai.quality.observability.health.HealthCheckResult

public final class HealthCheckResult extends Object
Result of a health check operation.

Contains the health status, optional message, and additional details.

Example usage:


 // Simple healthy result
 HealthCheckResult healthy = HealthCheckResult.up();

 // Healthy with details
 HealthCheckResult healthyWithDetails = HealthCheckResult.up()
     .withDetail("connections", 10)
     .withDetail("latency_ms", 50);

 // Unhealthy result
 HealthCheckResult unhealthy = HealthCheckResult.down("Connection refused")
     .withDetail("host", "localhost:5432");

 // From exception
 try {
     checkConnection();
     return HealthCheckResult.up();
 } catch (Exception e) {
     return HealthCheckResult.down(e);
 }
 
  • Method Details

    • up

      public static HealthCheckResult up()
      Creates a healthy (UP) result.
      Returns:
      a healthy result
    • up

      public static HealthCheckResult up(String message)
      Creates a healthy (UP) result with a message.
      Parameters:
      message - the status message
      Returns:
      a healthy result
    • down

      public static HealthCheckResult down()
      Creates an unhealthy (DOWN) result.
      Returns:
      an unhealthy result
    • down

      public static HealthCheckResult down(String message)
      Creates an unhealthy (DOWN) result with a message.
      Parameters:
      message - the error message
      Returns:
      an unhealthy result
    • down

      public static HealthCheckResult down(Throwable exception)
      Creates an unhealthy (DOWN) result from an exception.
      Parameters:
      exception - the exception that caused the failure
      Returns:
      an unhealthy result
    • degraded

      public static HealthCheckResult degraded(String message)
      Creates a degraded result.
      Parameters:
      message - the status message
      Returns:
      a degraded result
    • unknown

      public static HealthCheckResult unknown()
      Creates an unknown status result.
      Returns:
      an unknown status result
    • unknown

      public static HealthCheckResult unknown(String message)
      Creates an unknown status result with a message.
      Parameters:
      message - the status message
      Returns:
      an unknown status result
    • of

      public static HealthCheckResult of(HealthStatus status, String message)
      Creates a result with a specific status.
      Parameters:
      status - the health status
      message - the status message
      Returns:
      a health check result
    • withDetail

      public HealthCheckResult withDetail(String key, Object value)
      Adds a detail to the result.
      Parameters:
      key - the detail key
      value - the detail value
      Returns:
      a new result with the detail added
    • withDetails

      public HealthCheckResult withDetails(Map<String,Object> details)
      Adds multiple details to the result.
      Parameters:
      details - the details to add
      Returns:
      a new result with the details added
    • withDuration

      public HealthCheckResult withDuration(Duration duration)
      Sets the check duration.
      Parameters:
      duration - the duration of the health check
      Returns:
      a new result with the duration set
    • getStatus

      public HealthStatus getStatus()
      Gets the health status.
      Returns:
      the status
    • getMessage

      public String getMessage()
      Gets the status message.
      Returns:
      the message, or null if not set
    • getDetails

      public Map<String,Object> getDetails()
      Gets the additional details.
      Returns:
      unmodifiable map of details
    • getDetail

      public Object getDetail(String key)
      Gets a specific detail value.
      Parameters:
      key - the detail key
      Returns:
      the value, or null if not present
    • getTimestamp

      public Instant getTimestamp()
      Gets the timestamp when this result was created.
      Returns:
      the timestamp
    • getDuration

      public Duration getDuration()
      Gets the duration of the health check.
      Returns:
      the duration, or null if not measured
    • isHealthy

      public boolean isHealthy()
      Checks if the component is healthy (UP or DEGRADED).
      Returns:
      true if operational
    • combine

      public HealthCheckResult combine(HealthCheckResult other)
      Combines this result with another, returning the worse status.
      Parameters:
      other - the other result
      Returns:
      a combined result with the worse status
    • toMap

      public Map<String,Object> toMap()
      Converts this result to a map suitable for JSON serialization.
      Returns:
      map representation
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object