Expose health check endpoints as Prometheus/JSON format in seconds 💫
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.
- 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
JSONor Prometheus format. - Extract metrics as a plug and play on any HTTP handler which implements
HandlerFunc.
Install simplehealth using
go get -u github.com/mr-karan/simplehealth
Check the _examples directory for a complete working example.
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{})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())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"
}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") |