diff options
author | Dave Henderson <[email protected]> | 2022-01-22 19:08:57 -0500 |
---|---|---|
committer | Dave Henderson <[email protected]> | 2022-01-25 15:07:17 -0500 |
commit | eb891d46831252c5329218bfbb606727685fea72 (patch) | |
tree | 66e4cfaaaa311afc702c5489588e50ad93976fbc /metrics.go | |
parent | 44e5e9e43f3583f04613bbbb1996e9b5a13a60ac (diff) | |
download | caddy-eb891d46831252c5329218bfbb606727685fea72.tar.gz caddy-eb891d46831252c5329218bfbb606727685fea72.zip |
metrics: Enforce smaller set of method labels
Signed-off-by: Dave Henderson <[email protected]>
Diffstat (limited to 'metrics.go')
-rw-r--r-- | metrics.go | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/metrics.go b/metrics.go index ab9d79784..9a56f733a 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" +} |