htz

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

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

Go to latest
Published: May 8, 2019 License: MIT Imports: 4 Imported by: 0

README

Htz

Build Status Codecov branch GoDoc Go Report Card License

Go healthcheck for your apps

Instalation

Htz requires Go 1.10 or later.

go get github.com/faabiosr/htz

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

go get gopkg.in/faabiosr/htz.v0

Usage

Simple usage
package main

import (
	"encoding/json"
	"fmt"
	"github.com/faabiosr/htz"
	"time"
)

func main() {
	checkers := []htz.Checker{
		func() *htz.Check {
			return &htz.Check{
				Name:         "some-api",
				Type:         "internal-service",
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				Details: map[string]interface{}{
					"url": "internal-service.api",
				},
			}
		},
	}

	h := htz.New("my-app", "0.0.1", checkers...)
	res, _ := json.MarshalIndent(h.Check(), "", "  ")

	fmt.Println(string(res))
}
HTTP Check
package main

import (
	"github.com/faabiosr/htz"
	"net/http"
	"time"
)

func main() {
	checkers := []htz.Checker{
		func() *htz.Check {
			return &htz.Check{
				Name:         "some-api",
				Type:         "internal-service",
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				Details: map[string]interface{}{
					"url": "internal-service.api",
				},
			}
		},
	}

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

	http.Handle("/htz", h)

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

Available checkers

DB
package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"github.com/faabiosr/htz"
	"github.com/faabiosr/htz/checker"
	_"github.com/lib/pq"
)

func main() {
	db, _ := sql.Open("postgres", "user=htz dbname=htz")

	h := htz.New("my-app", "0.0.1", checker.DB(db, true))
	res, _ := json.MarshalIndent(h.Check(), "", "  ")

	fmt.Println(string(res))
}
Redis
package main

import (
	"encoding/json"
	"fmt"
	"github.com/faabiosr/htz"
	"github.com/faabiosr/htz/checker"
    "github.com/go-redis/redis"
)

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

	h := htz.New("my-app", "0.0.1", checker.Redis(client, true))
	res, _ := json.MarshalIndent(h.Check(), "", "  ")

	fmt.Println(string(res))
}
Runtime
package main

import (
	"fmt"
	"github.com/faabiosr/htz"
	"github.com/faabiosr/htz/checker"
)

func main() {
	h := htz.New("my-app", "0.0.1", checker.Runtime(false))
	res, _ := json.MarshalIndent(h.Check(), "", "  ")

	fmt.Println(string(res))
}

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 htz show the health of your app.

Example Usage

The following is a simple example using the library:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/faabiosr/htz"
	"time"
)

func main() {
	checkers := []htz.Checker{
		func() *htz.Check {
			return &htz.Check{
				Name:         "some-api",
				Type:         "internal-service",
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
				Details:      map[string]interface{}{
				    "url": "internal-service.api",
			    },
			}
		},
	}

	h := htz.New("my-app", "0.0.1", checkers...)
	res, _ := json.MarshalIndent(h.Check(), "", "  ")

	fmt.Println(string(res))
}

Using with a HTTP Server:

package main

import (
	"github.com/faabiosr/htz"
	"net/http"
	"time"
)

func main() {
	checkers := []htz.Checker{
		func() *htz.Check {
			return &htz.Check{
				Name:         "some-api",
				Status:       false,
				ResponseTime: 6 * time.Second,
				Optional:     false,
			}
		},
	}

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

	http.Handle("/htz", 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"`
	Type         CheckType              `json:"type"`
	Status       bool                   `json:"status"`
	ResponseTime time.Duration          `json:"response_time"`
	Optional     bool                   `json:"optional"`
	Details      map[string]interface{} `json:"details"`
}

Check retrieves the health of service.

type CheckType

type CheckType string

CheckType type.

func (CheckType) MarshalJSON

func (ct CheckType) MarshalJSON() ([]byte, error)

MarshalJSON marshals CheckType instance into string.

type Checker

type Checker func() *Check

Checker it's type function responsible for checking the service health.

type Health

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

Health defines the healthcheck configuration.

func New

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

New retrieves the new instance of Health.

func (*Health) Check

func (h *Health) Check() *Status

Check checks the status of services.

func (*Health) ServeHTTP

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

ServerHTTP retrieves the health status.

type Status

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

Status retrieves the status of health.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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