improve logging of ips, to account for xff and x-real-ip

This commit is contained in:
Brian Picciano 2022-08-09 15:23:03 -06:00
parent fa47baffb6
commit 7f6d7366e3

View File

@ -40,15 +40,24 @@ func setLoggerMiddleware(logger *mlog.Logger) middleware {
type logCtxKey string type logCtxKey string
ip, _, _ := net.SplitHostPort(r.RemoteAddr)
ctx := r.Context() ctx := r.Context()
ctx = mctx.Annotate(ctx, ctx = mctx.Annotate(ctx,
logCtxKey("remote_ip"), ip,
logCtxKey("url"), r.URL, logCtxKey("url"), r.URL,
logCtxKey("method"), r.Method, logCtxKey("method"), r.Method,
) )
if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
ctx = mctx.Annotate(ctx, logCtxKey("x_forwarded_for"), xff)
}
if xrip := r.Header.Get("X-Real-IP"); xrip != "" {
ctx = mctx.Annotate(ctx, logCtxKey("x_real_ip"), xrip)
}
if ip, _, _ := net.SplitHostPort(r.RemoteAddr); ip != "" {
ctx = mctx.Annotate(ctx, logCtxKey("remote_ip"), ip)
}
r = r.WithContext(ctx) r = r.WithContext(ctx)
r = apiutil.SetRequestLogger(r, logger) r = apiutil.SetRequestLogger(r, logger)
h.ServeHTTP(rw, r) h.ServeHTTP(rw, r)