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  <greg@influxdata.com>

Co-authored-by: Greg Linton  <greg@influxdata.com>
pull/19324/head
Michael Desa 2020-08-19 13:19:18 -07:00 committed by GitHub
parent 050aed013c
commit 550966dbe2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 16 deletions

View File

@ -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,

View File

@ -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)

View File

@ -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()