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
parent
050aed013c
commit
550966dbe2
|
@ -155,7 +155,7 @@ func (h *Handler) initMetrics() {
|
||||||
const namespace = "http"
|
const namespace = "http"
|
||||||
const handlerSubsystem = "api"
|
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{
|
h.requests = prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Subsystem: handlerSubsystem,
|
Subsystem: handlerSubsystem,
|
||||||
|
|
|
@ -59,22 +59,24 @@ func TestHandler_ServeHTTP(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
c := promtest.MustFindMetric(t, mfs, "http_api_requests_total", map[string]string{
|
c := promtest.MustFindMetric(t, mfs, "http_api_requests_total", map[string]string{
|
||||||
"handler": "test",
|
"handler": "test",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"path": "/",
|
"path": "/",
|
||||||
"status": "2XX",
|
"status": "2XX",
|
||||||
"user_agent": "ua1",
|
"user_agent": "ua1",
|
||||||
|
"response_code": "200",
|
||||||
})
|
})
|
||||||
if got := c.GetCounter().GetValue(); got != 1 {
|
if got := c.GetCounter().GetValue(); got != 1 {
|
||||||
t.Fatalf("expected counter to be 1, got %v", got)
|
t.Fatalf("expected counter to be 1, got %v", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
g := promtest.MustFindMetric(t, mfs, "http_api_request_duration_seconds", map[string]string{
|
g := promtest.MustFindMetric(t, mfs, "http_api_request_duration_seconds", map[string]string{
|
||||||
"handler": "test",
|
"handler": "test",
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"path": "/",
|
"path": "/",
|
||||||
"status": "2XX",
|
"status": "2XX",
|
||||||
"user_agent": "ua1",
|
"user_agent": "ua1",
|
||||||
|
"response_code": "200",
|
||||||
})
|
})
|
||||||
if got := g.GetHistogram().GetSampleCount(); got != 1 {
|
if got := g.GetHistogram().GetSampleCount(); got != 1 {
|
||||||
t.Fatalf("expected histogram sample count to be 1, got %v", got)
|
t.Fatalf("expected histogram sample count to be 1, got %v", got)
|
||||||
|
|
|
@ -2,6 +2,7 @@ package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -42,11 +43,12 @@ func Metrics(name string, reqMetric *prometheus.CounterVec, durMetric *prometheu
|
||||||
|
|
||||||
defer func(start time.Time) {
|
defer func(start time.Time) {
|
||||||
label := prometheus.Labels{
|
label := prometheus.Labels{
|
||||||
"handler": name,
|
"handler": name,
|
||||||
"method": r.Method,
|
"method": r.Method,
|
||||||
"path": normalizePath(r.URL.Path),
|
"path": normalizePath(r.URL.Path),
|
||||||
"status": statusW.StatusCodeClass(),
|
"status": statusW.StatusCodeClass(),
|
||||||
"user_agent": UserAgent(r),
|
"response_code": fmt.Sprintf("%d", statusW.Code()),
|
||||||
|
"user_agent": UserAgent(r),
|
||||||
}
|
}
|
||||||
durMetric.With(label).Observe(time.Since(start).Seconds())
|
durMetric.With(label).Observe(time.Since(start).Seconds())
|
||||||
reqMetric.With(label).Inc()
|
reqMetric.With(label).Inc()
|
||||||
|
|
Loading…
Reference in New Issue