Skip to content

Commit 4c9a2af

Browse files
pracuccipstibrany
andauthored
Propagate whether query stats is enabled from query-frontend down the request path (#3595)
* Propagate whether query stats is enabled from query-frontend down the request path Signed-off-by: Marco Pracucci <[email protected]> * Update CHANGELOG.md Signed-off-by: Marco Pracucci <[email protected]> Co-authored-by: Peter Štibraný <[email protected]> Co-authored-by: Peter Štibraný <[email protected]>
1 parent d1da3c1 commit 4c9a2af

17 files changed

+275
-114
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## master / unreleased
44

5+
* [CHANGE] Querier: it's not required to set `-frontend.query-stats-enabled=true` in the querier anymore to enable query statistics logging in the query-frontend. The flag is now required to be configured only in the query-frontend and it will be propagated to the queriers. #3595
56
* [CHANGE] Blocks storage: compactor is now required when running a Cortex cluster with the blocks storage, because it also keeps the bucket index updated. #3583
67
* [CHANGE] Blocks storage: block deletion marks are now stored in a per-tenant global markers/ location too, other than within the block location. The compactor, at startup, will copy deletion marks from the block location to the global location. This migration is required only once, so you can safely disable it via `-compactor.block-deletion-marks-migration-enabled=false` once new compactor has successfully started once in your cluster. #3583
78
* [ENHANCEMENT] Blocks storage: introduced a per-tenant bucket index, periodically updated by the compactor, used to avoid full bucket scanning done by queriers and store-gateways. The bucket index is updated by the compactor during blocks cleanup, on every `-compactor.cleanup-interval`. #3553 #3555 #3561 #3583

docs/configuration/config-file-reference.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -877,8 +877,7 @@ The `query_frontend_config` configures the Cortex query-frontend.
877877
[max_body_size: <int> | default = 10485760]
878878
879879
# True to enable query statistics tracking. When enabled, a message with some
880-
# statistics is logged for every query. This configuration option must be set
881-
# both on query-frontend and querier.
880+
# statistics is logged for every query.
882881
# CLI flag: -frontend.query-stats-enabled
883882
[query_stats_enabled: <boolean> | default = false]
884883

pkg/cortex/modules.go

-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ func (t *Cortex) initQuerier() (serv services.Service, err error) {
306306
}
307307

308308
t.Cfg.Worker.MaxConcurrentRequests = t.Cfg.Querier.MaxConcurrent
309-
t.Cfg.Worker.QueryStatsEnabled = t.Cfg.Frontend.Handler.QueryStatsEnabled
310309
return querier_worker.NewQuerierWorker(t.Cfg.Worker, httpgrpc_server.NewServer(internalQuerierRouter), util.Logger, prometheus.DefaultRegisterer)
311310
}
312311

