@marcel91200 / caddy Mirror · read-only / commits / eb891d46
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 23:00 UTC

metrics: Enforce smaller set of method labels

Signed-off-by: Dave Henderson <dhenderson@gmail.com>

Dave Henderson committed Jan 22, 2022 at 19:08 UTC eb891d46831252c5329218bfbb606727685fea72
3 files changed +73 -4
metrics.go
+25 -2
@@ -3,7 +3,6 @@ package caddy
3 import (
4 "net/http"
5 "strconv"
6 - "strings"
6
7 "github.com/prometheus/client_golang/prometheus"
8 "github.com/prometheus/client_golang/prometheus/collectors"
@@ -47,7 +46,7 @@ func instrumentHandlerCounter(counter *prometheus.CounterVec, next http.Handler)
46 next.ServeHTTP(d, r)
47 counter.With(prometheus.Labels{
48 "code": sanitizeCode(d.status),
50 - "method": strings.ToUpper(r.Method),
49 + "method": sanitizeMethod(r.Method),
50 }).Inc()
51 })
52 }
@@ -76,3 +75,27 @@ func sanitizeCode(s int) string {
75 return strconv.Itoa(s)
76 }
77 }
78 +
79 +// Only support the list of "regular" HTTP methods, see
80 +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
81 +var methodMap = map[string]string{
82 + "GET": http.MethodGet, "get": http.MethodGet,
83 + "HEAD": http.MethodHead, "head": http.MethodHead,
84 + "PUT": http.MethodPut, "put": http.MethodPut,
85 + "POST": http.MethodPost, "post": http.MethodPost,
86 + "DELETE": http.MethodDelete, "delete": http.MethodDelete,
87 + "CONNECT": http.MethodConnect, "connect": http.MethodConnect,
88 + "OPTIONS": http.MethodOptions, "options": http.MethodOptions,
89 + "TRACE": http.MethodTrace, "trace": http.MethodTrace,
90 + "PATCH": http.MethodPatch, "patch": http.MethodPatch,
91 +}
92 +
93 +// sanitizeMethod sanitizes the method for use as a metric label. This helps
94 +// prevent high cardinality on the method label. The name is always upper case.
95 +func sanitizeMethod(m string) string {
96 + if m, ok := methodMap[m]; ok {
97 + return m
98 + }
99 +
100 + return "other"
101 +}
modules/caddyhttp/metrics.go
+25 -2
@@ -4,7 +4,6 @@ import (
4 "context"
5 "net/http"
6 "strconv"
7 - "strings"
7 "sync"
8 "time"
9
@@ -109,7 +108,7 @@ func newMetricsInstrumentedHandler(handler string, mh MiddlewareHandler) *metric
108 func (h *metricsInstrumentedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
109 server := serverNameFromContext(r.Context())
110 labels := prometheus.Labels{"server": server, "handler": h.handler}
112 - method := strings.ToUpper(r.Method)
111 + method := sanitizeMethod(r.Method)
112 // the "code" value is set later, but initialized here to eliminate the possibility
113 // of a panic
114 statusLabels := prometheus.Labels{"server": server, "handler": h.handler, "method": method, "code": ""}
@@ -160,6 +159,30 @@ func sanitizeCode(code int) string {
159 return strconv.Itoa(code)
160 }
161
162 +// Only support the list of "regular" HTTP methods, see
163 +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
164 +var methodMap = map[string]string{
165 + "GET": http.MethodGet, "get": http.MethodGet,
166 + "HEAD": http.MethodHead, "head": http.MethodHead,
167 + "PUT": http.MethodPut, "put": http.MethodPut,
168 + "POST": http.MethodPost, "post": http.MethodPost,
169 + "DELETE": http.MethodDelete, "delete": http.MethodDelete,
170 + "CONNECT": http.MethodConnect, "connect": http.MethodConnect,
171 + "OPTIONS": http.MethodOptions, "options": http.MethodOptions,
172 + "TRACE": http.MethodTrace, "trace": http.MethodTrace,
173 + "PATCH": http.MethodPatch, "patch": http.MethodPatch,
174 +}
175 +
176 +// sanitizeMethod sanitizes the method for use as a metric label. This helps
177 +// prevent high cardinality on the method label. The name is always upper case.
178 +func sanitizeMethod(m string) string {
179 + if m, ok := methodMap[m]; ok {
180 + return m
181 + }
182 +
183 + return "other"
184 +}
185 +
186 // taken from https://github.com/prometheus/client_golang/blob/6007b2b5cae01203111de55f753e76d8dac1f529/prometheus/promhttp/instrument_server.go#L298
187 func computeApproximateRequestSize(r *http.Request) int {
188 s := 0
modules/caddyhttp/metrics_test.go
+23
@@ -5,6 +5,7 @@ import (
5 "errors"
6 "net/http"
7 "net/http/httptest"
8 + "strings"
9 "testing"
10
11 "github.com/prometheus/client_golang/prometheus/testutil"
@@ -82,3 +83,25 @@ type middlewareHandlerFunc func(http.ResponseWriter, *http.Request, Handler) err
83 func (f middlewareHandlerFunc) ServeHTTP(w http.ResponseWriter, r *http.Request, h Handler) error {
84 return f(w, r, h)
85 }
86 +
87 +func TestSanitizeMethod(t *testing.T) {
88 + tests := []struct {
89 + method string
90 + expected string
91 + }{
92 + {method: "get", expected: "GET"},
93 + {method: "POST", expected: "POST"},
94 + {method: "OPTIONS", expected: "OPTIONS"},
95 + {method: "connect", expected: "CONNECT"},
96 + {method: "trace", expected: "TRACE"},
97 + {method: "UNKNOWN", expected: "other"},
98 + {method: strings.Repeat("ohno", 9999), expected: "other"},
99 + }
100 +
101 + for _, d := range tests {
102 + actual := sanitizeMethod(d.method)
103 + if actual != d.expected {
104 + t.Errorf("Not same: expected %#v, but got %#v", d.expected, actual)
105 + }
106 + }
107 +}