Skip to content

Commit

Permalink
*: wire new Engine/Explain fields in query-frontend (thanos-io#6433)
Browse files Browse the repository at this point in the history
- Pass Engine/Explain fields in query-frontend codecs
- Add Engine field to QFE cache key
- Add e2e tests for all cases

Signed-off-by: Giedrius Statkevičius <giedrius.statkevicius@vinted.com>
  • Loading branch information
GiedriusS authored and pedro-stanaka committed Jun 27, 2023
1 parent 2f37bea commit d2f4de9
Show file tree
Hide file tree
Showing 20 changed files with 766 additions and 183 deletions.
80 changes: 50 additions & 30 deletions internal/cortex/querier/queryrange/query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,21 @@ func (prometheusCodec) MergeResponse(_ Request, responses ...Response) (Response
// Merge the responses.
sort.Sort(byFirstTime(promResponses))

var explanation *Explanation
for i := range promResponses {
if promResponses[i].Data.GetExplanation() != nil {
explanation = promResponses[i].Data.GetExplanation()
break
}
}

response := PrometheusResponse{
Status: StatusSuccess,
Data: PrometheusData{
ResultType: model.ValMatrix.String(),
Result: matrixMerge(promResponses),
Stats: StatsMerge(responses),
ResultType: model.ValMatrix.String(),
Result: matrixMerge(promResponses),
Stats: StatsMerge(responses),
Explanation: explanation,
},
}

Expand Down Expand Up @@ -524,16 +533,19 @@ func (s *StringSample) UnmarshalJSON(b []byte) error {
// UnmarshalJSON implements json.Unmarshaler.
func (s *PrometheusInstantQueryData) UnmarshalJSON(data []byte) error {
var queryData struct {
ResultType string `json:"resultType"`
Result jsoniter.RawMessage `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
ResultType string `json:"resultType"`
Result jsoniter.RawMessage `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
Explanation *Explanation `json:"explanation,omitempty"`
}

if err := json.Unmarshal(data, &queryData); err != nil {
return err
}

s.ResultType = queryData.ResultType
s.Stats = queryData.Stats
s.Explanation = queryData.Explanation
switch s.ResultType {
case model.ValVector.String():
var result struct {
Expand Down Expand Up @@ -593,46 +605,54 @@ func (s *PrometheusInstantQueryData) MarshalJSON() ([]byte, error) {
switch s.ResultType {
case model.ValVector.String():
res := struct {
ResultType string `json:"resultType"`
Data []*Sample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
ResultType string `json:"resultType"`
Data []*Sample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
Explanation *Explanation `json:"explanation,omitempty"`
}{
ResultType: s.ResultType,
Data: s.Result.GetVector().Samples,
Stats: s.Stats,
ResultType: s.ResultType,
Data: s.Result.GetVector().Samples,
Stats: s.Stats,
Explanation: s.Explanation,
}
return json.Marshal(res)
case model.ValMatrix.String():
res := struct {
ResultType string `json:"resultType"`
Data []*SampleStream `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
ResultType string `json:"resultType"`
Data []*SampleStream `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
Explanation *Explanation `json:"explanation,omitempty"`
}{
ResultType: s.ResultType,
Data: s.Result.GetMatrix().SampleStreams,
Stats: s.Stats,
ResultType: s.ResultType,
Data: s.Result.GetMatrix().SampleStreams,
Stats: s.Stats,
Explanation: s.Explanation,
}
return json.Marshal(res)
case model.ValScalar.String():
res := struct {
ResultType string `json:"resultType"`
Data *cortexpb.Sample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
ResultType string `json:"resultType"`
Data *cortexpb.Sample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
Explanation *Explanation `json:"explanation,omitempty"`
}{
ResultType: s.ResultType,
Data: s.Result.GetScalar(),
Stats: s.Stats,
ResultType: s.ResultType,
Data: s.Result.GetScalar(),
Stats: s.Stats,
Explanation: s.Explanation,
}
return json.Marshal(res)
case model.ValString.String():
res := struct {
ResultType string `json:"resultType"`
Data *StringSample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
ResultType string `json:"resultType"`
Data *StringSample `json:"result"`
Stats *PrometheusResponseStats `json:"stats,omitempty"`
Explanation *Explanation `json:"explanation,omitempty"`
}{
ResultType: s.ResultType,
Data: s.Result.GetStringSample(),
Stats: s.Stats,
ResultType: s.ResultType,
Data: s.Result.GetStringSample(),
Stats: s.Stats,
Explanation: s.Explanation,
}
return json.Marshal(res)
default:
Expand Down
2 changes: 1 addition & 1 deletion internal/cortex/querier/queryrange/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestResponseWithStats(t *testing.T) {
expected *PrometheusResponse
}{
{
body: `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}}}}`,
body: `{"status":"success","data":{"resultType":"matrix","result":[{"metric":{"foo":"bar"},"values":[[1536673680,"137"],[1536673780,"137"]]}],"stats":{"samples":{"totalQueryableSamples":10,"totalQueryableSamplesPerStep":[[1536673680,5],[1536673780,5]]}},"explanation":null}}`,
expected: &PrometheusResponse{
Status: "success",
Data: PrometheusData{
Expand Down
Loading

0 comments on commit d2f4de9

Please sign in to comment.