healthz

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

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

Go to latest
Published: May 10, 2018 License: MIT Imports: 5 Imported by: 1

README

Healthz

Build Status Codecov branch GoDoc Go Report Card License

Create healthcheck for your apps

Instalation

Go Health requires Go 1.10 or later.

go get github.com/dafiti/healthz

If you want to get an specific version, please use the example below:

go get gopkg.in/dafiti/healthz.v0

Usage

Simple usage
package main

import (
	"fmt"
	"github.com/dafiti/healthz"
	"time"
)

func main() {
	checkers := []healthz.Checker{
		func() *healthz.Check {
			return &healthz.Check{
				Name:         "some-api",
				Kind:         healthz.InternalService,
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				URL:          "http://api.example.com",
			}
		},
	}

	h := healthz.New("my-app", "0.0.1", checkers...)

	fmt.Println(h.Check())
}
Simple HTTP
package main

import (
	"github.com/dafiti/healthz"
	"net/http"
	"time"
)

func main() {
	checkers := []healthz.Checker{
		func() *healthz.Check {
			return &healthz.Check{
				Name:         "some-api",
				Kind:         healthz.InternalService,
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				URL:          "http://api.example.com",
				Details: map[string]string{
					"api-key": "bf15a8f489e8f3d4bf6f77343c08ad1e",
				},
			}
		},
	}

	h := healthz.New("my-app", "0.0.1", checkers...)

	http.Handle("/healthz", h)

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

Available checkers

SQL
package main

import (
    "database/sql"
    "github.com/dafiti/healthz"
    "github.com/dafiti/healthz/checker"
    _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, _ := sql.Open("mysql", "user:pass@tcp(127.0.0.1:3306)/hello")

    checkers := []healthz.Checker{
        checker.Sql(db, true),
    }
}
Redis
package main

import (
    "github.com/dafiti/healthz"
    "github.com/dafiti/healthz/checker"
    "github.com/go-redis/redis"
)

func main() {
    client := redis.NewClient(&redis.Options{
        Addr: "localhost:6379",
    })

    checkers := []healthz.Checker{
        checker.Redis(client, true),
    }
}

Development

Requirements
Makefile
// Clean up
$ make clean

// Creates folders and download dependencies
$ make configure

//Run tests and generates html coverage file
make cover

// Download project dependencies
make depend

// Format all go files
make fmt

//Run linters
make lint

// Run tests
make test

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Overview

Package healthz retrieves the healh of your app dependencies.

Example Usage

The following is a simple example using the library:

package main

import (
	"fmt"
	"github.com/dafiti/healthz"
	"time"
)

func main() {
	checkers := []healthz.Checker{
		func() *healthz.Check {
			return &healthz.Check{
				Name:         "some-api",
				Kind:         healthz.InternalService,
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				URL:          "http://api.example.com",
			}
		},
	}

	h := healthz.New("my-app", "0.0.1", checkers...)

	fmt.Println(h.Check())
}

Using with a HTTP Server:

package main

import (
	"github.com/dafiti/healthz"
	"net/http"
	"time"
)

func main() {
	checkers := []healthz.Checker{
		func() *healthz.Check {
			return &healthz.Check{
				Name:         "some-api",
				Kind:         healthz.InternalService,
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				URL:          "http://api.example.com",
				Details: map[string]string{
					"api-key": "bf15a8f489e8f3d4bf6f77343c08ad1e",
				},
			}
		},
	}

	h := healthz.New("my-app", "0.0.1", checkers...)

	http.Handle("/healthz", h)

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check struct {
	Name         string            `json:"name"`
	Kind         Kind              `json:"kind"`
	Status       bool              `json:"status"`
	ResponseTime time.Duration     `json:"response_time"`
	Optional     bool              `json:"optional"`
	URL          string            `json:"url"`
	Details      map[string]string `json:"details"`
}

Check retrieves the health of service

type Checker

type Checker func() *Check

Checker it's responsible for checking health

type Healthz

type Healthz struct {
	Name    string
	Version string
	// contains filtered or unexported fields
}

Healthz defines the healthcheck configuration

func New

func New(name string, version string, checkers ...Checker) *Healthz

New retrieves the instance of Healthz

func (*Healthz) Check

func (h *Healthz) Check() *Status

Check checks the status of services

func (*Healthz) ServeHTTP

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

ServerHTTP retrieves the health status

type Kind

type Kind uint8

Kind type

const (
	// Database type
	Database Kind = iota + 1

	// Messaging type
	Messaging

	// InternalService type
	InternalService

	// TimeFormat used for parsing and formating time
	TimeFormat = "2006-01-02 15:04:05"
)

func (Kind) MarshalJSON

func (k Kind) MarshalJSON() ([]byte, error)

MarshalJSON marshals Kind instance into string

type Status

type Status struct {
	Name    string   `json:"name"`
	Version string   `json:"version"`
	Status  bool     `json:"status"`
	Date    Time     `json:"date"`
	Checks  []*Check `json:"checks"`
}

Status retrieves the status of health

type Time

type Time struct {
	time.Time
}

Time it is a custom time struct for json parsing

func (*Time) MarshalJSON

func (t *Time) MarshalJSON() ([]byte, error)

MarshalJSON marshals Time instance into string

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(value []byte) (err error)

UnmarshalJSON unmarshals string time into Time instance

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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