Documentation ¶
Overview ¶
Package health is a easy to use, extensible health check library.
Example
package main import ( "net/http" "database/sql" "github.com/dimiro1/health" "github.com/dimiro1/health/url" "github.com/dimiro1/health/db" "github.com/dimiro1/health/redis" _ "github.com/go-sql-driver/mysql" ) func main() { database, _ := sql.Open("mysql", "/") mysql := db.NewMySQLChecker(database) companies := health.NewCompositeChecker() companies.AddChecker("Microsoft", url.NewChecker("https://www.microsoft.com/")) companies.AddChecker("Oracle", url.NewChecker("https://www.oracle.com/")) companies.AddChecker("Google", url.NewChecker("https://www.google.com/")) handler := health.NewHandler() handler.AddChecker("Go", url.NewChecker("https://golang.org/")) handler.AddChecker("Big Companies", companies) handler.AddChecker("MySQL", mysql) handler.AddChecker("Redis", redis.NewChecker("tcp", ":6379")) http.Handle("/health/", handler) http.ListenAndServe(":8080", nil) }
Executing a curl
$ curl localhost:8080/health/
If everything is ok the server must respond with HTTP Status 200 OK and have following json in the body.
{ "Big Companies": { "Google": { "code": 200, "status": "UP" }, "Microsoft": { "code": 200, "status": "UP" }, "Oracle": { "code": 200, "status": "UP" }, "status": "UP" }, "Go": { "code": 200, "status": "UP" }, "MySQL": { "status": "UP", "version": "10.1.9-MariaDB" }, "Redis": { "status": "UP", "version": "3.0.5" }, "status": "UP" }
The server responds with HTTP Status 503 Service Unavailable if the ckeck is Down and the json response could be something like this.
{ "Big Companies": { "Google": { "code": 200, "status": "UP" }, "Microsoft": { "code": 200, "status": "UP" }, "Oracle": { "code": 200, "status": "UP" }, "status": "UP" }, "Go": { "code": 200, "status": "UP" }, "MySQL": { "status": "DOWN", "error": "Error 1044: Access denied for user ''@'localhost' to database 'invalid-database'", }, "status": "DOWN" }
Motivation ¶
It is very important to verify the status of your system, not only the system itself, but all its dependencies, If your system is not Up you can easily know what is the cause of the problem only looking the health check.
Also it serves as a kind of basic itegration test between the systems.
Inspiration ¶
I took a lot of ideas from the spring framework (http://spring.io/).
Installation ¶
This package is a go getable packake.
$ go get github.com/dimiro1/health
API ¶
The API is stable and I do not have any plans to break compatibility, but I recommend you to vendor this dependency in your project, as it is a good practice.
Testing ¶
You have to install the test dependencies.
$ go get gopkg.in/DATA-DOG/go-sqlmock.v1
or you can go get this package with the -t flag
$ go get -t github.com/dimiro1/health
Implementing custom checkers ¶
The key interface is health.Checker, you only have to implement a type that satisfies that interface.
type Checker interface { Check() Health }
Here an example of Disk Space usage (unix only).
package main import ( "syscall" "os" ) type DiskSpaceChecker struct { Dir string Threshold uint64 } func NewDiskSpaceChecker(dir string, threshold uint64) DiskSpaceChecker { return DiskSpaceChecker{Dir: dir, Threshold: threshold} } func (d DiskSpaceChecker) Check() health.Health { health := health.NewHealth() var stat syscall.Statfs_t wd, err := os.Getwd() if err != nil { health.Down().AddInfo("error", err.Error()) // Why the check is Down return health } syscall.Statfs(wd, &stat) diskFreeInBytes := stat.Bavail * uint64(stat.Bsize) if diskFreeInBytes < d.Threshold { health.Down() } else { health.Up() } health. AddInfo("free", diskFreeInBytes). AddInfo("threshold", d.Threshold) return health }
Important ¶
The **status** key in the json have priority over a "status" key added by a Checker, so if some checker add a "status" key to the json, it will not be rendered
Index ¶
- type Checker
- type CheckerFunc
- type CompositeChecker
- type Handler
- type Health
- func (h *Health) AddInfo(key string, value interface{}) *Health
- func (h *Health) Down() *Health
- func (h Health) GetInfo(key string) interface{}
- func (h Health) IsDown() bool
- func (h Health) IsOutOfService() bool
- func (h Health) IsUnknown() bool
- func (h Health) IsUp() bool
- func (h Health) MarshalJSON() ([]byte, error)
- func (h *Health) OutOfService() *Health
- func (h *Health) Unknown() *Health
- func (h *Health) Up() *Health
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Checker ¶
type Checker interface {
Check() Health
}
Checker is a interface used to provide an indication of application health.
type CheckerFunc ¶
type CheckerFunc func() Health
CheckerFunc is an adapter to allow the use of ordinary go functions as Checkers.
func (CheckerFunc) Check ¶
func (f CheckerFunc) Check() Health
type CompositeChecker ¶
type CompositeChecker struct {
// contains filtered or unexported fields
}
CompositeChecker aggregate a list of Checkers
func NewCompositeChecker ¶
func NewCompositeChecker() CompositeChecker
NewCompositeChecker creates a new CompositeChecker
func (*CompositeChecker) AddChecker ¶
func (c *CompositeChecker) AddChecker(name string, checker Checker)
AddChecker add a Checker to the aggregator
func (*CompositeChecker) AddInfo ¶
func (c *CompositeChecker) AddInfo(key string, value interface{}) *CompositeChecker
AddInfo adds a info value to the Info map
func (CompositeChecker) Check ¶
func (c CompositeChecker) Check() Health
Check returns the combination of all checkers added if some check is not up, the combined is marked as down
type Handler ¶
type Handler struct {
CompositeChecker
}
Handler is a HTTP Server Handler implementation
type Health ¶
type Health struct {
// contains filtered or unexported fields
}
Health is a health status struct
func (Health) IsOutOfService ¶
IsOutOfService returns true if Status is IsOutOfService
func (Health) MarshalJSON ¶
MarshalJSON is a custom JSON marshaller
func (*Health) OutOfService ¶
OutOfService set the status to OutOfService