Skip to content

mr-karan/simplehealth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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")

About

Tiny lib for exposing health-check endpoints as Prometheus/JSON format

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages