From 550966dbe28493be530e1587aa76994a2eaa2810 Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Wed, 19 Aug 2020 13:19:18 -0700 Subject: [PATCH] chore(http): add response_code as label to http_api_* metrics (#19389) This was added so that we can distinguish between 4XX and 401 class errors. It should have a minimal impact in overall cardinality. Co-authored-by: Greg Linton Co-authored-by: Greg Linton --- http/handler.go | 2 +- http/handler_test.go | 22 ++++++++++++---------- kit/transport/http/middleware.go | 12 +++++++----- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/http/handler.go b/http/handler.go index d584410317..6aeb1c7e94 100644 --- a/http/handler.go +++ b/http/handler.go @@ -155,7 +155,7 @@ func (h *Handler) initMetrics() { const namespace = "http" const handlerSubsystem = "api" - labelNames := []string{"handler", "method", "path", "status", "user_agent"} + labelNames := []string{"handler", "method", "path", "status", "user_agent", "response_code"} h.requests = prometheus.NewCounterVec(prometheus.CounterOpts{ Namespace: namespace, Subsystem: handlerSubsystem, diff --git a/http/handler_test.go b/http/handler_test.go index 6df8f7bc91..5edd13cf45 100644 --- a/http/handler_test.go +++ b/http/handler_test.go @@ -59,22 +59,24 @@ func TestHandler_ServeHTTP(t *testing.T) { } c := promtest.MustFindMetric(t, mfs, "http_api_requests_total", map[string]string{ - "handler": "test", - "method": "GET", - "path": "/", - "status": "2XX", - "user_agent": "ua1", + "handler": "test", + "method": "GET", + "path": "/", + "status": "2XX", + "user_agent": "ua1", + "response_code": "200", }) if got := c.GetCounter().GetValue(); got != 1 { t.Fatalf("expected counter to be 1, got %v", got) } g := promtest.MustFindMetric(t, mfs, "http_api_request_duration_seconds", map[string]string{ - "handler": "test", - "method": "GET", - "path": "/", - "status": "2XX", - "user_agent": "ua1", + "handler": "test", + "method": "GET", + "path": "/", + "status": "2XX", + "user_agent": "ua1", + "response_code": "200", }) if got := g.GetHistogram().GetSampleCount(); got != 1 { t.Fatalf("expected histogram sample count to be 1, got %v", got) diff --git a/kit/transport/http/middleware.go b/kit/transport/http/middleware.go index 6713996ca8..3450c12f1f 100644 --- a/kit/transport/http/middleware.go +++ b/kit/transport/http/middleware.go @@ -2,6 +2,7 @@ package http import ( "context" + "fmt" "net/http" "path" "strings" @@ -42,11 +43,12 @@ func Metrics(name string, reqMetric *prometheus.CounterVec, durMetric *prometheu defer func(start time.Time) { label := prometheus.Labels{ - "handler": name, - "method": r.Method, - "path": normalizePath(r.URL.Path), - "status": statusW.StatusCodeClass(), - "user_agent": UserAgent(r), + "handler": name, + "method": r.Method, + "path": normalizePath(r.URL.Path), + "status": statusW.StatusCodeClass(), + "response_code": fmt.Sprintf("%d", statusW.Code()), + "user_agent": UserAgent(r), } durMetric.With(label).Observe(time.Since(start).Seconds()) reqMetric.With(label).Inc()