-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move common metrics-related funcs to internal package
Signed-off-by: Dave Henderson <[email protected]>
- Loading branch information
1 parent
da4a759
commit 7ca5921
Showing
5 changed files
with
74 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package metrics | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package metrics | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSanitizeMethod(t *testing.T) { | ||
tests := []struct { | ||
method string | ||
expected string | ||
}{ | ||
{method: "get", expected: "GET"}, | ||
{method: "POST", expected: "POST"}, | ||
{method: "OPTIONS", expected: "OPTIONS"}, | ||
{method: "connect", expected: "CONNECT"}, | ||
{method: "trace", expected: "TRACE"}, | ||
{method: "UNKNOWN", expected: "OTHER"}, | ||
{method: strings.Repeat("ohno", 9999), expected: "OTHER"}, | ||
} | ||
|
||
for _, d := range tests { | ||
actual := SanitizeMethod(d.method) | ||
if actual != d.expected { | ||
t.Errorf("Not same: expected %#v, but got %#v", d.expected, actual) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters