health

package module
v0.0.0-...-ea4c3c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2016 License: MIT Imports: 2 Imported by: 0

README

Build Status Go Report Card GoDoc

Go Health Check

A Easy to use, extensible health check library

Table of Contents

Example

package main

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

    "github.com/dimiro1/health"
    "github.com/dimiro1/health/url"
    "github.com/dimiro1/health/db"
    _ "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)

    http.Handle("/health/", handler)
    http.ListenAndServe(":8080", nil)
}
$ curl localhost:8080/health/

If everything is ok the server must return the following json.

{
    "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"
    },
    "status": "UP"
}

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.

Instalation

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()
	health.Up() // Default is Down

	var stat syscall.Statfs_t

	wd, err := os.Getwd()

	if err != nil {
		health.Down()
		health.Info = map[string]interface{}{
			"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()
	}

	health.Info = map[string]interface{}{
		"free":      diskFreeInBytes,
		"threshold": d.Threshold,
	}

	return health
}

Implemented health check indicators

Health Description Package
url.Checker Check the connection with some URL https://github.com/dimiro1/health/tree/master/url
db.Checker Check the connection with the database https://github.com/dimiro1/health/tree/master/db

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Claudemiro

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

    http.Handle("/health/", handler)
    http.ListenAndServe(":8080", nil)
}

Executing a curl

$ curl localhost:8080/health/

If everything is ok the server must return the following json.

{
    "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"
    },
    "status": "UP"
}

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

Instalation

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()
    health.Up() // Default is Down

    var stat syscall.Statfs_t

    wd, err := os.Getwd()

    if err != nil {
        health.Down()
        health.Info = map[string]interface{}{
            "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()
    }

    health.Info = map[string]interface{}{
        "free":      diskFreeInBytes,
        "threshold": d.Threshold,
    }

    return health
}

Index

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

func NewHandler

func NewHandler() Handler

NewHandler returns a new Handler

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP returns a json encoded Health set the status to http.StatusServiceUnavailable if the check is down

type Health

type Health struct {
	Info map[string]interface{}
	// contains filtered or unexported fields
}

Health is a health status struct

func NewHealth

func NewHealth() Health

NewHealth return a new Health with status Down

func (*Health) Down

func (h *Health) Down()

Down set the status to Down

func (Health) IsDown

func (h Health) IsDown() bool

IsDown returns true if Status is Down

func (Health) IsOutOfService

func (h Health) IsOutOfService() bool

IsOutOfService returns true if Status is IsOutOfService

func (Health) IsUnknown

func (h Health) IsUnknown() bool

IsUnknown returns true if Status is Unknown

func (Health) IsUp

func (h Health) IsUp() bool

IsUp returns true if Status is Up

func (Health) MarshalJSON

func (h Health) MarshalJSON() ([]byte, error)

MarshalJSON is a custom JSON marshaller

func (*Health) OutOfService

func (h *Health) OutOfService()

OutOfService set the status to OutOfService

func (*Health) Unknown

func (h *Health) Unknown()

Unknown set the status to Unknown

func (*Health) Up

func (h *Health) Up()

Up set the status to Up

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL