Skip to content

A simple, easily extensible and concurrent health-check library for Go services

License

Notifications You must be signed in to change notification settings

hoshsadiq/go-healthcheck

 
 

Repository files navigation

Healthcheck

Build Status Go Report Card GoDoc codecov FOSSA Status

Note: This is a fork of etherlabsio/healthcheck with some changes that allow the health checkers to be easily used outside of HTTP servers, but also, in HTTP servers the error responses can be customised. This fork is not compatible with the original code.

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

Without HTTP servers

package main

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

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

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

	svc := healthcheck.NewService(

		// 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),
		),
	)

	errorCode, errorMessages := svc.CheckHealth(context.Background())
	fmt.Println(errorCode)
	fmt.Println(errorMessages)

	// this can also with a HTTP server
	r := mux.NewRouter()
	r.handle("/healthcheck", svc.Handler())
	// alternatively you can customise the error message
	r.handle("/healthcheck", func(w http.ResponseWriter, r *http.Request) {
		errorCode, errorMessages := svc.CheckHealth(context.Background())
		// do something with errorCode and errorMessages
		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		w.WriteHeader(errorCode)
		json.NewEncoder(w).Encode(errorMessages)
	})
	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.

About

A simple, easily extensible and concurrent health-check library for Go services

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%