Skip to content

Commit 01c138f

Browse files
Saumya40-codesNaman-B-Parlecha
authored andcommitted
query: handle query.Analyze returning nil gracefully (thanos-io#8199)
* fix: handle analyze returning nil gracefully Signed-off-by: Saumya Shah <[email protected]> * update CHANGELOG.md Signed-off-by: Saumya Shah <[email protected]> * fix format Signed-off-by: Saumya Shah <[email protected]> --------- Signed-off-by: Saumya Shah <[email protected]>
1 parent 2944a4d commit 01c138f

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
2828
### Removed
2929

3030
### Fixed
31+
- [#8199](https://github.com/thanos-io/thanos/pull/8199) Query: handle panics or nil pointer dereference in querier gracefully when query analyze returns nil
3132

3233
- [#8211](https://github.com/thanos-io/thanos/pull/8211) Query: fix panic on nested partial response in distributed instant query
3334
- [#8216](https://github.com/thanos-io/thanos/pull/8216) Query/Receive: fix iter race between `next()` and `stop()` introduced in https://github.com/thanos-io/thanos/pull/7821.

pkg/api/query/grpc.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ func extractQueryStats(qry promql.Query) *querypb.QueryStats {
273273
}
274274
if explQry, ok := qry.(engine.ExplainableQuery); ok {
275275
analyze := explQry.Analyze()
276+
if analyze == nil {
277+
return stats
278+
}
276279
stats.SamplesTotal = analyze.TotalSamples()
277280
stats.PeakSamples = analyze.PeakSamples()
278281
}

pkg/api/query/v1.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,6 @@ func (qapi *QueryAPI) getQueryExplain(query promql.Query) (*engine.ExplainOutput
407407
return eq.Explain(), nil
408408
}
409409
return nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf("Query not explainable")}
410-
411410
}
412411

413412
func (qapi *QueryAPI) parseQueryAnalyzeParam(r *http.Request) bool {
@@ -416,7 +415,11 @@ func (qapi *QueryAPI) parseQueryAnalyzeParam(r *http.Request) bool {
416415

417416
func analyzeQueryOutput(query promql.Query, engineType PromqlEngineType) (queryTelemetry, error) {
418417
if eq, ok := query.(engine.ExplainableQuery); ok {
419-
return processAnalysis(eq.Analyze()), nil
418+
if analyze := eq.Analyze(); analyze != nil {
419+
return processAnalysis(analyze), nil
420+
} else {
421+
return queryTelemetry{}, errors.Errorf("Query: %v not analyzable", query)
422+
}
420423
}
421424

422425
var warning error
@@ -539,7 +542,6 @@ func (qapi *QueryAPI) queryExplain(r *http.Request) (interface{}, []error, *api.
539542
var qErr error
540543
qry, qErr = qapi.queryCreate.makeInstantQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, ts)
541544
return qErr
542-
543545
}); err != nil {
544546
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
545547
}
@@ -648,7 +650,6 @@ func (qapi *QueryAPI) query(r *http.Request) (interface{}, []error, *api.ApiErro
648650
var qErr error
649651
qry, qErr = qapi.queryCreate.makeInstantQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, ts)
650652
return qErr
651-
652653
}); err != nil {
653654
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
654655
}
@@ -827,7 +828,6 @@ func (qapi *QueryAPI) queryRangeExplain(r *http.Request) (interface{}, []error,
827828
var qErr error
828829
qry, qErr = qapi.queryCreate.makeRangeQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, start, end, step)
829830
return qErr
830-
831831
}); err != nil {
832832
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
833833
}
@@ -961,7 +961,6 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
961961
var qErr error
962962
qry, qErr = qapi.queryCreate.makeRangeQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, start, end, step)
963963
return qErr
964-
965964
}); err != nil {
966965
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
967966
}
@@ -1164,7 +1163,6 @@ func (qapi *QueryAPI) series(r *http.Request) (interface{}, []error, *api.ApiErr
11641163
nil,
11651164
query.NoopSeriesStatsReporter,
11661165
).Querier(timestamp.FromTime(start), timestamp.FromTime(end))
1167-
11681166
if err != nil {
11691167
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, func() {}
11701168
}

0 commit comments

Comments
 (0)