health

package module
v1.2.13 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

health-go

Go Report Card Go Doc

  • Exposes an HTTP handler that retrieves health status of the application
  • Implements some generic checkers for the following services:
    • RabbitMQ
    • PostgreSQL
    • Redis
    • HTTP
    • MongoDB
    • MySQL
    • gRPC
    • Memcached
    • InfluxDB

Usage

The library exports Handler and HandlerFunc functions which are fully compatible with net/http.

Additionally, library exports Measure function that returns summary status for all the registered health checks, so it can be used in non-HTTP environments.

Handler
package main

import (
	"context"
	"net/http"
	"time"

	"github.com/hellofresh/health-go/v4"
	healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
)

func main() {
	// add some checks on instance creation
	h, _ := health.New(health.WithChecks(health.Config{
		Name:      "rabbitmq",
		Timeout:   time.Second * 5,
		SkipOnErr: true,
		Check: func(ctx context.Context) error {
			// rabbitmq health check implementation goes here
			return nil
		}}, health.Config{
		Name: "mongodb",
		Check: func(ctx context.Context) error {
			// mongo_db health check implementation goes here
			return nil
		},
	},
	))

	// and then add some more if needed
	h.Register(health.Config{
		Name:      "mysql",
		Timeout:   time.Second * 2,
		SkipOnErr: false,
		Check: healthMysql.New(healthMysql.Config{
			DSN: "test:test@tcp(0.0.0.0:31726)/test?charset=utf8",
		}),
	})

	http.Handle("/status", h.Handler())
	http.ListenAndServe(":3000", nil)
}
HandlerFunc
package main

import (
	"context"
	"net/http"
	"time"

	"github.com/go-chi/chi"
	"github.com/hellofresh/health-go/v4"
	healthMysql "github.com/hellofresh/health-go/v4/checks/mysql"
)

func main() {
	// add some checks on instance creation
	h, _ := health.New(health.WithChecks(health.Config{
		Name:      "rabbitmq",
		Timeout:   time.Second * 5,
		SkipOnErr: true,
		Check: func(ctx context.Context) error {
			// rabbitmq health check implementation goes here
			return nil
		}}, health.Config{
		Name: "mongodb",
		Check: func(ctx context.Context) error {
			// mongo_db health check implementation goes here
			return nil
		},
	},
	))

	// and then add some more if needed
	h.Register(health.Config{
		Name:      "mysql",
		Timeout:   time.Second * 2,
		SkipOnErr: false,
		Check: healthMysql.New(healthMysql.Config{
			DSN: "test:test@tcp(0.0.0.0:31726)/test?charset=utf8",
		}),
	})

	r := chi.NewRouter()
	r.Get("/status", h.HandlerFunc)
	http.ListenAndServe(":3000", nil)
}

For more examples please check here

API Documentation

GET /status

Get the health of the application.

  • Method: GET
  • Endpoint: /status
  • Request:
curl localhost:3000/status
  • Response:

HTTP/1.1 200 OK

{
  "status": "OK",
  "timestamp": "2017-01-01T00:00:00.413567856+033:00",
  "system": {
    "version": "go1.8",
    "goroutines_count": 4,
    "total_alloc_bytes": 21321,
    "heap_objects_count": 21323,
    "alloc_bytes": 234523
  }
}

HTTP/1.1 200 OK

{
  "status": "Partially Available",
  "timestamp": "2017-01-01T00:00:00.413567856+033:00",
  "failures": {
    "rabbitmq": "Failed during rabbitmq health check"
  },
  "system": {
    "version": "go1.8",
    "goroutines_count": 4,
    "total_alloc_bytes": 21321,
    "heap_objects_count": 21323,
    "alloc_bytes": 234523
  }
}

HTTP/1.1 503 Service Unavailable

