lib/httpserver: log remote address in error message from httpserver.Errorf

This should improve detection of the root cause of errors.
Thanks to Anant for the idea.
This commit is contained in:
Aliaksandr Valialkin
2020-07-20 14:00:33 +03:00
parent 1c641037e8
commit b35cb293f5
8 changed files with 52 additions and 46 deletions

View File

@@ -172,7 +172,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
requestsTotal.Inc()
path, err := getCanonicalPath(r.URL.Path)
if err != nil {
Errorf(w, "cannot get canonical path: %s", err)
Errorf(w, r, "cannot get canonical path: %s", err)
unsupportedRequestErrors.Inc()
return
}
@@ -238,7 +238,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
return
}
Errorf(w, "unsupported path requested: %q", r.URL.Path)
Errorf(w, r, "unsupported path requested: %q", r.URL.Path)
unsupportedRequestErrors.Inc()
return
}
@@ -482,8 +482,9 @@ var (
)
// Errorf writes formatted error message to w and to logger.
func Errorf(w http.ResponseWriter, format string, args ...interface{}) {
func Errorf(w http.ResponseWriter, r *http.Request, format string, args ...interface{}) {
errStr := fmt.Sprintf(format, args...)
errStr = fmt.Sprintf("remoteAddr: %s; %s", r.RemoteAddr, errStr)
logger.WarnfSkipframes(1, "%s", errStr)
// Extract statusCode from args