DE-3247 | updated logging for failing requests

This commit is contained in:
puru
2024-08-26 16:49:15 +05:30
parent 497b7db262
commit dae6a7c324
3 changed files with 20 additions and 2 deletions

View File

@@ -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

View File

@@ -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),
)

View File

@@ -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
}