Add Health Monitoring to a Plan

Habitat provides basic health monitoring for your running service; however, if you want to perform custom tests or other status checks, you can customize your plan to do that. This is done by using a health_check hook, which is a script that responds to a specific event during the lifecycle of a running service.

There are multiple hooks that you can define in your plan. For example, the init and run hooks are responsible for defining behavior during initialization and when the child service/application starts up. The Ruby scaffolding creates the initialization for you to handle connecting to a backend database and the build script used by the Studio creates a generic run hook. However, to create the health_check hook, you have to perform the following operations:

  1. Change directory to hooks.

    $ cd hooks
  2. Create a new file named health_check.

    $ touch health_check
  3. Open health_check in your text editor.

  4. Hooks use shebangs to decide which interpreter to use when running their scripts. In the case of the hook in our plan, we will use the system shell, so add the following to the health_check hook file:

    ~/sample-rails-app/habitat/hooks/health_check
    #!/bin/sh
    #
    # default return code is 0
    rc=0
    # This request will test whether our index page can be retrieved.
    {{pkgPathFor "core/curl"}}/bin/curl -sS --head --fail --max-time 1 http://localhost:{{cfg.app.port}}/
    case $? in
    # Zero exit status means curl got back a 200 end everything is ok.
    0)
    rc=0 ;;
    # Anything else is critical.
    *)
    rc=2 ;;
    esac
    exit $rc

    This hook performs a basic test to check whether the results of the curl command return an HTTP status code less than 400. If the curl command doesn't succeed, then this check will be denoted as "critical."

  5. Save the health_check hook file.

Congratulations, you have added a custom health_check application lifecycle hook to your sample application. It's time to build your sample Rails application.

Next: Build the Package