{
  "status": "Unavailable",
  "timestamp": "2017-01-01T00:00:00.413567856+033:00",
  "failures": {
    "mongodb": "Failed during mongodb health check"
  },
  "system": {
    "version": "go1.8",
    "goroutines_count": 4,
    "total_alloc_bytes": 21321,
    "heap_objects_count": 21323,
    "alloc_bytes": 234523
  }
}

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

GitHub @hellofresh  ·  Medium @engineering.hellofresh

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Check

type Check struct {
	// IsOK tells if all non-skippable services are ok.
	IsOK IsOK `json:"is_service_ok"`
	// Status is the check status.
	Status Status `json:"status"`
	// Timestamp is the time in which the check occurred.
	Timestamp time.Time `json:"timestamp"`
	// Services holds the checks along with their messages.
	Services map[string]ServiceStatus `json:"service"`
	// System holds information of the go process.
	System `json:"system"`
}

Check represents the health check response.

type CheckFunc

type CheckFunc func(context.Context) error

CheckFunc is the func which executes the check.

type Config

type Config struct {
	// Name is the name of the resource to be checked.
	Name string
	// Timeout is the timeout defined for every check.
	Timeout time.Duration
	// SkipOnErr if set to true, it will retrieve StatusOK providing the error message from the failed resource.
	SkipOnErr bool
	// Check is the func which executes the check.
	Check CheckFunc
}

Config carries the parameters to run the check.

type Health

type Health struct {
	// contains filtered or unexported fields
}

Health is the health-checks container

func New

func New(opts ...Option) (*Health, error)

New instantiates and build new health check container

func (*Health) LivenessHandler

func (h *Health) LivenessHandler() http.Handler

func (*Health) LivenessHandlerFunc

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

func (*Health) Measure

func (h *Health) Measure(ctx context.Context) Check

Measure runs all the registered health checks and returns summary status

func (*Health) ReadinessHandler

func (h *Health) ReadinessHandler() http.Handler

ReadinessHandler returns an readiness HTTP handler (http.HandlerFunc).

func (*Health) ReadinessHandlerFunc

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

ReadinessHandlerFunc is the readiness HTTP handler function.

func (*Health) Register

func (h *Health) Register(c Config) error

Register registers a check config to be performed.

type IsOK

type IsOK bool

type Liveness

type Liveness struct {
	IsOK IsOK `json:"is_service_ok"`
}

type Option

type Option func(*Health) error

Option is the health-container options type

func WithChecks

func WithChecks(checks ...Config) Option

WithChecks adds checks to newly instantiated health-container

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider, instrumentationName string) Option

WithTracerProvider sets trace provider for the checks and instrumentation name that will be used for tracer from trace provider.

type ServiceStatus

type ServiceStatus struct {
	IsOk      bool   `json:"is_ok"`
	Message   string `json:"message"`
	Skippable bool   `json:"skippable"`
}

type Status

type Status string

Status type represents health status

const (
	StatusOK                 Status = "OK"
	StatusPartiallyAvailable Status = "Partially Available"
	StatusUnavailable        Status = "Unavailable"
	StatusTimeout            Status = "Timeout during health check"
)

Possible health statuses

type System

type System struct {
	// Version is the go version.
	Version string `json:"version"`
	// GoroutinesCount is the number of the current goroutines.
	GoroutinesCount int `json:"goroutines_count"`
	// TotalAllocBytes is the total bytes allocated.
	TotalAllocBytes int `json:"total_alloc_bytes"`
	// HeapObjectsCount is the number of objects in the go heap.
	HeapObjectsCount int `json:"heap_objects_count"`
	// TotalAllocBytes is the bytes allocated and not yet freed.
	AllocBytes int `json:"alloc_bytes"`
}

System runtime variables about the go process.

Directories

Path Synopsis
checks
influxdb
Package influxdb implements a health check for InfluxDB instance.
Package influxdb implements a health check for InfluxDB instance.

Jump to

Keyboard shortcuts

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