Skip to content

Commit 167032d

Browse files
authored
Merge pull request #7363 from freenowtech/slow-query-logs-user-header
Query-frontend: Set value of remote_user field in Slow Query Logs from HTTP header
2 parents 863d914 + c9c0024 commit 167032d

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
2020
- [#7317](https://github.com/thanos-io/thanos/pull/7317) Tracing: allow specifying resource attributes for the OTLP configuration.
2121
- [#7367](https://github.com/thanos-io/thanos/pull/7367) Store Gateway: log request ID in request logs.
2222
- [#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).
23+
- [#7363](https://github.com/thanos-io/thanos/pull/7363) Query-frontend: set value of remote_user field in Slow Query Logs from HTTP header
2324

2425
### Changed
2526

cmd/thanos/query_frontend.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ func registerQueryFrontend(app *extkingpin.App) {
161161

162162
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)
163163

164+
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("<http-header-name>").Default("").StringVar(&cfg.CortexHandlerConfig.SlowQueryLogsUserHeader)
165+
164166
reqLogConfig := extkingpin.RegisterRequestLoggingFlags(cmd)
165167

166168
cmd.Setup(func(g *run.Group, logger log.Logger, reg *prometheus.Registry, tracer opentracing.Tracer, _ <-chan struct{}, _ bool) error {

docs/components/query-frontend.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Other cache configuration parameters, you can refer to [redis-index-cache](store
157157

158158
Query Frontend supports `--query-frontend.log-queries-longer-than` flag to log queries running longer than some duration.
159159

160+
The field `remote_user` can be read from an HTTP header, like `X-Grafana-User`, by setting `--query-frontend.slow-query-logs-user-header`.
161+
160162
## Naming
161163

162164
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:
295297
the request, the first matching arg specified
296298
will take precedence. If no headers match
297299
'anonymous' will be used.
300+
--query-frontend.slow-query-logs-user-header=<http-header-name>
301+
Set the value of the field remote_user in the
302+
slow query logs to the value of the given HTTP
303+
header. Falls back to reading the user from the
304+
basic auth header.
298305
--query-frontend.vertical-shards=QUERY-FRONTEND.VERTICAL-SHARDS
299306
Number of shards to use when
300307
distributing shardable PromQL queries.

internal/cortex/frontend/transport/handler.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ var (
4343

4444
// Config for a Handler.
4545
type HandlerConfig struct {
46-
LogQueriesLongerThan time.Duration `yaml:"log_queries_longer_than"`
47-
MaxBodySize int64 `yaml:"max_body_size"`
48-
QueryStatsEnabled bool `yaml:"query_stats_enabled"`
46+
LogQueriesLongerThan time.Duration `yaml:"log_queries_longer_than"`
47+
MaxBodySize int64 `yaml:"max_body_size"`
48+
QueryStatsEnabled bool `yaml:"query_stats_enabled"`
49+
SlowQueryLogsUserHeader string `yaml:"slow_query_logs_user_header"`
4950
}
5051

5152
// 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,
176177
thanosTraceID = traceID
177178
}
178179

179-
remoteUser, _, _ := r.BasicAuth()
180+
var remoteUser string
181+
// Prefer reading remote user from header. Fall back to the value of basic authentication.
182+
if f.cfg.SlowQueryLogsUserHeader != "" {
183+
remoteUser = r.Header.Get(f.cfg.SlowQueryLogsUserHeader)
184+
} else {
185+
remoteUser, _, _ = r.BasicAuth()
186+
}
180187

181188
logMessage := append([]interface{}{
182189
"msg", "slow query detected",

0 commit comments

Comments
 (0)