Skip to content

Commit

Permalink
Implement flag --query-frontend.slow-query-logs-user-header
Browse files Browse the repository at this point in the history
  • Loading branch information
wndhydrnt committed May 15, 2024
1 parent 9b26db4 commit 388dce6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cmd/thanos/query_frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<http-header-name>").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 {
Expand Down
15 changes: 11 additions & 4 deletions internal/cortex/frontend/transport/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 388dce6

Please sign in to comment.