mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 13:02:14 +00:00
Apiserver don't log stacktrace when proxying
Attempt at closing #32747, With the `RequestInfoResolver` struct, it's possible to inspect the request and get the `Verb`. In this case, the `proxy` value is what I was looking for to avoid logging stacktraces. I'm wrapping the `.Log()` call with an `if` statement to remove all stacktrace logging when we proxied through the apiserver Another approach would have been to add another kind of `StacktracePred` in the `httplog` package. I found this path to be trickier to code as it's currently only accepting int values.
This commit is contained in:
parent
db07433782
commit
9c2705a5a2
@ -134,30 +134,33 @@ func tooManyRequests(req *http.Request, w http.ResponseWriter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RecoverPanics wraps an http Handler to recover and log panics.
|
// RecoverPanics wraps an http Handler to recover and log panics.
|
||||||
func RecoverPanics(handler http.Handler) http.Handler {
|
func RecoverPanics(handler http.Handler, resolver *RequestInfoResolver) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
defer runtime.HandleCrash(func(err interface{}) {
|
defer runtime.HandleCrash(func(err interface{}) {
|
||||||
http.Error(w, "This request caused apisever to panic. Look in log for details.", http.StatusInternalServerError)
|
http.Error(w, "This request caused apisever to panic. Look in log for details.", http.StatusInternalServerError)
|
||||||
glog.Errorf("APIServer panic'd on %v %v: %v\n%s\n", req.Method, req.RequestURI, err, debug.Stack())
|
glog.Errorf("APIServer panic'd on %v %v: %v\n%s\n", req.Method, req.RequestURI, err, debug.Stack())
|
||||||
})
|
})
|
||||||
defer httplog.NewLogged(req, &w).StacktraceWhen(
|
|
||||||
httplog.StatusIsNot(
|
|
||||||
http.StatusOK,
|
|
||||||
http.StatusCreated,
|
|
||||||
http.StatusAccepted,
|
|
||||||
http.StatusBadRequest,
|
|
||||||
http.StatusMovedPermanently,
|
|
||||||
http.StatusTemporaryRedirect,
|
|
||||||
http.StatusConflict,
|
|
||||||
http.StatusNotFound,
|
|
||||||
http.StatusUnauthorized,
|
|
||||||
http.StatusForbidden,
|
|
||||||
http.StatusNotModified,
|
|
||||||
errors.StatusUnprocessableEntity,
|
|
||||||
http.StatusSwitchingProtocols,
|
|
||||||
),
|
|
||||||
).Log()
|
|
||||||
|
|
||||||
|
requestInfo, err := resolver.GetRequestInfo(req)
|
||||||
|
if err != nil || requestInfo.Verb != "proxy" {
|
||||||
|
defer httplog.NewLogged(req, &w).StacktraceWhen(
|
||||||
|
httplog.StatusIsNot(
|
||||||
|
http.StatusOK,
|
||||||
|
http.StatusCreated,
|
||||||
|
http.StatusAccepted,
|
||||||
|
http.StatusBadRequest,
|
||||||
|
http.StatusMovedPermanently,
|
||||||
|
http.StatusTemporaryRedirect,
|
||||||
|
http.StatusConflict,
|
||||||
|
http.StatusNotFound,
|
||||||
|
http.StatusUnauthorized,
|
||||||
|
http.StatusForbidden,
|
||||||
|
http.StatusNotModified,
|
||||||
|
errors.StatusUnprocessableEntity,
|
||||||
|
http.StatusSwitchingProtocols,
|
||||||
|
),
|
||||||
|
).Log()
|
||||||
|
}
|
||||||
// Dispatch to the internal handler
|
// Dispatch to the internal handler
|
||||||
handler.ServeHTTP(w, req)
|
handler.ServeHTTP(w, req)
|
||||||
})
|
})
|
||||||
|
@ -288,7 +288,7 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
|
|||||||
|
|
||||||
secureStartedCh := make(chan struct{})
|
secureStartedCh := make(chan struct{})
|
||||||
if secureLocation != "" {
|
if secureLocation != "" {
|
||||||
handler := apiserver.TimeoutHandler(apiserver.RecoverPanics(s.Handler), longRunningTimeout)
|
handler := apiserver.TimeoutHandler(apiserver.RecoverPanics(s.Handler, s.NewRequestInfoResolver()), longRunningTimeout)
|
||||||
secureServer := &http.Server{
|
secureServer := &http.Server{
|
||||||
Addr: secureLocation,
|
Addr: secureLocation,
|
||||||
Handler: apiserver.MaxInFlightLimit(sem, longRunningRequestCheck, handler),
|
Handler: apiserver.MaxInFlightLimit(sem, longRunningRequestCheck, handler),
|
||||||
@ -358,7 +358,7 @@ func (s *GenericAPIServer) Run(options *options.ServerRunOptions) {
|
|||||||
close(secureStartedCh)
|
close(secureStartedCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
handler := apiserver.TimeoutHandler(apiserver.RecoverPanics(s.InsecureHandler), longRunningTimeout)
|
handler := apiserver.TimeoutHandler(apiserver.RecoverPanics(s.InsecureHandler, s.NewRequestInfoResolver()), longRunningTimeout)
|
||||||
http := &http.Server{
|
http := &http.Server{
|
||||||
Addr: insecureLocation,
|
Addr: insecureLocation,
|
||||||
Handler: handler,
|
Handler: handler,
|
||||||
|
Loading…
Reference in New Issue
Block a user