pkg/frontend/transport/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type HandlerConfig struct {
4545
func (cfg *HandlerConfig) RegisterFlags(f *flag.FlagSet) {
4646
f.DurationVar(&cfg.LogQueriesLongerThan, "frontend.log-queries-longer-than", 0, "Log queries that are slower than the specified duration. Set to 0 to disable. Set to < 0 to enable on all queries.")
4747
f.Int64Var(&cfg.MaxBodySize, "frontend.max-body-size", 10*1024*1024, "Max body size for downstream prometheus.")
48-
f.BoolVar(&cfg.QueryStatsEnabled, "frontend.query-stats-enabled", false, "True to enable query statistics tracking. When enabled, a message with some statistics is logged for every query. This configuration option must be set both on query-frontend and querier.")
48+
f.BoolVar(&cfg.QueryStatsEnabled, "frontend.query-stats-enabled", false, "True to enable query statistics tracking. When enabled, a message with some statistics is logged for every query.")
4949
}
5050

5151
// Handler accepts queries and forwards them to RoundTripper. It can log slow queries,

pkg/frontend/v1/frontend.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,9 @@ func (f *Frontend) Process(server frontendv1pb.Frontend_ProcessServer) error {
190190
errs := make(chan error, 1)
191191
go func() {
192192
err = server.Send(&frontendv1pb.FrontendToClient{
193-
Type: frontendv1pb.HTTP_REQUEST,
194-
HttpRequest: req.request,
193+
Type: frontendv1pb.HTTP_REQUEST,
194+
HttpRequest: req.request,
195+
StatsEnabled: stats.IsEnabled(req.originalCtx),
195196
})
196197
if err != nil {
197198
errs <- err

pkg/frontend/v1/frontendv1pb/frontend.pb.go

+78-29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/frontend/v1/frontendv1pb/frontend.proto

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ enum Type {
2727
message FrontendToClient {
2828
httpgrpc.HTTPRequest httpRequest = 1;
2929
Type type = 2;
30+
31+
// Whether query statistics tracking should be enabled. The response will include
32+
// statistics only when this option is enabled.
33+
bool statsEnabled = 3;
3034
}
3135

3236
message ClientToFrontend {

pkg/frontend/v2/frontend.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,10 @@ type Frontend struct {
7373
}
7474

7575
type frontendRequest struct {
76-
queryID uint64
77-
request *httpgrpc.HTTPRequest
78-
userID string
76+
queryID uint64
77+
request *httpgrpc.HTTPRequest
78+
userID string
79+
statsEnabled bool
7980

8081
cancel context.CancelFunc
8182

@@ -170,9 +171,10 @@ func (f *Frontend) RoundTripGRPC(ctx context.Context, req *httpgrpc.HTTPRequest)
170171
defer cancel()
171172

172173
freq := &frontendRequest{
173-
queryID: f.lastQueryID.Inc(),
174-
request: req,
175-
userID: userID,
174+
queryID: f.lastQueryID.Inc(),
175+
request: req,
176+
userID: userID,
177+
statsEnabled: stats.IsEnabled(ctx),
176178

177179
cancel: cancel,
178180

pkg/frontend/v2/frontend_scheduler_worker.go

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ func (w *frontendSchedulerWorker) schedulerLoop(loop schedulerpb.SchedulerForFro
261261
UserID: req.userID,
262262
HttpRequest: req.request,
263263
FrontendAddress: w.frontendAddr,
264+
StatsEnabled: req.statsEnabled,
264265
})
265266

266267
if err != nil {

pkg/querier/stats/stats.go

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ func FromContext(ctx context.Context) *Stats {
2929
return o.(*Stats)
3030
}
3131

32+
// IsEnabled returns whether stats tracking is enabled in the context.
33+
func IsEnabled(ctx context.Context) bool {
34+
// When query statistics are enabled, the stats object is already initialised
35+
// within the context, so we can just check it.
36+
return FromContext(ctx) != nil
37+
}
38+
3239
// AddWallTime adds some time to the counter.
3340
func (s *Stats) AddWallTime(t time.Duration) {
3441
if s == nil {

pkg/querier/stats/time_middleware.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ func NewWallTimeMiddleware() WallTimeMiddleware {
1616
// Wrap implements middleware.Interface.
1717
func (m WallTimeMiddleware) Wrap(next http.Handler) http.Handler {
1818
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
19-
stats := FromContext(r.Context())
20-
if stats == nil {
19+
if !IsEnabled(r.Context()) {
2120
next.ServeHTTP(w, r)
2221
return
2322
}
2423

2524
startTime := time.Now()
2625
next.ServeHTTP(w, r)
26+
27+
stats := FromContext(r.Context())
2728
stats.AddWallTime(time.Since(startTime))
2829
})
2930
}

pkg/querier/worker/frontend_processor.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ var (
2626

2727
func newFrontendProcessor(cfg Config, handler RequestHandler, log log.Logger) processor {
2828
return &frontendProcessor{
29-
log: log,
30-
handler: handler,
31-
maxMessageSize: cfg.GRPCClientConfig.GRPC.MaxSendMsgSize,
32-
querierID: cfg.QuerierID,
33-
queryStatsEnabled: cfg.QueryStatsEnabled,
29+
log: log,
30+
handler: handler,
31+
maxMessageSize: cfg.GRPCClientConfig.GRPC.MaxSendMsgSize,
32+
querierID: cfg.QuerierID,
3433
}
3534
}
3635

3736
// Handles incoming queries from frontend.
3837
type frontendProcessor struct {
39-
handler RequestHandler
40-
maxMessageSize int
41-
querierID string
42-
queryStatsEnabled bool
38+
handler RequestHandler
39+
maxMessageSize int
40+
querierID string
4341

4442
log log.Logger
4543
}
@@ -86,7 +84,7 @@ func (fp *frontendProcessor) process(c frontendv1pb.Frontend_ProcessClient) erro
8684
// and cancel the query. We don't actually handle queries in parallel
8785
// here, as we're running in lock step with the server - each Recv is
8886
// paired with a Send.
89-
go fp.runRequest(ctx, request.HttpRequest, func(response *httpgrpc.HTTPResponse, stats *stats.Stats) error {
87+
go fp.runRequest(ctx, request.HttpRequest, request.StatsEnabled, func(response *httpgrpc.HTTPResponse, stats *stats.Stats) error {
9088
return c.Send(&frontendv1pb.ClientToFrontend{
9189
HttpResponse: response,
9290
Stats: stats,
@@ -105,9 +103,9 @@ func (fp *frontendProcessor) process(c frontendv1pb.Frontend_ProcessClient) erro
105103
}
106104
}
107105

108-
func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.HTTPRequest, sendHTTPResponse func(response *httpgrpc.HTTPResponse, stats *stats.Stats) error) {
106+
func (fp *frontendProcessor) runRequest(ctx context.Context, request *httpgrpc.HTTPRequest, statsEnabled bool, sendHTTPResponse func(response *httpgrpc.HTTPResponse, stats *stats.Stats) error) {
109107
var stats *querier_stats.Stats
110-
if fp.queryStatsEnabled {
108+
if statsEnabled {
111109
stats, ctx = querier_stats.ContextWithEmptyStats(ctx)
112110
}
113111

0 commit comments

Comments
 (0)