Skip to content

Library to provide basic healthcheck functionality to Go applications.

License

Notifications You must be signed in to change notification settings

gmolinas/health-go

 
 

Repository files navigation

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
    • SQLServer
    • 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/v5"
	healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
	// add some checks on instance creation
	h, _ := health.New(health.WithComponent(health.Component{
		Name:    "myservice",
		Version: "v1.0",
	}), 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/v5"
	healthMysql "github.com/hellofresh/health-go/v5/checks/mysql"
)

func main() {
	// add some checks on instance creation
	h, _ := health.New(health.WithComponent(health.Component{
		Name:    "myservice",
		Version: "v1.0",
	}), 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