From 1163bf734ec2d1a3b33ff544e562cd34e331de05 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Tue, 11 Feb 2020 17:33:57 -0500 Subject: [PATCH 1/2] Returns httpgrpc error in roundtriper. Signed-off-by: Cyril Tovena --- pkg/querier/queryrange/roundtrip.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/querier/queryrange/roundtrip.go b/pkg/querier/queryrange/roundtrip.go index 01476a5c9103d..cba817903868e 100644 --- a/pkg/querier/queryrange/roundtrip.go +++ b/pkg/querier/queryrange/roundtrip.go @@ -12,6 +12,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/grafana/loki/pkg/logql" "github.com/prometheus/prometheus/pkg/labels" + "github.com/weaveworks/common/httpgrpc" ) // Config is the configuration for the queryrange tripperware @@ -55,7 +56,8 @@ func NewTripperware(cfg Config, log log.Logger, limits Limits) (frontend.Tripper query := params.Get("query") expr, err := logql.ParseExpr(query) if err != nil { - return nil, err + // weavework server uses httpgrpc errors for status code. + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } if _, ok := expr.(logql.SampleExpr); ok { return metricRT.RoundTrip(req) @@ -70,7 +72,7 @@ func NewTripperware(cfg Config, log log.Logger, limits Limits) (frontend.Tripper } filter, err := logSelector.Filter() if err != nil { - return nil, err + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } if filter != nil { return logFilterRT.RoundTrip(req) From cb0a92e14d723c0d7c122beb9f9731a4e5e491ef Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Wed, 12 Feb 2020 11:54:35 -0500 Subject: [PATCH 2/2] Ensure error codes are set correctly in the frontend. Previously we had cases where 400 would endup as 500. We should use httpgrpc.Errorf when possible. Signed-off-by: Cyril Tovena --- pkg/querier/queryrange/codec.go | 11 ++++------- pkg/querier/queryrange/split_by_interval.go | 5 +++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/pkg/querier/queryrange/codec.go b/pkg/querier/queryrange/codec.go index e4c42d08e99ab..87b4575f1d554 100644 --- a/pkg/querier/queryrange/codec.go +++ b/pkg/querier/queryrange/codec.go @@ -47,11 +47,11 @@ func (r *LokiRequest) WithStartEnd(s int64, e int64) queryrange.Request { func (codec) DecodeRequest(_ context.Context, r *http.Request) (queryrange.Request, error) { if err := r.ParseForm(); err != nil { - return nil, err + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } req, err := loghttp.ParseRangeQuery(r) if err != nil { - return nil, err + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } return &LokiRequest{ Query: req.Query, @@ -117,14 +117,11 @@ func (codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang if err := json.Unmarshal(buf, &resp); err != nil { return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error decoding response: %v", err) } - if resp.Status != loghttp.QueryStatusSuccess { - return nil, httpgrpc.Errorf(http.StatusInternalServerError, "error executing request: %v", resp.Status) - } switch string(resp.Data.ResultType) { case loghttp.ResultTypeMatrix: return &LokiPromResponse{ Response: &queryrange.PrometheusResponse{ - Status: loghttp.QueryStatusSuccess, + Status: resp.Status, Data: queryrange.PrometheusData{ ResultType: loghttp.ResultTypeMatrix, Result: toProto(resp.Data.Result.(loghttp.Matrix)), @@ -134,7 +131,7 @@ func (codec) DecodeResponse(ctx context.Context, r *http.Response, req queryrang }, nil case loghttp.ResultTypeStream: return &LokiResponse{ - Status: loghttp.QueryStatusSuccess, + Status: resp.Status, Direction: req.(*LokiRequest).Direction, Limit: req.(*LokiRequest).Limit, Version: uint32(loghttp.GetVersion(req.(*LokiRequest).Path)), diff --git a/pkg/querier/queryrange/split_by_interval.go b/pkg/querier/queryrange/split_by_interval.go index c2380d795af26..c6d83c9aee02d 100644 --- a/pkg/querier/queryrange/split_by_interval.go +++ b/pkg/querier/queryrange/split_by_interval.go @@ -2,12 +2,14 @@ package queryrange import ( "context" + "net/http" "time" "github.com/cortexproject/cortex/pkg/querier/queryrange" "github.com/grafana/loki/pkg/logproto" "github.com/opentracing/opentracing-go" otlog "github.com/opentracing/opentracing-go/log" + "github.com/weaveworks/common/httpgrpc" "github.com/weaveworks/common/user" ) @@ -135,7 +137,7 @@ func (h *splitByInterval) Do(ctx context.Context, r queryrange.Request) (queryra userid, err := user.ExtractOrgID(ctx) if err != nil { - return nil, err + return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error()) } interval := h.limits.QuerySplitDuration(userid) @@ -169,7 +171,6 @@ func (h *splitByInterval) Do(ctx context.Context, r queryrange.Request) (queryra if err != nil { return nil, err } - return h.merger.MergeResponse(resps...) }