Use map to check whether stack trace is needed

Signed-off-by: Ted Yu <yute@vmware.com>
k3s-v1.15.3
Ted Yu 2019-05-08 14:31:47 -07:00 committed by Ted Yu
parent 16085784bc
commit 85fc089855
2 changed files with 17 additions and 17 deletions

View File

@ -835,10 +835,7 @@ func isLongRunningRequest(path string) bool {
return false return false
} }
// ServeHTTP responds to HTTP requests on the Kubelet. var statusesNoTracePred = httplog.StatusIsNot(
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer httplog.NewLogged(req, &w).StacktraceWhen(
httplog.StatusIsNot(
http.StatusOK, http.StatusOK,
http.StatusFound, http.StatusFound,
http.StatusMovedPermanently, http.StatusMovedPermanently,
@ -846,8 +843,11 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
http.StatusBadRequest, http.StatusBadRequest,
http.StatusNotFound, http.StatusNotFound,
http.StatusSwitchingProtocols, http.StatusSwitchingProtocols,
), )
).Log()
// ServeHTTP responds to HTTP requests on the Kubelet.
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer httplog.NewLogged(req, &w).StacktraceWhen(statusesNoTracePred).Log()
// monitor http requests // monitor http requests
var serverType string var serverType string

View File

@ -125,13 +125,13 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged // StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
// for any status *not* in the given list. // for any status *not* in the given list.
func StatusIsNot(statuses ...int) StacktracePred { func StatusIsNot(statuses ...int) StacktracePred {
return func(status int) bool { statusesNoTrace := map[int]bool{}
for _, s := range statuses { for _, s := range statuses {
if status == s { statusesNoTrace[s] = true
return false
} }
} return func(status int) bool {
return true _, ok := statusesNoTrace[status]
return !ok
} }
} }