github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/conf/http/mws/prometheus.go (about)

     1  package mws
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  	"github.com/prometheus/client_golang/prometheus/promhttp"
     8  )
     9  
    10  func MetricsHandler() func(http.Handler) http.Handler {
    11  	return func(handler http.Handler) http.Handler {
    12  		return &metricsHandler{next: handler}
    13  	}
    14  }
    15  
    16  type metricsHandler struct{ next http.Handler }
    17  
    18  func (h *metricsHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    19  	if req.Method == http.MethodGet && req.URL.Path == "/metrics" {
    20  		promhttp.InstrumentMetricHandler(
    21  			prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{}),
    22  		).(http.HandlerFunc)(rw, req)
    23  		return
    24  	}
    25  
    26  	h.next.ServeHTTP(rw, req)
    27  }