diff --git a/lib/RequestHandler.go b/lib/RequestHandler.go index 0d4adf2..4396965 100644 --- a/lib/RequestHandler.go +++ b/lib/RequestHandler.go @@ -68,6 +68,7 @@ func (wp *WorkerPool) processRequest(request RequestObject) { if err := json.Unmarshal(messageBytes, &result); err != nil { wp.logger.Error("Failed to unmarshal JSON", zap.Error(err), + zap.String("client_addr", utils.GetClientIP(request.Header.Get("X-Forwarded-For"), "")), zap.String("messageBytes", string(messageBytes)), zap.Int("bytesLength", len(messageBytes))) return diff --git a/server/handlers.go b/server/handlers.go index bb7fbe3..62164a3 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -72,7 +72,7 @@ func eventsHandlerJson(workerPool *lib.WorkerPool) http.HandlerFunc { if err != nil { logger.Error("Error reading request body", zap.Error(err), - zap.String("remote_addr", r.RemoteAddr), + zap.String("client_addr", utils.GetClientIP(r.Header.Get("X-Forwarded-For"), r.RemoteAddr)), zap.String("user_agent", r.UserAgent()), zap.Int64("content_length", r.ContentLength), ) diff --git a/utils/time.go b/utils/time.go index 80f348f..c6af6cd 100644 --- a/utils/time.go +++ b/utils/time.go @@ -1,7 +1,24 @@ package utils -import "time" +import ( + "net" + "strings" + "time" +) func NanosToMillis(timestamp int64) int64 { return timestamp * (int64(time.Nanosecond) / int64(time.Millisecond)) } + +func GetClientIP(xForwardedFor string, remoteAddr string) string { + if xForwardedFor != "" { + ips := strings.Split(xForwardedFor, ",") + return strings.TrimSpace(ips[0]) + } + + ip, _, err := net.SplitHostPort(remoteAddr) + if err != nil { + return remoteAddr + } + return ip +}