Skip to content

Commit b7dc012

Browse files
authored
Merge pull request kubernetes#77379 from tedyu/http-stat-map
Use map to check whether stack trace is needed
2 parents c14106a + 85fc089 commit b7dc012

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
@@ -837,19 +837,19 @@ func isLongRunningRequest(path string) bool {
837837
return ok
838838
}
839839

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

854854
// monitor http requests
855855
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)