From a7d3a456d67624472f8d6a846ed41024064526d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20S=CC=8Ctibrany=CC=81?= Date: Wed, 19 Feb 2020 11:57:20 +0100 Subject: [PATCH] =?UTF-8?q?Updated=20code=20based=20on=20Juliens=20comment?= =?UTF-8?q?s=20(via=20Slack).=20Signed-off-by:=20Peter=20S=CC=8Ctibrany?= =?UTF-8?q?=CC=81=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/httputil/context.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/util/httputil/context.go b/util/httputil/context.go index adf93dd0a..89bdac642 100644 --- a/util/httputil/context.go +++ b/util/httputil/context.go @@ -34,23 +34,20 @@ func ContextWithPath(ctx context.Context, path string) context.Context { // ContextFromRequest returns a new context with identifiers of // the request to be used later when logging the query. func ContextFromRequest(ctx context.Context, r *http.Request) context.Context { - reqCtxVal := map[string]string{ - "method": r.Method, + var ip string + if r.RemoteAddr != "" { + // r.RemoteAddr has no defined format, so don't return error if we cannot split it into IP:Port. + ip, _, _ = net.SplitHostPort(r.RemoteAddr) } - - // r.RemoteAddr has no defined format, so don't return error if we cannot split it into IP:Port. - ip, _, _ := net.SplitHostPort(r.RemoteAddr) - if ip != "" { - reqCtxVal["clientIP"] = ip - } - var path string if v := ctx.Value(pathParam); v != nil { path = v.(string) - reqCtxVal["path"] = path } - return promql.NewOriginContext(ctx, map[string]interface{}{ - "httpRequest": reqCtxVal, + "httpRequest": map[string]string{ + "clientIP": ip, + "method": r.Method, + "path": path, + }, }) }