healthz

package module
v0.0.0-...-9b0725f Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2016 License: Apache-2.0 Imports: 6 Imported by: 4

README

go-healthz

CircleCI

This package provides an HTTP handler that returns information about the health status of the application. If the application is healthy and all the registered check pass, it returns a 200 OK HTTP status, otherwise, it fails with a 503 Service Unavailable. All responses contain a JSON encoded payload with information about the runtime system, current checks statuses and some configurable metadata.

Usage
package main

import (
	"errors"
	"net/http"
	"time"

	"github.com/MEDIGO/go-healthz"
)

const version = "1.0.0"

func main() {
	healthz.Set("version", version)

	healthz.Register("important_check", time.Second*5, func() error {
		return errors.New("fail fail fail")
	})

	http.Handle("/healthz", healthz.Handler())
	http.ListenAndServe(":8000", nil)
}
$ http GET localhost:8000/healthz
HTTP/1.1 503 Service Unavailable
Content-Length: 317
Content-Type: application/json
Date: Fri, 23 Sep 2016 08:55:16 GMT

{
    "status": "Unavailable",
    "time": "2016-09-23T10:55:16.781538256+02:00",
    "since": "2016-09-23T10:55:14.268149643+02:00",
    "metadata": {
        "version": "1.0.0"
    },
    "failures": {
        "important_check": "fail fail fail"
    },
    "runtime": {
        "alloc_bytes": 314048,
        "arch": "amd64",
        "goroutines_count": 4,
        "heap_objects_count": 4575,
        "os": "darwin",
        "total_alloc_bytes": 314048,
        "version": "go1.7"
    }
}

Copyright © 2016 MEDIGO GmbH.

go-healthz is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Documentation

Overview

Package healthz provides an HTTP handler that returns information about the health status of the application.

Index

Constants

View Source
const (
	// StatusOK is returned when all the registered checks pass.
	StatusOK = "OK"
	// StatusUnavailable is returned when any of the registered checks fail.
	StatusUnavailable = "Unavailable"
)

Variables

View Source
var (
	// DefaultRuntimeTTL is the default TTL of the collected runtime stats.
	DefaultRuntimeTTL = 15 * time.Second
	// DefaultChecker is the default golbal checker referenced by the shortcut
	// functions in this package. Change this variable with caution.
	DefaultChecker = NewChecker(&Config{DefaultRuntimeTTL})
)

Functions

func Delete

func Delete(name string)

Delete is a shortcut for DefaultChecker.Delete. See there for more information.

func Deregister

func Deregister(name string)

Deregister is a shortcut for DefaultChecker.Deregister. See there for more information.

func Handler

func Handler() http.Handler

Handler is a shortcut for DefaultChecker.Handler. See there for more information.

func Register

func Register(name string, period time.Duration, fn CheckFunc)

Register is a shortcut for DefaultChecker.Register. See there for more information.

func Set

func Set(name string, value interface{})

Set is a shortcut for DefaultChecker.Set. See there for more information.

Types

type CheckFunc

type CheckFunc func() error

CheckFunc is an application health check function.

type Checker

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

Checker is a health status checker responsible of evaluating the registered checks as well as of collecting useful runtime information about the Go Process. It provides an HTTP handler that returns the current health status.

func NewChecker

func NewChecker(config *Config) *Checker

NewChecker creates a new Checker.

func (*Checker) Delete

func (c *Checker) Delete(name string)

Delete deletes a named entry from the configured metadata.

func (*Checker) Deregister

func (c *Checker) Deregister(name string)

Deregister deregisters a check.

func (*Checker) Handler

func (c *Checker) Handler() http.Handler

Handler returns an HTTP handler to be used as a health check endpoint. If the application is healthy and all the registered check pass, it returns a `200 OK` HTTP status code, otherwise, it fails with a `503 Service Unavailable` code. All responses contain a JSON encoded payload with information about the runtime system, current checks statuses and some configurable metadata.

func (*Checker) Register

func (c *Checker) Register(name string, period time.Duration, fn CheckFunc)

Register registers a check to be evaluated each given period.

func (*Checker) Set

func (c *Checker) Set(name string, value interface{})

Set sets a name value pair as a metadata entry to be returned with earch response. This can be used to store useful debug informaton like version numbers or git commit shas.

func (*Checker) Status

func (c *Checker) Status() Status

Status returns the current service status.

type Config

type Config struct {
	RuntimeTTL time.Duration
}

Config parameterizes a Checker.

type Runtime

type Runtime struct {
	CollectedAt      time.Time `json:"-"`
	Arch             string    `json:"arch"`
	OS               string    `json:"os"`
	Version          string    `json:"version"`
	GoroutinesCount  int       `json:"goroutines_count"`
	HeapObjectsCount int       `json:"heap_objects_count"`
	AllocBytes       int       `json:"alloc_bytes"`
	TotalAllocBytes  int       `json:"total_alloc_bytes"`
}

Runtime contains statistics about the Go's process.

type Status

type Status struct {
	Status   string                 `json:"status"`
	Time     time.Time              `json:"time"`
	Since    time.Time              `json:"since"`
	Runtime  Runtime                `json:"runtime"`
	Failures map[string]string      `json:"failures,omitempty"`
	Metadata map[string]interface{} `json:"metadata,omitempty"`
}

Status represents the service health status.

Jump to

Keyboard shortcuts

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