diff --git a/CHANGELOG.md b/CHANGELOG.md index c3edd24e4b..a0d7f6f046 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re - [#7317](https://github.com/thanos-io/thanos/pull/7317) Tracing: allow specifying resource attributes for the OTLP configuration. - [#7367](https://github.com/thanos-io/thanos/pull/7367) Store Gateway: log request ID in request logs. - [#7361](https://github.com/thanos-io/thanos/pull/7361) Query: *breaking :warning:* pass query stats from remote execution from server to client. We changed the protobuf of the QueryAPI, if you use `query.mode=distributed` you need to update your client (upper level Queriers) first, before updating leaf Queriers (servers). +- [#7363](https://github.com/thanos-io/thanos/pull/7363) Query-frontend: set value of remote_user field in Slow Query Logs from HTTP header ### Changed diff --git a/cmd/thanos/query_frontend.go b/cmd/thanos/query_frontend.go index 78d1ec3744..e60169f103 100644 --- a/cmd/thanos/query_frontend.go +++ b/cmd/thanos/query_frontend.go @@ -161,6 +161,8 @@ func registerQueryFrontend(app *extkingpin.App) { cmd.Flag("query-frontend.vertical-shards", "Number of shards to use when distributing shardable PromQL queries. For more details, you can refer to the Vertical query sharding proposal: https://thanos.io/tip/proposals-accepted/202205-vertical-query-sharding.md").IntVar(&cfg.NumShards) + cmd.Flag("query-frontend.slow-query-logs-user-header", "Set the value of the field remote_user in the slow query logs to the value of the given HTTP header. Falls back to reading the user from the basic auth header.").PlaceHolder("").Default("").StringVar(&cfg.CortexHandlerConfig.SlowQueryLogsUserHeader) + reqLogConfig := extkingpin.RegisterRequestLoggingFlags(cmd) cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ <-chan struct{}, _ bool) error { diff --git a/docs/components/query-frontend.md b/docs/components/query-frontend.md index 8025cdecc8..bb0f68a2fd 100644 --- a/docs/components/query-frontend.md +++ b/docs/components/query-frontend.md @@ -157,6 +157,8 @@ Other cache configuration parameters, you can refer to [redis-index-cache](store Query Frontend supports `--query-frontend.log-queries-longer-than` flag to log queries running longer than some duration. +The field `remote_user` can be read from an HTTP header, like `X-Grafana-User`, by setting `--query-frontend.slow-query-logs-user-header`. + ## Naming Naming is hard :) Please check [here](https://github.com/thanos-io/thanos/pull/2434#discussion_r408300683) to see why we chose `query-frontend` as the name. @@ -295,6 +297,11 @@ Flags: the request, the first matching arg specified will take precedence. If no headers match 'anonymous' will be used. + --query-frontend.slow-query-logs-user-header= + Set the value of the field remote_user in the + slow query logs to the value of the given HTTP + header. Falls back to reading the user from the + basic auth header. --query-frontend.vertical-shards=QUERY-FRONTEND.VERTICAL-SHARDS Number of shards to use when distributing shardable PromQL queries. diff --git a/internal/cortex/frontend/transport/handler.go b/internal/cortex/frontend/transport/handler.go index 602a54d027..8c3020a6d9 100644 --- a/internal/cortex/frontend/transport/handler.go +++ b/internal/cortex/frontend/transport/handler.go @@ -43,9 +43,10 @@ var ( // Config for a Handler. type HandlerConfig struct { - LogQueriesLongerThan time.Duration `yaml:"log_queries_longer_than"` - MaxBodySize int64 `yaml:"max_body_size"` - QueryStatsEnabled bool `yaml:"query_stats_enabled"` + LogQueriesLongerThan time.Duration `yaml:"log_queries_longer_than"` + MaxBodySize int64 `yaml:"max_body_size"` + QueryStatsEnabled bool `yaml:"query_stats_enabled"` + SlowQueryLogsUserHeader string `yaml:"slow_query_logs_user_header"` } // Handler accepts queries and forwards them to RoundTripper. It can log slow queries, @@ -176,7 +177,13 @@ func (f *Handler) reportSlowQuery(r *http.Request, responseHeaders http.Header, thanosTraceID = traceID } - remoteUser, _, _ := r.BasicAuth() + var remoteUser string + // Prefer reading remote user from header. Fall back to the value of basic authentication. + if f.cfg.SlowQueryLogsUserHeader != "" { + remoteUser = r.Header.Get(f.cfg.SlowQueryLogsUserHeader) + } else { + remoteUser, _, _ = r.BasicAuth() + } logMessage := append([]interface{}{ "msg", "slow query detected",