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 --- metrics.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'metrics.go') diff --git a/metrics.go b/metrics.go index ab9d797..9a56f73 100644 --- a/metrics.go +++ b/metrics.go @@ -3,7 +3,6 @@ package caddy import ( "net/http" "strconv" - "strings" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" @@ -47,7 +46,7 @@ func instrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) next.ServeHTTP(d, r) counter.With(prometheus.Labels{ "code": sanitizeCode(d.status), - "method": strings.ToUpper(r.Method), + "method": sanitizeMethod(r.Method), }).Inc() }) } @@ -76,3 +75,27 @@ func sanitizeCode(s int) string { return strconv.Itoa(s) } } + +// 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" +} -- 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 --- metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'metrics.go') diff --git a/metrics.go b/metrics.go index 9a56f73..8a50260 100644 --- a/metrics.go +++ b/metrics.go @@ -97,5 +97,5 @@ func sanitizeMethod(m string) string { return m } - return "other" + return "OTHER" } -- 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 --- metrics.go | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) (limited to 'metrics.go') diff --git a/metrics.go b/metrics.go index 8a50260..325006f 100644 --- a/metrics.go +++ b/metrics.go @@ -2,8 +2,8 @@ package caddy import ( "net/http" - "strconv" + "github.com/caddyserver/caddy/v2/internal/metrics" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/collectors" "github.com/prometheus/client_golang/prometheus/promauto" @@ -45,8 +45,8 @@ func instrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler) d := newDelegator(w) next.ServeHTTP(d, r) counter.With(prometheus.Labels{ - "code": sanitizeCode(d.status), - "method": sanitizeMethod(r.Method), + "code": metrics.SanitizeCode(d.status), + "method": metrics.SanitizeMethod(r.Method), }).Inc() }) } @@ -66,36 +66,3 @@ func (d *delegator) WriteHeader(code int) { d.status = code d.ResponseWriter.WriteHeader(code) } - -func sanitizeCode(s int) string { - switch s { - case 0, 200: - return "200" - default: - return strconv.Itoa(s) - } -} - -// 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" -} -- cgit v1.2.3