simplehealth

package module
v0.0.0-...-cd229c0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2020 License: MIT Imports: 4 Imported by: 0

README

simplehealth

Expose health check endpoints as Prometheus/JSON format in seconds 💫

GoDoc

Overview

simplehealth makes it ridiculously easy to expose health checks in your Go service. It is a tiny abstraction over VictoriaMetrics/metrics which is a lightweight alternative to the official Prometheus client library.

Features

  • Accepts a map of service name and a callback func to determine if the service is up or not.
  • Exposition format is configurable, can be in JSON or Prometheus format.
  • Extract metrics as a plug and play on any HTTP handler which implements HandlerFunc.

Installation

Install simplehealth using

go get -u github.com/mr-karan/simplehealth

Usage

Examples

Check the _examples directory for a complete working example.

Registering Metrics

Metrics need to be registered with a manager using the NewManager method. NewManager accepts a map of service name with it's callback function for executing the health check.

For example:

// {"api": true} indicates api service is up and running...
callbacks := map[string]func() bool{"api": func() bool{
	return true
}}
// will construct a metric like `namespace{service="api"} 1`
manager := simplehealth.NewManager(callbacks, simplehealth.Options{})
Exposing Metrics

manager comes with a Collect method which returns a http.HandlerFunc.

router := http.NewServeMux()
// Expose the registered metrics at `/metrics` path.
router.Handle("/metrics", m.Collect())

Exposition Format

You can configure to export metrics either in Prometheus (default) or JSON format.

  • Prometheus example
curl localhost:8888/metrics

app{service="db"} 1
app{service="redis"} 1
  • JSON example
curl localhost:8888/metrics

{ 
   "db":"healthy",
   "redis":"healthy"
}

Configuring Manager

While creating a new manager, you can pass in additional options with simplehealth.Options{}:

Option Type Description
Namespace string Global namespace for each metric exposed as Prometheus format. Optional. (Default: "app")
ExposeDefaultMetrics bool Whether to expose default metrics like go_* and process_* Optional. (Default: false)
ExpositionFormat string Format to expose the metrics. Can be one of prometheus or json Optional. (Default: "prometheus")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manager

type Manager interface {
	Collect() http.HandlerFunc
}

Manager represents the set of methods for collecting and exposing metrics.

func NewManager

func NewManager(ms map[string]func() bool, opts Options) (Manager, error)

NewManager instantiates an object of Manager.

type Options

type Options struct {
	Namespace            string // global namespace for the app
	ExposeDefaultMetrics bool   // whether to expose go_* and process_* metrics
	ExpositionFormat     string // prometheus or json
}

Options represents configuration option for exporter.

type Target

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

Target represents options for registering a new manager.

func (*Target) Collect

func (target *Target) Collect() http.HandlerFunc

Collect creates metrics on the fly and fetches value of the metric by executing the callbacks.s

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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