Git Product home page Git Product logo

isabella232 / dropwizard-health Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dropwizard/dropwizard-health

0.0 0.0 0.0 229 KB

Provides a health check implementation that performs ongoing monitoring of an application's dependencies and includes an endpoint that can be called by a load balancer to determine if the application is healthy and thus able to receive traffic.

License: Apache License 2.0

Java 100.00%

dropwizard-health's Introduction

dropwizard-health

Build Quality Gate Status Maven Central

Provides a health check implementation that performs ongoing monitoring of an application's dependencies and includes an endpoint that can be called by a load balancer to determine if the application is healthy and thus able to receive traffic.

Dropwizard Version Support Matrix

dropwizard-health Dropwizard v1.3.x Dropwizard v2.0.x
v1.3.x
v1.4.x
v1.5.x
v1.6.x

Usage

Add dependency on library.

<dependency>
  <groupId>io.dropwizard.modules</groupId>
  <artifactId>dropwizard-health</artifactId>
  <version>${dropwizard-health.version}</version>
</dependency>

Health Checks

In your application's Configuration class, add a HealthConfiguration object:

public class ExampleConfiguration extends Configuration {
    ...
     
    @Valid
    @NotNull
    @JsonProperty("health")
    private HealthConfiguration healthConfiguration = new HealthConfiguration();
    
    public HealthConfiguration getHealthConfiguration() {
        return healthConfiguration;
    }

    public void setHealthConfiguration(final HealthConfiguration healthConfiguration) {
        this.healthConfiguration = healthConfiguration;
    }
}

Add a HealthCheckBundle to the Bootstrap object in your initialize method:

bootstrap.addBundle(new HealthCheckBundle<ExampleConfiguration>() {
    @Override
    protected HealthConfiguration getHealthConfiguration(final ExampleConfiguration configuration) {
        return configuration.getHealthConfiguration();
    }
});

Configure health checks for any dependencies your application has, such as any databases, caches, queues, etc. Dropwizard modules that support connecting to a dependency will often register a health check automatically.

Define the following health check configurations in your config.yml file:

health:
  delayedShutdownHandlerEnabled: true
  shutdownWaitPeriod: 30s
  healthChecks:
    - name: UserDatabase
      critical: true
    - name: UserNotificationTopic
      critical: false
      schedule:
        checkInterval: 2500ms
        downtimeInterval: 10s
        failureAttempts: 2
        successAttempts: 1
    - name: UserCache
      critical: false

Configuration Reference

Health Configuration

Name Default Description
delayedShutdownHandlerEnabled true Flag indicating whether to delay shutdown to allow already processing requests to complete.
shutdownWaitPeriod 15 seconds Amount of time to delay shutdown by to allow already processing requests to complete. Only applicable if delayedShutdownHandlerEnabled is true.
healthCheckUrlPaths ["/health-check"] URLs to expose the app's health check on.
healthChecks [] A list of configured health checks. See the Health Check Configuration section for more details.
servlet default health servlet The health servlet that is used to generate health check responses. See the Default Health Servlet section for more details.

Health Check Configuration

Name Default Description
name (none) The name of this health check. This must be unique.
critical false Flag indicating whether this dependency is critical to determine the health of the application. If true and this dependency is unhealthy, the application will also be marked as unhealthy.
schedule (none) The schedule that this health check will be run on. See the Schedule section for more details.

Schedule

Name Default Description
checkInterval 5 seconds The interval on which to perform a health check for this dependency while the dependency is in a healthy state.
downtimeInterval 30 seconds The interval on which to perform a health check for this dependency while the dependency is in an unhealthy state.
failureAttempts 3 The threshold of consecutive failed attempts needed to mark a dependency as unhealthy (from a healthy state).
successAttempts 2 The threshold of consecutive successful attempts needed to mark a dependency as healthy (from an unhealthy state).

Default Health Servlet

Name Default Description
cacheControlEnabled true Flag controlling whether a Cache-Control header will be included in the health check response or not. Set header value using cacheControlValue.
cacheControlValue "no-store" The value to be set in the Cache-Control header in the health check response. Only used if cacheControlEnabled is set to true.
contentType application/json The value of the Content-Type header in the health check response.
healthyValue {"status":"healthy"} The value of the body of the health check response when the application is healthy.
unhealthyValue {"status":"unhealthy"} The value of the body of the health check response when the application is unhealthy.

Example

Healthy

$ curl -v https://<hostname>:<port>/health-check
> GET /health-check HTTP/1.1
...
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Cache-Control: no-store
...
<
{"status": "healthy"}

Not Healthy

$ curl -v https://<hostname>:<port>/health-check
> GET /health-check HTTP/1.1
...
>
< HTTP/1.1 503 Service Unavailable
< Content-Type: application/json
< Cache-Control: no-store
...
<
{"status": "unhealthy"}

HTTP and TCP Health Checks

Should your service have any dependencies that it needs to perform health checks against that expose either an HTTP or TCP health check interface, you can use the HttpHealthCheck or TcpHealthCheck classes to do so easily.

Usage

You will need to register your health check(s) in your Application class run() method.

HTTP

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    environment.healthChecks().register("http-service-dependency", new HttpHealthCheck("http://some-http-dependency.com:8080/health-check"));
}

TCP

@Override
public void run(final AppConfiguration configuration, final Environment environment) {
    ...
    environment.healthChecks().register("tcp-service-dependency", new TcpHealthCheck("some-tcp-dependency.com", 443));
}

Composite Health Checks

You might find you need a health check that is a composite of more than one other health check. For instance, consider the case where you have a database and a cache, and if only one of those two are unhealthy, your service can still fulfill a subset of functionality, and thus should not necessarily be marked down.

Let's say that you have a database health check registered under the name UserDatabase and a cache health check under the name UserCache. Below, see an example of how you might create a health check that is a composite of these two checks. Note that there does not exist a CompositeHealthCheck class currently, but it might be a nice addition to this library.

Example Composite Health Check

private void registerCompositeHealthCheck(final HealthCheckRegistry healthChecks) {
    final HealthCheck databaseCheck = healthChecks.getHealthCheck("UserDatabase");
    final HealthCheck cacheCheck = healthChecks.getHealthCheck("UserCache");

    final UserHealthCheck userHealthCheck = new UserHealthCheck(databaseCheck, cacheCheck);
    healthChecks.register("UserComposite", userHealthCheck);
}

Example Composite Health Check YAML

health:
  delayedShutdownHandlerEnabled: true
  shutdownWaitPeriod: 30s
  healthChecks:
    - name: UserComposite
      critical: true

Future improvements:

  • Hooks for health status change events.
  • Better support for Cloud-native liveness and readiness checks.
  • The ability to expose health check data to other modules (for instance, an Admin page may want to show historical health check results).
  • More out-of-the-box generally useful health checks implementations, like file-system health checks.

dropwizard-health's People

Contributors

dependabot-preview[bot] avatar pstackle avatar joschi avatar dependabot[bot] avatar natalie-zamani avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.