health

package module
v0.0.0-...-8aca8b6 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2021 License: BSD-3-Clause Imports: 6 Imported by: 0

README

Health

Go Reference

Monitor and track health of services, send heartbeats (to-do), and expose status through HTTP endpoint.

Installation

Install health using the go get command:

$ go get github.com/gocrumb/health

The package requires no additional dependencies other than Go itself.

Usage

hm := health.New(
	health.Checks(
		http.Head("http://example.com/", http.Period(3*time.Minute)),
		mongo.Ping(mongoClient, mongo.Period(1*time.Minute), mongo.ReadPref(rp)),
		redis.Ping(redisClient), // Falls back to default period (5 minutes)
	),
	health.Logger(log.New(os.Stderr, "[Health] ", log.LstdFlags|log.Lmsgprefix)),
	health.OnFailure(func() {
		log.Fatal("Health check failed; exiting")
	}),
)
hm.Start()

Checks

Check Description
http.Head Sends HTTP HEAD requests to the given URL. Expects response with status code 2XX.
mongo.Ping Pings MongoDB through the given client.
redis.Ping Pings Redis through the given client.

Documentation

Contributing

Contributions are welcome.

License

This package is available under the BSD (3-Clause) License.

TODO

  • Check: TCP connection, fails on disconnect.
  • Check: RabbitMQ
  • Beacon: Send periodic pings to remote health monitoring service (e.g. healthchecks.io)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check interface {
	Notify(ch chan<- error)
	Stop(ch chan<- error)
}

func Periodic

func Periodic(fn func() error, d time.Duration) Check

type Monitor

type Monitor struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"time"

	"github.com/gocrumb/health"
	"github.com/gocrumb/health/http"
)

func main() {
	m := health.New(
		health.Checks(
			http.Head("http://example.com/", http.Period(1*time.Minute)),
		),
	)
	m.Start()
}
Output:

func New

func New(opts ...Option) *Monitor

func (*Monitor) Endpoint

func (m *Monitor) Endpoint() http.Handler

func (*Monitor) Start

func (m *Monitor) Start()

func (*Monitor) State

func (m *Monitor) State(c Check) State

func (*Monitor) Status

func (m *Monitor) Status() State

func (*Monitor) Stop

func (m *Monitor) Stop()

type Option

type Option interface {
	Apply(*Monitor)
}

func Checks

func Checks(checks ...Check) Option

func Logger

func Logger(l *log.Logger) Option

func OnFailure

func OnFailure(fn func()) Option

type OptionFunc

type OptionFunc func(*Monitor)

func (OptionFunc) Apply

func (o OptionFunc) Apply(m *Monitor)

type State

type State int
const (
	Stopped State = iota
	Starting
	Healthy
	Unhealthy
)

func (State) String

func (i State) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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