Class HealthRegistry

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

public final class HealthRegistry extends Object
Central registry for health indicators with aggregation support.

Provides a unified view of system health by aggregating multiple health indicators and supporting both synchronous and asynchronous checks.

Example usage:


 HealthRegistry registry = HealthRegistry.getInstance();

 // Register health indicators
 registry.register(new AgentHealthIndicator(agent));
 registry.register(new LLMHealthIndicator(llmClient));
 registry.register(HealthIndicator.of("custom", () -> true));

 // Check overall health
 HealthCheckResult overall = registry.checkAll();

 // Check specific indicator
 Optional<HealthCheckResult> agentHealth = registry.check("agent");

 // Get health summary as map (for HTTP endpoint)
 Map<String, Object> summary = registry.getHealthSummary();
 

Thread-safety: This class is thread-safe. Health checks can be performed concurrently from multiple threads.

See Also:
  • Constructor Details

    • HealthRegistry

      public HealthRegistry(Duration defaultTimeout)
      Creates a new health registry with a custom timeout.
      Parameters:
      defaultTimeout - default timeout for health checks
  • Method Details

    • getInstance

      public static HealthRegistry getInstance()
      Gets the singleton instance of the health registry.
      Returns:
      the health registry instance
    • register

      public HealthRegistry register(HealthIndicator indicator)
      Registers a health indicator.
      Parameters:
      indicator - the indicator to register
      Returns:
      this registry for chaining
    • register

      public HealthRegistry register(String name, HealthIndicator indicator)
      Registers a health indicator with a custom name.
      Parameters:
      name - the indicator name
      indicator - the indicator
      Returns:
      this registry for chaining
    • unregister

      public boolean unregister(String name)
      Unregisters a health indicator.
      Parameters:
      name - the indicator name
      Returns:
      true if the indicator was removed
    • getIndicatorNames

      public Set<String> getIndicatorNames()
      Gets all registered indicator names.
      Returns:
      set of indicator names
    • getIndicatorCount

      public int getIndicatorCount()
      Gets the number of registered indicators.
      Returns:
      the count
    • check

      public Optional<HealthCheckResult> check(String name)
      Checks the health of a specific indicator.
      Parameters:
      name - the indicator name
      Returns:
      the result, or empty if indicator not found
    • checkAll

      public HealthCheckResult checkAll()
      Checks all registered health indicators.
      Returns:
      aggregated health check result
    • checkAll

      public HealthCheckResult checkAll(Duration timeout)
      Checks all registered health indicators with a timeout.
      Parameters:
      timeout - the maximum time to wait for all checks
      Returns:
      aggregated health check result
    • checkAllAsync

      public CompletableFuture<HealthCheckResult> checkAllAsync()
      Checks all health indicators asynchronously.
      Returns:
      a future containing the aggregated result
    • checkMatching

      public HealthCheckResult checkMatching(String pattern)
      Checks health of indicators matching a pattern.
      Parameters:
      pattern - the name pattern (supports * wildcard)
      Returns:
      aggregated result for matching indicators
    • getHealthSummary

      public Map<String,Object> getHealthSummary()
      Gets a summary of all health checks as a map.

      Suitable for JSON serialization and HTTP health endpoints.

      Returns:
      health summary map
    • getQuickStatus

      public HealthStatus getQuickStatus()
      Gets a quick health status without details.
      Returns:
      the overall health status
    • clear

      public void clear()
      Clears all registered health indicators.
    • shutdown

      public void shutdown()
      Shuts down the health check executor.

      Should be called when the application is shutting down.