From eb891d46831252c5329218bfbb606727685fea72 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 22 Jan 2022 19:08:57 -0500 Subject: metrics: Enforce smaller set of method labels Signed-off-by: Dave Henderson --- modules/caddyhttp/metrics.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'modules/caddyhttp/metrics.go') diff --git a/modules/caddyhttp/metrics.go b/modules/caddyhttp/metrics.go index 3e5d639..8aa9184 100644 --- a/modules/caddyhttp/metrics.go +++ b/modules/caddyhttp/metrics.go @@ -4,7 +4,6 @@ import ( "context" "net/http" "strconv" - "strings" "sync" "time" @@ -109,7 +108,7 @@ func newMetricsInstrumentedHandler(handler string, mh MiddlewareHandler) *metric func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error { server := serverNameFromContext(r.Context()) labels := prometheus.Labels{"server": server, "handler": h.handler} - method := strings.ToUpper(r.Method) + method := sanitizeMethod(r.Method) // the "code" value is set later, but initialized here to eliminate the possibility // of a panic statusLabels := prometheus.Labels{"server": server, "handler": h.handler, "method": method, "code": ""} @@ -160,6 +159,30 @@ func sanitizeCode(code int) string { return strconv.Itoa(code) } +// Only support the list of "regular" HTTP methods, see +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods +var methodMap = map[string]string{ + "GET": http.MethodGet, "get": http.MethodGet, + "HEAD": http.MethodHead, "head": http.MethodHead, + "PUT": http.MethodPut, "put": http.MethodPut, + "POST": http.MethodPost, "post": http.MethodPost, + "DELETE": http.MethodDelete, "delete": http.MethodDelete, + "CONNECT": http.MethodConnect, "connect": http.MethodConnect, + "OPTIONS": http.MethodOptions, "options": http.MethodOptions, + "TRACE": http.MethodTrace, "trace": http.MethodTrace, + "PATCH": http.MethodPatch, "patch": http.MethodPatch, +} + +// sanitizeMethod sanitizes the method for use as a metric label. This helps +// prevent high cardinality on the method label. The name is always upper case. +func sanitizeMethod(m string) string { + if m, ok := methodMap[m]; ok { + return m + } + + return "other" +} + // taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298 func computeApproximateRequestSize(r *http.Request) int { s := 0 -- cgit v1.2.3 From 042abeb431c55a12cc101efdb75b1c1b67340cb4 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Sat, 22 Jan 2022 19:30:16 -0500 Subject: other is not uppercase Signed-off-by: Dave Henderson --- modules/caddyhttp/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/caddyhttp/metrics.go') diff --git a/modules/caddyhttp/metrics.go b/modules/caddyhttp/metrics.go index 8aa9184..f2023cf 100644 --- a/modules/caddyhttp/metrics.go +++ b/modules/caddyhttp/metrics.go @@ -180,7 +180,7 @@ func sanitizeMethod(m string) string { return m } - return "other" + return "OTHER" } // taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298 -- cgit v1.2.3 From 7ca5921a87c819f9848ccd7ec786aab0f896be72 Mon Sep 17 00:00:00 2001 From: Dave Henderson Date: Mon, 24 Jan 2022 08:35:51 -0500 Subject: move common metrics-related funcs to internal package Signed-off-by: Dave Henderson --- modules/caddyhttp/metrics.go | 39 ++++----------------------------------- 1 file changed, 4 insertions(+), 35 deletions(-) (limited to 'modules/caddyhttp/metrics.go') diff --git a/modules/caddyhttp/metrics.go b/modules/caddyhttp/metrics.go index f2023cf..458c22a 100644 --- a/modules/caddyhttp/metrics.go +++ b/modules/caddyhttp/metrics.go @@ -3,10 +3,10 @@ package caddyhttp import ( "context" "net/http" - "strconv" "sync" "time" + "github.com/caddyserver/caddy/v2/internal/metrics" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" ) @@ -108,7 +108,7 @@ func newMetricsInstrumentedHandler(handler string, mh MiddlewareHandler) *metric func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error { server := serverNameFromContext(r.Context()) labels := prometheus.Labels{"server": server, "handler": h.handler} - method := sanitizeMethod(r.Method) + method := metrics.SanitizeMethod(r.Method) // the "code" value is set later, but initialized here to eliminate the possibility // of a panic statusLabels := prometheus.Labels{"server": server, "handler": h.handler, "method": method, "code": ""} @@ -123,7 +123,7 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re // being called when the headers are written. // Effectively the same behaviour as promhttp.InstrumentHandlerTimeToWriteHeader. writeHeaderRecorder := ShouldBufferFunc(func(status int, header http.Header) bool { - statusLabels["code"] = sanitizeCode(status) + statusLabels["code"] = metrics.SanitizeCode(status) ttfb := time.Since(start).Seconds() httpMetrics.responseDuration.With(statusLabels).Observe(ttfb) return false @@ -142,7 +142,7 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re if statusLabels["code"] == "" { // we still sanitize it, even though it's likely to be 0. A 200 is // returned on fallthrough so we want to reflect that. - statusLabels["code"] = sanitizeCode(wrec.Status()) + statusLabels["code"] = metrics.SanitizeCode(wrec.Status()) } httpMetrics.requestDuration.With(statusLabels).Observe(dur) @@ -152,37 +152,6 @@ func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Re return nil } -func sanitizeCode(code int) string { - if code == 0 { - return "200" - } - return strconv.Itoa(code) -} - -// Only support the list of "regular" HTTP methods, see -// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods -var methodMap = map[string]string{ - "GET": http.MethodGet, "get": http.MethodGet, - "HEAD": http.MethodHead, "head": http.MethodHead, - "PUT": http.MethodPut, "put": http.MethodPut, - "POST": http.MethodPost, "post": http.MethodPost, - "DELETE": http.MethodDelete, "delete": http.MethodDelete, - "CONNECT": http.MethodConnect, "connect": http.MethodConnect, - "OPTIONS": http.MethodOptions, "options": http.MethodOptions, - "TRACE": http.MethodTrace, "trace": http.MethodTrace, - "PATCH": http.MethodPatch, "patch": http.MethodPatch, -} - -// sanitizeMethod sanitizes the method for use as a metric label. This helps -// prevent high cardinality on the method label. The name is always upper case. -func sanitizeMethod(m string) string { - if m, ok := methodMap[m]; ok { - return m - } - - return "OTHER" -} - // taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298 func computeApproximateRequestSize(r *http.Request) int { s := 0 -- cgit v1.2.3