From b2d6c37a4176c23ce557c6589db745a85cb7ecf7 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Wed, 24 Apr 2024 13:45:59 +0100 Subject: [PATCH] [CHORE] considering X-Forwarded-For on HTTP Logging Signed-off-by: Nicolas Takashi --- CHANGELOG.md | 1 + pkg/logging/http.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bb2bf8b686..078665bc985 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#7233](https://github.com/thanos-io/thanos/pull/7233): UI: Showing Block Size Stats - [#7280](https://github.com/thanos-io/thanos/pull/7281): Adding User-Agent to request logs - [#7219](https://github.com/thanos-io/thanos/pull/7219): Receive: add `--remote-write.client-tls-secure` and `--remote-write.client-tls-skip-verify` flags to stop relying on grpc server config to determine grpc client secure/skipVerify. +- [#7302](https://github.com/thanos-io/thanos/pull/7303) Considering the `X-Forwarded-For` header for the remote address in the logs. ### Changed diff --git a/pkg/logging/http.go b/pkg/logging/http.go index d7d8b299622..229bcf07995 100644 --- a/pkg/logging/http.go +++ b/pkg/logging/http.go @@ -37,13 +37,19 @@ func (m *HTTPServerMiddleware) preCall(name string, start time.Time, r *http.Req func (m *HTTPServerMiddleware) postCall(name string, start time.Time, wrapped *httputil.ResponseWriterWithStatus, r *http.Request) { status := wrapped.Status() + + remoteAddr := r.Header.Get("X-Forwarded-For") + if remoteAddr == "" { + remoteAddr = r.RemoteAddr + } + logger := log.With(m.logger, "http.method", fmt.Sprintf("%s %s", r.Method, r.URL), "http.request_id", r.Header.Get("X-Request-ID"), "http.user_agent", r.Header.Get("User-Agent"), "http.status_code", fmt.Sprintf("%d", status), "http.time_ms", fmt.Sprintf("%v", durationToMilliseconds(time.Since(start))), - "http.remote_addr", r.RemoteAddr, + "http.remote_addr", remoteAddr, "thanos.method_name", name) logger = m.opts.filterLog(logger)