Avoid 3 object creations when no dryRun parameter is provided

These allocations should only occur rarely. ``` BenchmarkGet-12 100000 108013 ns/op 17732 B/op 149 allocs/op BenchmarkGet-12 100000 109045 ns/op 17608 B/op 146 allocs/op ```
parent 83c41eab
...@@ -20,6 +20,7 @@ import ( ...@@ -20,6 +20,7 @@ import (
"bufio" "bufio"
"net" "net"
"net/http" "net/http"
"net/url"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
...@@ -234,7 +235,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp ...@@ -234,7 +235,7 @@ func RecordLongRunning(req *http.Request, requestInfo *request.RequestInfo, comp
// a request. verb must be uppercase to be backwards compatible with existing monitoring tooling. // a request. verb must be uppercase to be backwards compatible with existing monitoring tooling.
func MonitorRequest(req *http.Request, verb, group, version, resource, subresource, scope, component, contentType string, httpCode, respSize int, elapsed time.Duration) { func MonitorRequest(req *http.Request, verb, group, version, resource, subresource, scope, component, contentType string, httpCode, respSize int, elapsed time.Duration) {
reportedVerb := cleanVerb(verb, req) reportedVerb := cleanVerb(verb, req)
dryRun := cleanDryRun(req.URL.Query()["dryRun"]) dryRun := cleanDryRun(req.URL)
client := cleanUserAgent(utilnet.GetHTTPClient(req)) client := cleanUserAgent(utilnet.GetHTTPClient(req))
elapsedMicroseconds := float64(elapsed / time.Microsecond) elapsedMicroseconds := float64(elapsed / time.Microsecond)
elapsedSeconds := elapsed.Seconds() elapsedSeconds := elapsed.Seconds()
...@@ -331,12 +332,19 @@ func cleanVerb(verb string, request *http.Request) string { ...@@ -331,12 +332,19 @@ func cleanVerb(verb string, request *http.Request) string {
return reportedVerb return reportedVerb
} }
func cleanDryRun(dryRun []string) string { func cleanDryRun(u *url.URL) string {
// avoid allocating when we don't see dryRun in the query
if !strings.Contains(u.RawQuery, "dryRun") {
return ""
}
dryRun := u.Query()["dryRun"]
if errs := validation.ValidateDryRun(nil, dryRun); len(errs) > 0 { if errs := validation.ValidateDryRun(nil, dryRun); len(errs) > 0 {
return "invalid" return "invalid"
} }
// Since dryRun could be valid with any arbitrarily long length // Since dryRun could be valid with any arbitrarily long length
// we have to dedup and sort the elements before joining them together // we have to dedup and sort the elements before joining them together
// TODO: this is a fairly large allocation for what it does, consider
// a sort and dedup in a single pass
return strings.Join(utilsets.NewString(dryRun...).List(), ",") return strings.Join(utilsets.NewString(dryRun...).List(), ",")
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment