health

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: ISC Imports: 5 Imported by: 3

README

Go Doc Build Status Go Report Card Test Coverage Maintainability

Health

Health is a minimal package for implementing health checking for services built in Go.

Quick Start

package main

import (
  "net/http"

  "github.com/moorara/health"
)

func main() {
  logger := &Logger{}
  dbClient := NewClient("db-client")
  queueClient := NewClient("queue-client")

  health.SetLogger(logger)
  health.RegisterChecker(dbClient, queueClient)

  logger.Infof("Listening on port 8080 ...")
  http.Handle("/health", health.HandlerFunc())
  http.ListenAndServe(":8080", nil)
}

You can find the complete example here.

Behaviour

Here is how the handler returned from HandlerFunc() behaves:

  1. A new context with a timeout will be derived from the request context.
  2. The CheckHealth() method of all registered Checkers will be called each in a new goroutine.
    • If at least one of the check fails, the handler responds with 503 status code.
    • Only the failed checks will be logged (if logging enabled).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandlerFunc

func HandlerFunc() http.HandlerFunc

HandlerFunc returns an http handler function for checking the health of registered checkers.

func RegisterChecker

func RegisterChecker(c ...Checker)

RegisterChecker registers new health checkers.

func SetLogger

func SetLogger(l Logger)

SetLogger enables logging.

func SetTimeout

func SetTimeout(d time.Duration)

SetTimeout sets the timeout for health checkers. If the timeout reaches, the health check is considered failed. The default timeout is 5 seconds.

Types

type Checker

type Checker interface {
	fmt.Stringer
	CheckHealth(context.Context) error
}

Checker is the interface for checking the health of a component.

type Logger

type Logger interface {
	Errorf(template string, args ...interface{})
}

Logger is a simple interface for logging.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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