Fix metrics bucket key high cardinality
This commit is contained in:
parent
2d56be0ebb
commit
4da33c2bc2
4 changed files with 96 additions and 17 deletions
|
@ -159,12 +159,27 @@ func containsHeader(req *http.Request, name, value string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// getMethod returns the request's method.
|
||||
// It checks whether the method is a valid UTF-8 string.
|
||||
// To restrict the (potentially infinite) number of accepted values for the method,
|
||||
// and avoid unbounded memory issues,
|
||||
// values that are not part of the set of HTTP verbs are replaced with EXTENSION_METHOD.
|
||||
// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
|
||||
// https://datatracker.ietf.org/doc/html/rfc2616/#section-5.1.1.
|
||||
func getMethod(r *http.Request) string {
|
||||
if !utf8.ValidString(r.Method) {
|
||||
log.Warnf("Invalid HTTP method encoding: %s", r.Method)
|
||||
log.WithoutContext().Warnf("Invalid HTTP method encoding: %s", r.Method)
|
||||
return "NON_UTF8_HTTP_METHOD"
|
||||
}
|
||||
return r.Method
|
||||
|
||||
switch r.Method {
|
||||
case "HEAD", "GET", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE", // https://datatracker.ietf.org/doc/html/rfc7231#section-4
|
||||
"PATCH", // https://datatracker.ietf.org/doc/html/rfc5789#section-2
|
||||
"PRI": // https://datatracker.ietf.org/doc/html/rfc7540#section-11.6
|
||||
return r.Method
|
||||
default:
|
||||
return "EXTENSION_METHOD"
|
||||
}
|
||||
}
|
||||
|
||||
type retryMetrics interface {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/go-kit/kit/metrics"
|
||||
|
@ -98,3 +99,33 @@ func TestCloseNotifier(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getMethod(t *testing.T) {
|
||||
testCases := []struct {
|
||||
method string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
method: http.MethodGet,
|
||||
expected: http.MethodGet,
|
||||
},
|
||||
{
|
||||
method: strings.ToLower(http.MethodGet),
|
||||
expected: "EXTENSION_METHOD",
|
||||
},
|
||||
{
|
||||
method: "THIS_IS_NOT_A_VALID_METHOD",
|
||||
expected: "EXTENSION_METHOD",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
test := test
|
||||
t.Run(test.method, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
request := httptest.NewRequest(test.method, "http://example.com", nil)
|
||||
assert.Equal(t, test.expected, getMethod(request))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue