package metrics import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" "github.com/prometheus/client_golang/prometheus/promhttp" "net/http" ) type PrometheusHttpMetric struct { Prefix string ClientConnected prometheus.Gauge TransactionTotal *prometheus.CounterVec ResponseTimeHistogram *prometheus.HistogramVec Buckets []float64 } func InitPrometheusHttpMetric(prefix string, buckets []float64) *PrometheusHttpMetric { phm := PrometheusHttpMetric{ Prefix: prefix, ClientConnected: promauto.NewGauge(prometheus.GaugeOpts{ Name: prefix + "_client_connected", Help: "Number of active client connections", }), TransactionTotal: promauto.NewCounterVec(prometheus.CounterOpts{ Name: prefix + "_requests_total", Help: "total HTTP requests processed", }, []string{"code", "method"}, ), ResponseTimeHistogram: promauto.NewHistogramVec(prometheus.HistogramOpts{ Name: prefix + "_response_time", Help: "Histogram of response time for handler", Buckets: buckets, }, []string{"handler", "method"}), } return &phm } func (phm *PrometheusHttpMetric) WrapHandler(handlerLabel string, handlerFunc http.HandlerFunc) http.Handler { //handle := http.HandlerFunc(handlerFunc) wrappedHandler := promhttp.InstrumentHandlerInFlight(phm.ClientConnected, promhttp.InstrumentHandlerCounter(phm.TransactionTotal, promhttp.InstrumentHandlerDuration(phm.ResponseTimeHistogram.MustCurryWith(prometheus.Labels{"handler": handlerLabel}), handlerFunc), ), ) return wrappedHandler }