web: add 'code' label to HTTP metrics (#5640)

* web: add prometheus_http_requests_total metrics

Signed-off-by: Simon Pasquier <spasquie@redhat.com>

* Add unit test for requestCounter metric

Signed-off-by: Simon Pasquier <spasquie@redhat.com>
pull/5695/head
Simon Pasquier 6 years ago committed by GitHub
parent 372b3438e5
commit d7f38dfdde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -90,6 +90,13 @@ func withStackTracer(h http.Handler, l log.Logger) http.Handler {
}
var (
requestCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "prometheus_http_requests_total",
Help: "Counter of HTTP requests.",
},
[]string{"handler", "code"},
)
requestDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "prometheus_http_request_duration_seconds",
@ -109,7 +116,7 @@ var (
)
func init() {
prometheus.MustRegister(requestDuration, responseSize)
prometheus.MustRegister(requestCounter, requestDuration, responseSize)
}
// Handler serves various HTTP endpoints of the Prometheus server
@ -199,11 +206,14 @@ func instrumentHandlerWithPrefix(prefix string) func(handlerName string, handler
}
func instrumentHandler(handlerName string, handler http.HandlerFunc) http.HandlerFunc {
return promhttp.InstrumentHandlerDuration(
requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}),
promhttp.InstrumentHandlerResponseSize(
responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}),
handler,
return promhttp.InstrumentHandlerCounter(
requestCounter.MustCurryWith(prometheus.Labels{"handler": handlerName}),
promhttp.InstrumentHandlerDuration(
requestDuration.MustCurryWith(prometheus.Labels{"handler": handlerName}),
promhttp.InstrumentHandlerResponseSize(
responseSize.MustCurryWith(prometheus.Labels{"handler": handlerName}),
handler,
),
),
)
}

@ -21,10 +21,13 @@ import (
"net/http/httptest"
"net/url"
"os"
"strconv"
"strings"
"testing"
"time"
prom_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/notifier"
"github.com/prometheus/prometheus/rules"
@ -402,3 +405,31 @@ func TestDebugHandler(t *testing.T) {
testutil.Equals(t, tc.code, w.Code)
}
}
func TestHTTPMetrics(t *testing.T) {
t.Parallel()
handler := New(nil, &Options{RoutePrefix: "/"})
getReady := func() int {
t.Helper()
w := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/-/ready", nil)
testutil.Ok(t, err)
handler.router.ServeHTTP(w, req)
return w.Code
}
code := getReady()
testutil.Equals(t, http.StatusServiceUnavailable, code)
testutil.Equals(t, 1, int(prom_testutil.ToFloat64(requestCounter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable)))))
handler.Ready()
for range [2]int{} {
code = getReady()
testutil.Equals(t, http.StatusOK, code)
}
testutil.Equals(t, 2, int(prom_testutil.ToFloat64(requestCounter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusOK)))))
testutil.Equals(t, 1, int(prom_testutil.ToFloat64(requestCounter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable)))))
}

Loading…
Cancel
Save