README

Healthcheck

Build Status Go Report Card GoDoc codecov FOSSA Status

A simple and extensible RESTful Healthcheck API implementation for Go services.

Health provides an http.Handlefunc for use as a healthcheck endpoint used by external services or load balancers. The function is used to determine the health of the application and to remove unhealthy application hosts or containers from rotation.

Instead of blindly returning a 200 HTTP status code, a healthcheck endpoint should test all the mandatory dependencies that are essential for proper functioning of a web service.

Implementing the Checker interface and passing it on to healthcheck allows you to test the the dependencies such as a database connection, caches, files and even external services you rely on. You may choose to not fail the healthcheck on failure of certain dependencies such as external services that you are not always dependent on.

Example

package main

import (
    "context"
    "database/sql"
    "net/http"
    "time"

    "github.com/etherlabsio/healthcheck"
    "github.com/etherlabsio/healthcheck/checkers"
    _ "github.com/go-sql-driver/mysql"
    "github.com/gorilla/mux"
)

func main() {
    // For brevity, error check is being omitted here.
    db, _ := sql.Open("mysql", "user:password@/dbname")
    defer db.Close()

    r := mux.NewRouter()
    r.Handle("/healthcheck", healthcheck.Handler(

        // WithTimeout allows you to set a max overall timeout.
        healthcheck.WithTimeout(5*time.Second),

        // Checkers fail the status in case of any error.
        healthcheck.WithChecker(
            "heartbeat", checkers.Heartbeat("$PROJECT_PATH/heartbeat"),
        ),

        healthcheck.WithChecker(
            "database", healthcheck.CheckerFunc(
                func(ctx context.Context) error {
                    return db.PingContext(ctx)
                },
            ),
        ),

        // Observers do not fail the status in case of error.
        healthcheck.WithObserver(
            "diskspace", checkers.DiskSpace("/var/log", 90),
        ),
    ))

    http.ListenAndServe(":8080", r)
}

Based on the example provided above, curl localhost:8080/healthcheck | jq should yield on error a response with an HTTP statusCode of 503.

{
  "status": "Service Unavailable",
  "errors": {
    "database": "dial tcp 127.0.0.1:3306: getsockopt: connection refused",
    "heartbeat": "heartbeat not found. application should be out of rotation"
  }
}

License

This project is licensed under the terms of the MIT license. See the LICENSE file.

FOSSA Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Handler

func Handler(opts ...Option) http.Handler

    Handler returns an http.Handler

    func HandlerFunc

    func HandlerFunc(opts ...Option) http.HandlerFunc

      HandlerFunc returns an http.HandlerFunc to mount the API implementation at a specific route

      Types

      type Checker

      type Checker interface {
      	Check(ctx context.Context) error
      }

        Checker checks the status of the dependency and returns error. In case the dependency is working as expected, return nil.

        type CheckerFunc

        type CheckerFunc func(ctx context.Context) error

          CheckerFunc is a convenience type to create functions that implement the Checker interface.

          func (CheckerFunc) Check

          func (c CheckerFunc) Check(ctx context.Context) error

            Check Implements the Checker interface to allow for any func() error method to be passed as a Checker

            type Option

            type Option func(*health)

              Option adds optional parameter for the HealthcheckHandlerFunc

              func WithChecker

              func WithChecker(name string, s Checker) Option

                WithChecker adds a status checker that needs to be added as part of healthcheck. i.e database, cache or any external dependency

                func WithObserver

                func WithObserver(name string, s Checker) Option

                  WithObserver adds a status checker but it does not fail the entire status.

                  func WithTimeout

                  func WithTimeout(timeout time.Duration) Option

                    WithTimeout configures the global timeout for all individual checkers.

                    Source Files

                    Directories

                    Path Synopsis