Skip to content

query: handle query.Analyze returning nil gracefully #8199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
### Removed

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

## [v0.38.0](https://github.com/thanos-io/thanos/tree/release-0.38) - 03.04.2025

Expand Down
3 changes: 3 additions & 0 deletions pkg/api/query/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ func extractQueryStats(qry promql.Query) *querypb.QueryStats {
}
if explQry, ok := qry.(engine.ExplainableQuery); ok {
analyze := explQry.Analyze()
if analyze == nil {
return stats
}
stats.SamplesTotal = analyze.TotalSamples()
stats.PeakSamples = analyze.PeakSamples()
}
Expand Down
17 changes: 7 additions & 10 deletions pkg/api/query/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,16 @@ func (qapi *QueryAPI) getQueryExplain(query promql.Query) (*engine.ExplainOutput
return eq.Explain(), nil
}
return nil, &api.ApiError{Typ: api.ErrorBadData, Err: errors.Errorf("Query not explainable")}

}

func (qapi *QueryAPI) parseQueryAnalyzeParam(r *http.Request, query promql.Query) (queryTelemetry, error) {
if r.FormValue(QueryAnalyzeParam) == "true" || r.FormValue(QueryAnalyzeParam) == "1" {
if eq, ok := query.(engine.ExplainableQuery); ok {
return processAnalysis(eq.Analyze()), nil
if analyze := eq.Analyze(); analyze == nil {
return queryTelemetry{}, errors.Errorf("Query: %v not analyzable", query)
} else {
return processAnalysis(analyze), nil
}
}
return queryTelemetry{}, errors.Errorf("Query not analyzable; change engine to 'thanos'")
}
Expand Down Expand Up @@ -530,7 +533,6 @@ func (qapi *QueryAPI) queryExplain(r *http.Request) (interface{}, []error, *api.
var qErr error
qry, qErr = qapi.queryCreate.makeInstantQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, ts)
return qErr

}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}
Expand Down Expand Up @@ -638,14 +640,13 @@ func (qapi *QueryAPI) query(r *http.Request) (interface{}, []error, *api.ApiErro
var qErr error
qry, qErr = qapi.queryCreate.makeInstantQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, ts)
return qErr

}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}

analysis, err := qapi.parseQueryAnalyzeParam(r, qry)
if err != nil {
return nil, nil, apiErr, func() {}
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, func() {}
}

if err := tracing.DoInSpanWithErr(ctx, "query_gate_ismyturn", qapi.gate.Start); err != nil {
Expand Down Expand Up @@ -813,7 +814,6 @@ func (qapi *QueryAPI) queryRangeExplain(r *http.Request) (interface{}, []error,
var qErr error
qry, qErr = qapi.queryCreate.makeRangeQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, start, end, step)
return qErr

}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}
Expand Down Expand Up @@ -946,14 +946,13 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
var qErr error
qry, qErr = qapi.queryCreate.makeRangeQuery(ctx, engineParam, queryable, remoteEndpoints, planOrQuery{query: queryStr}, queryOpts, start, end, step)
return qErr

}); err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorBadData, Err: err}, func() {}
}

analysis, err := qapi.parseQueryAnalyzeParam(r, qry)
if err != nil {
return nil, nil, apiErr, func() {}
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, func() {}
}

if err := tracing.DoInSpanWithErr(ctx, "query_gate_ismyturn", qapi.gate.Start); err != nil {
Expand All @@ -964,7 +963,6 @@ func (qapi *QueryAPI) queryRange(r *http.Request) (interface{}, []error, *api.Ap
var res *promql.Result
tracing.DoInSpan(ctx, "range_query_exec", func(ctx context.Context) {
res = qry.Exec(ctx)

})
beforeRange := time.Now()
if res.Err != nil {
Expand Down Expand Up @@ -1145,7 +1143,6 @@ func (qapi *QueryAPI) series(r *http.Request) (interface{}, []error, *api.ApiErr
nil,
query.NoopSeriesStatsReporter,
).Querier(timestamp.FromTime(start), timestamp.FromTime(end))

if err != nil {
return nil, nil, &api.ApiError{Typ: api.ErrorExec, Err: err}, func() {}
}
Expand Down
Loading