Skip to content

Commit 85fc089

Browse files
tedyuyutedz
authored andcommitted
Use map to check whether stack trace is needed
Signed-off-by: Ted Yu <[email protected]>
1 parent 1608578 commit 85fc089

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

pkg/kubelet/server/server.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -835,19 +835,19 @@ func isLongRunningRequest(path string) bool {
835835
return false
836836
}
837837

838+
var statusesNoTracePred = httplog.StatusIsNot(
839+
http.StatusOK,
840+
http.StatusFound,
841+
http.StatusMovedPermanently,
842+
http.StatusTemporaryRedirect,
843+
http.StatusBadRequest,
844+
http.StatusNotFound,
845+
http.StatusSwitchingProtocols,
846+
)
847+
838848
// ServeHTTP responds to HTTP requests on the Kubelet.
839849
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
840-
defer httplog.NewLogged(req, &w).StacktraceWhen(
841-
httplog.StatusIsNot(
842-
http.StatusOK,
843-
http.StatusFound,
844-
http.StatusMovedPermanently,
845-
http.StatusTemporaryRedirect,
846-
http.StatusBadRequest,
847-
http.StatusNotFound,
848-
http.StatusSwitchingProtocols,
849-
),
850-
).Log()
850+
defer httplog.NewLogged(req, &w).StacktraceWhen(statusesNoTracePred).Log()
851851

852852
// monitor http requests
853853
var serverType string

staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
125125
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
126126
// for any status *not* in the given list.
127127
func StatusIsNot(statuses ...int) StacktracePred {
128+
statusesNoTrace := map[int]bool{}
129+
for _, s := range statuses {
130+
statusesNoTrace[s] = true
131+
}
128132
return func(status int) bool {
129-
for _, s := range statuses {
130-
if status == s {
131-
return false
132-
}
133-
}
134-
return true
133+
_, ok := statusesNoTrace[status]
134+
return !ok
135135
}
136136
}
137137

0 commit comments

Comments
 (0)