Class HealthCheckHandler
- java.lang.Object
-
- ratpack.core.health.HealthCheckHandler
-
- All Implemented Interfaces:
Handler
public class HealthCheckHandler extends Object implements Handler
A handler that executeshealth checks
and renders the results.The handler obtains health checks via the context's registry. Typically, health checks are added to the server registry.
import ratpack.exec.Promise; import ratpack.exec.registry.Registry; import ratpack.core.health.HealthCheck; import ratpack.core.health.HealthCheckHandler; import ratpack.test.embed.EmbeddedApp; import static org.junit.jupiter.api.Assertions.*; public class Example { static class ExampleHealthCheck implements HealthCheck { public String getName() { return "example"; // must be unique within the application } public Promise<HealthCheck.Result> check(Registry registry) throws Exception { return Promise.value(HealthCheck.Result.healthy()); } } public static void main(String... args) throws Exception { EmbeddedApp.of(s -> s .registryOf(r -> r .add(new ExampleHealthCheck()) .add(HealthCheck.of("inline", registry -> // alternative way to implement health checks Promise.value(HealthCheck.Result.unhealthy("FAILED")) )) ) .handlers(c -> c .get("health/:name?", new HealthCheckHandler()) ) ).test(httpClient -> { // Run all health checks String result = httpClient.getText("health"); String[] results = result.split("\n"); assertEquals(2, results.length); assertEquals("example : HEALTHY", results[0]); assertEquals("inline : UNHEALTHY [FAILED]", results[1]); // Run a single health check result = httpClient.getText("health/example"); assertEquals("example : HEALTHY", result); }); } }
The output format
The handler creates a
HealthCheckResults
object with the results of running the health checks andrenders
it. Ratpack provides a default renderer forHealthCheckResults
objects, that renders results as plain text one per line with the format:name : HEALTHY|UNHEALTHY [message] [exception]
If any result is unhealthy, a
503
status will be emitted, else200
.To change the output format, simply add your own renderer for this type to the registry.
Concurrency
The concurrency of the health check execution is managed by a
Throttle
. By default, anunlimited throttle
is used. A sized throttle can be explicitly given to the constructor.Rendering single health checks
The handler checks for the presence of a
path token
to indicate the name of an individual check to execute. By default, the token is named "name". If a path token is present, but indicates the name of a non-existent health check, a404
client error
will be raised.- See Also:
HealthCheck
,HealthCheckResults
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_NAME_TOKEN
The default path token name that indicates the individual health check to run.
-
Constructor Summary
Constructors Modifier Constructor Description HealthCheckHandler()
Uses the default values ofDEFAULT_NAME_TOKEN
and an unlimited throttle.HealthCheckHandler(String pathTokenName)
Uses an unlimited throttle and the given name for the health check identifying path token.protected
HealthCheckHandler(String pathTokenName, Throttle throttle)
Constructor.HealthCheckHandler(Throttle throttle)
Uses theDEFAULT_NAME_TOKEN
and the given throttle.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getName()
The name of the path token that may indicate a particular health check to execute.Throttle
getThrottle()
The throttle for executing health checks.void
handle(Context ctx)
Renders health checks.
-
-
-
Field Detail
-
DEFAULT_NAME_TOKEN
public static final String DEFAULT_NAME_TOKEN
The default path token name that indicates the individual health check to run. Value: "name"
-
-
Constructor Detail
-
HealthCheckHandler
public HealthCheckHandler()
Uses the default values ofDEFAULT_NAME_TOKEN
and an unlimited throttle.- See Also:
HealthCheckHandler(String, Throttle)
-
HealthCheckHandler
public HealthCheckHandler(String pathTokenName)
Uses an unlimited throttle and the given name for the health check identifying path token.- Parameters:
pathTokenName
- health check name- See Also:
HealthCheckHandler(String, Throttle)
-
HealthCheckHandler
public HealthCheckHandler(Throttle throttle)
Uses theDEFAULT_NAME_TOKEN
and the given throttle.- Parameters:
throttle
- the throttle for health check execution- See Also:
HealthCheckHandler(String, Throttle)
-
HealthCheckHandler
protected HealthCheckHandler(String pathTokenName, Throttle throttle)
Constructor.The path token name parameter specifies the name of the path token that, if present, indicates the single health check to execute. If this path token is not present, all checks will be run.
The throttle controls the concurrency of health check execution. The actual parallelism of the health check executions is ultimately determined by the size of the application event loop in conjunction with the throttle size. Generally, an
unlimited throttle
is appropriate unless there are many health checks that are executed frequently as this may degrade the performance of other requests. To serialize health check execution, use a throttle of size 1.- Parameters:
pathTokenName
- the name of health checkthrottle
- the throttle for health check execution
-
-
Method Detail
-
getThrottle
public Throttle getThrottle()
The throttle for executing health checks.- Returns:
- the throttle for executing health checks.
-
getName
public String getName()
The name of the path token that may indicate a particular health check to execute.- Returns:
- the name of the path token that may indicate a particular health check to execute
-
-