Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/kube-scheduler/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func buildHandlerChain(handler http.Handler, authn authenticator.Request, authz
handler = genericapifilters.WithAuthentication(handler, authn, failedHandler, nil, nil)
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
handler = genericapifilters.WithCacheControl(handler)
handler = genericfilters.WithHTTPLogging(handler, nil)
handler = genericfilters.WithHTTPLogging(handler)
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)

return handler
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ var statusesNoTracePred = httplog.StatusIsNot(

// ServeHTTP responds to HTTP requests on the Kubelet.
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
handler := httplog.WithLogging(s.restfulCont, statusesNoTracePred, nil)
handler := httplog.WithLogging(s.restfulCont, statusesNoTracePred)

// monitor http requests
var serverType string
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/apiserver/pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler {
}
handler = genericfilters.WithOptInRetryAfter(handler, c.newServerFullyInitializedFunc())
handler = genericfilters.WithShutdownResponseHeader(handler, c.lifecycleSignals.ShutdownInitiated, c.ShutdownDelayDuration, c.APIServerID)
handler = genericfilters.WithHTTPLogging(handler, c.newIsTerminatingFunc())
handler = genericfilters.WithHTTPLogging(handler)
handler = genericapifilters.WithLatencyTrackers(handler)
// WithRoutine will execute future handlers in a separate goroutine and serving
// handler in current goroutine to minimize the stack memory usage. It must be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,6 @@ func TestTimeoutWithLogging(t *testing.T) {
},
),
),
func() bool {
return false
},
),
)
defer ts.Close()
Expand Down
4 changes: 2 additions & 2 deletions staging/src/k8s.io/apiserver/pkg/server/filters/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func WithPanicRecovery(handler http.Handler, resolver request.RequestInfoResolve
}

// WithHTTPLogging enables logging of incoming requests.
func WithHTTPLogging(handler http.Handler, isTerminating func() bool) http.Handler {
return httplog.WithLogging(handler, httplog.DefaultStacktracePred, isTerminating)
func WithHTTPLogging(handler http.Handler) http.Handler {
return httplog.WithLogging(handler, httplog.DefaultStacktracePred)
}

func withPanicRecovery(handler http.Handler, crashHandler func(http.ResponseWriter, *http.Request, interface{})) http.Handler {
Expand Down
24 changes: 6 additions & 18 deletions staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ type respLogger struct {
addedInfo strings.Builder
addedKeyValuePairs []interface{}
startTime time.Time
isTerminating bool

captureErrorOutput bool

Expand Down Expand Up @@ -101,13 +100,13 @@ func DefaultStacktracePred(status int) bool {
const withLoggingLevel = 3

// WithLogging wraps the handler with logging.
func WithLogging(handler http.Handler, pred StacktracePred, isTerminatingFn func() bool) http.Handler {
func WithLogging(handler http.Handler, pred StacktracePred) http.Handler {
return withLogging(handler, pred, func() bool {
return klog.V(withLoggingLevel).Enabled()
}, isTerminatingFn)
})
}

func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred, isTerminatingFn func() bool) http.Handler {
func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogRequest ShouldLogRequestPred) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if !shouldLogRequest() {
handler.ServeHTTP(w, req)
Expand All @@ -118,16 +117,14 @@ func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogR
if old := respLoggerFromRequest(req); old != nil {
panic("multiple WithLogging calls!")
}

startTime := time.Now()
if receivedTimestamp, ok := request.ReceivedTimestampFrom(ctx); ok {
startTime = receivedTimestamp
}

isTerminating := false
if isTerminatingFn != nil {
isTerminating = isTerminatingFn()
}
rl := newLoggedWithStartTime(req, w, startTime).StacktraceWhen(stackTracePred).IsTerminating(isTerminating)
rl := newLoggedWithStartTime(req, w, startTime)
rl.StacktraceWhen(stackTracePred)
req = req.WithContext(context.WithValue(ctx, respLoggerContextKey, rl))

var logFunc func()
Expand All @@ -138,9 +135,6 @@ func withLogging(handler http.Handler, stackTracePred StacktracePred, shouldLogR
}
}()

if klog.V(3).Enabled() || (rl.isTerminating && klog.V(1).Enabled()) {
defer rl.Log()
}
w = responsewriter.WrapForHTTP1Or2(rl)
handler.ServeHTTP(w, req)

Expand Down Expand Up @@ -211,12 +205,6 @@ func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
return rl
}

// IsTerminating informs the logger that the server is terminating.
func (rl *respLogger) IsTerminating(is bool) *respLogger {
rl.isTerminating = is
return rl
}

// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
// for any status *not* in the given list.
func StatusIsNot(statuses ...int) StacktracePred {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestWithLogging(t *testing.T) {
shouldLogRequest := func() bool { return true }
var handler http.Handler
handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest, nil), DefaultStacktracePred, shouldLogRequest, nil)
handler = withLogging(withLogging(handler, DefaultStacktracePred, shouldLogRequest), DefaultStacktracePred, shouldLogRequest)

func() {
defer func() {
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestLogOf(t *testing.T) {
t.Errorf("Expected %v, got %v", test.want, got)
}
})
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest }, nil)
handler = withLogging(handler, DefaultStacktracePred, func() bool { return test.shouldLogRequest })
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
})
Expand All @@ -135,7 +135,7 @@ func TestUnlogged(t *testing.T) {
}
})
if makeLogger {
handler = WithLogging(handler, DefaultStacktracePred, nil)
handler = WithLogging(handler, DefaultStacktracePred)
}

handler.ServeHTTP(origWriter, req)
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestRespLoggerWithDecoratedResponseWriter(t *testing.T) {
}
})

handler = withLogging(handler, DefaultStacktracePred, func() bool { return true }, nil)
handler = withLogging(handler, DefaultStacktracePred, func() bool { return true })
handler.ServeHTTP(test.r(), req)
})
}
Expand Down
22 changes: 0 additions & 22 deletions staging/src/k8s.io/apiserver/pkg/server/patch_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,6 @@ limitations under the License.

package server

// newIsTerminatingFunc returns a 'func() bool' that relies on the
// 'ShutdownInitiated' life cycle signal of answer if the apiserver
// has started the termination process.
func (c *Config) newIsTerminatingFunc() func() bool {
var shutdownCh <-chan struct{}
// TODO: a properly initialized Config object should always have lifecycleSignals
// initialized, but some config unit tests leave lifecycleSignals as nil.
// Fix the unit tests upstream and then we can remove this check.
if c.lifecycleSignals.ShutdownInitiated != nil {
shutdownCh = c.lifecycleSignals.ShutdownInitiated.Signaled()
}

return func() bool {
select {
case <-shutdownCh:
return true
default:
return false
}
}
}

func (c *Config) newServerFullyInitializedFunc() func() bool {
return func() bool {
select {
Expand Down
2 changes: 1 addition & 1 deletion staging/src/k8s.io/controller-manager/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func BuildHandlerChain(apiHandler http.Handler, authorizationInfo *apiserver.Aut
}
handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver)
handler = genericapifilters.WithCacheControl(handler)
handler = genericfilters.WithHTTPLogging(handler, nil)
handler = genericfilters.WithHTTPLogging(handler)
handler = genericfilters.WithPanicRecovery(handler, requestInfoResolver)

return handler
Expand Down