Skip to content
Snippets Groups Projects
monitoring.go 1.3 KiB
Newer Older
  • Learn to ignore specific revisions
  • package server
    
    import (
    	"net/http"
    
    	"github.com/prometheus/client_golang/prometheus"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    )
    
    var (
    	namespacesMetric = prometheus.NewGauge(prometheus.GaugeOpts{
    		Name: "namespaces",
    		Help: "Number of namespaces",
    	})
    
    	displayListernersMetric = prometheus.NewGauge(prometheus.GaugeOpts{
    		Name: "display_listeners",
    		Help: "Active clients listening to /display",
    	})
    
    	counterMetric = prometheus.NewCounterVec(
    		prometheus.CounterOpts{
    			Name: "requests_total",
    			Help: "Count the total number of requests.",
    		},
    		[]string{"path", "code"},
    	)
    
    	durationMetric = prometheus.NewHistogramVec(
    		prometheus.HistogramOpts{
    			Name:    "requests_duration_seconds",
    			Help:    "A histogram of duration for requests.",
    			Buckets: []float64{.25, .5, 1, 2.5, 5, 10},
    		},
    		[]string{"path"},
    	)
    )
    
    func init() {
    	prometheus.MustRegister(namespacesMetric, displayListernersMetric, counterMetric, durationMetric)
    }
    
    func monitoringMiddleware(handler string) func(next http.Handler) http.Handler {
    	return func(next http.Handler) http.Handler {
    		return promhttp.InstrumentHandlerDuration(
    			durationMetric.MustCurryWith(prometheus.Labels{"path": handler}),
    			promhttp.InstrumentHandlerCounter(counterMetric.MustCurryWith(prometheus.Labels{"path": handler}), next))
    	}
    }