Skip to content
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

Returning Prometheus Warnings #5916

Merged
merged 2 commits into from
May 2, 2024
Merged
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 @@ -4,6 +4,7 @@
* [CHANGE] Ruler: Remove `experimental.ruler.api-enable-rules-backup` flag and use `ruler.ring.replication-factor` to check if rules backup is enabled

* [ENHANCEMENT] Query Frontend/Querier: Added store gateway postings touched count and touched size in Querier stats and log in Query Frontend. #5892
* [ENHANCEMENT] Query Frontend/Querier: Returns `warnings` on prometheus query responses. #5916
* [CHANGE] Upgrade Dockerfile Node version from 14x to 18x. #5906

## 1.17.0 in progress
Expand Down
10 changes: 8 additions & 2 deletions pkg/querier/tripperware/instantquery/instant_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
promqlparser "github.com/prometheus/prometheus/promql/parser"
"github.com/thanos-io/thanos/pkg/strutil"
"github.com/weaveworks/common/httpgrpc"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -260,8 +261,12 @@ func (instantQueryCodec) MergeResponse(ctx context.Context, req tripperware.Requ
}

promResponses := make([]*PrometheusInstantQueryResponse, 0, len(responses))
warnings := make([][]string, 0, len(responses))
for _, resp := range responses {
promResponses = append(promResponses, resp.(*PrometheusInstantQueryResponse))
if w := resp.(*PrometheusInstantQueryResponse).Warnings; w != nil {
warnings = append(warnings, w)
}
}

var data PrometheusInstantQueryData
Expand Down Expand Up @@ -303,8 +308,9 @@ func (instantQueryCodec) MergeResponse(ctx context.Context, req tripperware.Requ
}

res := &PrometheusInstantQueryResponse{
Status: queryrange.StatusSuccess,
Data: data,
Status: queryrange.StatusSuccess,
Data: data,
Warnings: strutil.MergeUnsortedSlices(warnings...),
}
return res, nil
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/querier/tripperware/instantquery/instant_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,15 @@ func TestMergeResponse(t *testing.T) {
},
expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]},{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]}]}}`,
},
{
name: "merge with warnings.",
req: &PrometheusRequest{Query: "topk(10, up) by(job)"},
resps: []string{
`{"status":"success","warnings":["warning1","warning2"],"data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]}]}}`,
`{"status":"success","warnings":["warning1","warning3"],"data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]}]}}`,
},
expectedResp: `{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","job":"foo"},"value":[1,"1"]},{"metric":{"__name__":"up","job":"bar"},"value":[1,"2"]}]},"warnings":["warning1","warning2","warning3"]}`,
},
{
name: "merge two responses with stats",
req: defaultReq,
Expand Down
153 changes: 110 additions & 43 deletions pkg/querier/tripperware/instantquery/instantquery.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/querier/tripperware/instantquery/instantquery.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ message PrometheusInstantQueryResponse {
string ErrorType = 3 [(gogoproto.jsontag) = "errorType,omitempty"];
string Error = 4 [(gogoproto.jsontag) = "error,omitempty"];
repeated tripperware.PrometheusResponseHeader Headers = 5 [(gogoproto.jsontag) = "-"];
repeated string Warnings = 6 [(gogoproto.jsontag) = "warnings,omitempty"];
}

message PrometheusInstantQueryData {
Expand Down
6 changes: 6 additions & 0 deletions pkg/querier/tripperware/queryrange/query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
otlog "github.com/opentracing/opentracing-go/log"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/thanos-io/thanos/pkg/strutil"
"github.com/weaveworks/common/httpgrpc"

"github.com/cortexproject/cortex/pkg/querier/tripperware"
Expand Down Expand Up @@ -135,8 +136,12 @@ func (c prometheusCodec) MergeResponse(ctx context.Context, _ tripperware.Reques
}

promResponses := make([]*PrometheusResponse, 0, len(responses))
warnings := make([][]string, 0, len(responses))
for _, res := range responses {
promResponses = append(promResponses, res.(*PrometheusResponse))
if w := res.(*PrometheusResponse).Warnings; w != nil {
warnings = append(warnings, w)
}
}

// Merge the responses.
Expand All @@ -153,6 +158,7 @@ func (c prometheusCodec) MergeResponse(ctx context.Context, _ tripperware.Reques
Result: sampleStreams,
Stats: statsMerge(c.sharded, promResponses),
},
Warnings: strutil.MergeUnsortedSlices(warnings...),
}

return &response, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func TestRoundTrip(t *testing.T) {
var err error
if r.RequestURI == query {
_, err = w.Write([]byte(responseBody))
} else if r.RequestURI == queryWithWarnings {
_, err = w.Write([]byte(responseBodyWithWarnings))
} else {
_, err = w.Write([]byte("bar"))
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/querier/tripperware/queryrange/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ func TestRequest(t *testing.T) {
func TestResponse(t *testing.T) {
t.Parallel()
r := *parsedResponse
rWithWarnings := *parsedResponseWithWarnings
r.Headers = respHeaders
rWithWarnings.Headers = respHeaders
for i, tc := range []struct {
body string
expected *PrometheusResponse
Expand All @@ -102,6 +104,10 @@ func TestResponse(t *testing.T) {
body: responseBody,
expected: &r,
},
{
body: responseBodyWithWarnings,
expected: &rWithWarnings,
},
{
body: responseBody,
cancelCtxBeforeDecode: true,
Expand Down Expand Up @@ -390,6 +396,28 @@ func TestMergeAPIResponses(t *testing.T) {
},
},
},
{
name: "Merge response with warnings.",
input: []tripperware.Response{
mustParse(t, `{"status":"success","warnings":["warning1","warning2"],"data":{"resultType":"matrix","result":[{"metric":{"a":"b","c":"d"},"values":[[1,"1"]]}]}}`),
mustParse(t, `{"status":"success","warnings":["warning1","warning3"],"data":{"resultType":"matrix","result":[{"metric":{"a":"b","c":"d"},"values":[[1,"1"]]}]}}`),
},
expected: &PrometheusResponse{
Status: StatusSuccess,
Warnings: []string{"warning1", "warning2", "warning3"},
Data: PrometheusData{
ResultType: matrix,
Result: []tripperware.SampleStream{
{
Labels: []cortexpb.LabelAdapter{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}},
Samples: []cortexpb.Sample{
{Value: 1, TimestampMs: 1000},
},
},
},
},
},
},
{
name: "Merging of samples where there is complete overlap.",
input: []tripperware.Response{
Expand Down
Loading
Loading