From 85fc0898556d5c0c4b80920bac62842bfc97d763 Mon Sep 17 00:00:00 2001 From: Ted Yu Date: Wed, 8 May 2019 14:31:47 -0700 Subject: [PATCH] Use map to check whether stack trace is needed Signed-off-by: Ted Yu --- pkg/kubelet/server/server.go | 22 +++++++++---------- .../apiserver/pkg/server/httplog/httplog.go | 12 +++++----- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/pkg/kubelet/server/server.go b/pkg/kubelet/server/server.go index 4a9d9080d4c..7e5740d03a0 100644 --- a/pkg/kubelet/server/server.go +++ b/pkg/kubelet/server/server.go @@ -835,19 +835,19 @@ func isLongRunningRequest(path string) bool { return false } +var statusesNoTracePred = httplog.StatusIsNot( + http.StatusOK, + http.StatusFound, + http.StatusMovedPermanently, + http.StatusTemporaryRedirect, + http.StatusBadRequest, + http.StatusNotFound, + http.StatusSwitchingProtocols, +) + // ServeHTTP responds to HTTP requests on the Kubelet. func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) { - defer httplog.NewLogged(req, &w).StacktraceWhen( - httplog.StatusIsNot( - http.StatusOK, - http.StatusFound, - http.StatusMovedPermanently, - http.StatusTemporaryRedirect, - http.StatusBadRequest, - http.StatusNotFound, - http.StatusSwitchingProtocols, - ), - ).Log() + defer httplog.NewLogged(req, &w).StacktraceWhen(statusesNoTracePred).Log() // monitor http requests var serverType string diff --git a/staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go b/staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go index dcdba69225d..8b7f1fd0474 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go +++ b/staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go @@ -125,13 +125,13 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger { // StatusIsNot returns a StacktracePred which will cause stacktraces to be logged // for any status *not* in the given list. func StatusIsNot(statuses ...int) StacktracePred { + statusesNoTrace := map[int]bool{} + for _, s := range statuses { + statusesNoTrace[s] = true + } return func(status int) bool { - for _, s := range statuses { - if status == s { - return false - } - } - return true + _, ok := statusesNoTrace[status] + return !ok } }