/go-health-prometheus

Go library for integrating alexliesenfeld/health with Prometheus

Primary LanguageGoMIT LicenseMIT

go-health-prometheus

Go library for integrating alexliesenfeld/health with Prometheus. Implements both Health Interceptor and Prometheus Collector.

Go Reference Go Report Card

Module installation

go get github.com/chickenzord/go-health-prometheus

Example usage

package main

import (
	"fmt"
	"net/http"

	"github.com/alexliesenfeld/health"
	healthprometheus "github.com/chickenzord/go-health-prometheus"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
	healthProm := healthprometheus.NewDefault("", "myapp")

	// Setup health checker
	healthChecker := health.NewChecker(
		health.WithInterceptors(healthProm.Interceptor), // Use the interceptor to record health metrics
		// ... checks omitted for brevity
	)

	// Setup Prometheus
	registry := prometheus.NewRegistry()
	registry.MustRegister(healthProm.Collectors()...) // Register the health metric collectors
	// ... you can register another collectors here (e.g. Go process collector) 

	// Setup HTTP server
	mux := http.NewServeMux()
	mux.Handle("/health", health.NewHandler(healthChecker))
	mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))

	fmt.Println("Listening on :9000")
	if err := http.ListenAndServe(":9000", mux); err != nil {
		panic(err)
	}
}

See example folder for more info on how to use this library.

Example metrics

# database is UP
myapp_health{name="database" status="up"} 1
myapp_health{name="database" status="down"} 0
myapp_health{name="database" status="unknown"} 0

# redis is DOWN
myapp_health{name="redis" status="up"} 0
myapp_health{name="redis" status="down"} 1
myapp_health{name="redis" status="unknown"} 0