lib/httpserver: emit X-Forwarded-For additionally to remoteAddr in error logs

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/659
This commit is contained in:
Aliaksandr Valialkin 2020-07-29 13:12:15 +03:00
parent 328b52e5ff
commit 67a64c142d

View File

@ -484,7 +484,11 @@ var (
// Errorf writes formatted error message to w and to logger.
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)
remoteAddr := strconv.Quote(r.RemoteAddr) // quote remoteAddr and X-Forwarded-For, since they may contain untrusted input
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
remoteAddr += ", X-Forwarded-For: " + strconv.Quote(addr)
}
errStr = fmt.Sprintf("remoteAddr: %s; %s", remoteAddr, errStr)
logger.WarnfSkipframes(1, "%s", errStr)
// Extract statusCode from args