healthprometheus

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2022 License: MIT Imports: 3 Imported by: 1

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMetricNameOpts = MetricNameOpts{
	AvailabilityStatusGauge: "health",
}

DefaultMetricNameOpts MetricNameOpts with default values

Functions

This section is empty.

Types

type HealthPrometheus

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

func New

func New(namespace, subsystem string, nameOpts MetricNameOpts) *HealthPrometheus

New create new instance of HealthPrometheus

func NewDefault added in v0.2.0

func NewDefault(namespace, subsystem string) *HealthPrometheus

New create new instance of HealthPrometheus with default metric names

func (*HealthPrometheus) AvailabilityStatusCollector added in v0.2.0

func (m *HealthPrometheus) AvailabilityStatusCollector() prometheus.Collector

func (*HealthPrometheus) Collectors

func (m *HealthPrometheus) Collectors() []prometheus.Collector

Collectors return all Prometheus collectors

func (*HealthPrometheus) Interceptor

Interceptor implements health.Interceptor

Will update underlying metrics as availability status changes

type MetricNameOpts added in v0.2.0

type MetricNameOpts struct {
	// Name for availability status gauge
	AvailabilityStatusGauge string
}

MetricNameOpts options struct for customizing metric names

Directories

Path Synopsis
example module

Jump to

Keyboard shortcuts

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