Skip to content

Commit 6a6ca74

Browse files
committed
Move DecorateWithParamName to queryapi
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
1 parent b0871cb commit 6a6ca74

File tree

5 files changed

+34
-39
lines changed

5 files changed

+34
-39
lines changed

pkg/querier/queryapi/query_api.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
v1 "github.com/prometheus/prometheus/web/api/v1"
1818
"github.com/weaveworks/common/httpgrpc"
1919

20-
"github.com/cortexproject/cortex/pkg/querier/tripperware/queryrange"
2120
"github.com/cortexproject/cortex/pkg/util"
2221
"github.com/cortexproject/cortex/pkg/util/api"
2322
)
@@ -52,6 +51,7 @@ func NewQueryAPI(
5251
}
5352

5453
func (c *QueryAPI) RangeQueryHandler(r *http.Request) (result apiFuncResult) {
54+
// TODO(Sungjin1212): Change to emit basic error (not gRPC)
5555
start, err := util.ParseTime(r.FormValue("start"))
5656
if err != nil {
5757
return invalidParamError(err, "start")
@@ -61,7 +61,7 @@ func (c *QueryAPI) RangeQueryHandler(r *http.Request) (result apiFuncResult) {
6161
return invalidParamError(err, "end")
6262
}
6363
if end < start {
64-
return invalidParamError(queryrange.ErrEndBeforeStart, "end")
64+
return invalidParamError(ErrEndBeforeStart, "end")
6565
}
6666

6767
step, err := util.ParseDurationMs(r.FormValue("step"))
@@ -70,13 +70,13 @@ func (c *QueryAPI) RangeQueryHandler(r *http.Request) (result apiFuncResult) {
7070
}
7171

7272
if step <= 0 {
73-
return invalidParamError(queryrange.ErrNegativeStep, "step")
73+
return invalidParamError(ErrNegativeStep, "step")
7474
}
7575

7676
// For safety, limit the number of returned points per timeseries.
7777
// This is sufficient for 60s resolution for a week or 1h resolution for a year.
7878
if (end-start)/step > 11000 {
79-
return apiFuncResult{nil, &apiError{errorBadData, queryrange.ErrStepTooSmall}, nil, nil}
79+
return apiFuncResult{nil, &apiError{errorBadData, ErrStepTooSmall}, nil, nil}
8080
}
8181

8282
ctx := r.Context()
@@ -126,6 +126,7 @@ func (c *QueryAPI) RangeQueryHandler(r *http.Request) (result apiFuncResult) {
126126
}
127127

128128
func (c *QueryAPI) InstantHandler(r *http.Request) (result apiFuncResult) {
129+
// TODO(Sungjin1212): Change to emit basic error (not gRPC)
129130
ts, err := util.ParseTimeParam(r, "time", c.now().Unix())
130131
if err != nil {
131132
return invalidParamError(err, "time")

pkg/querier/queryapi/util.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@ import (
77
"net/http"
88
"time"
99

10+
"github.com/gogo/status"
1011
"github.com/prometheus/prometheus/promql"
1112
"github.com/prometheus/prometheus/util/annotations"
1213
"github.com/weaveworks/common/httpgrpc"
1314

14-
"github.com/cortexproject/cortex/pkg/querier/tripperware/queryrange"
1515
"github.com/cortexproject/cortex/pkg/util"
1616
)
1717

18+
var (
19+
ErrEndBeforeStart = httpgrpc.Errorf(http.StatusBadRequest, "%s", "end timestamp must not be before start time")
20+
ErrNegativeStep = httpgrpc.Errorf(http.StatusBadRequest, "%s", "zero or negative query resolution step widths are not accepted. Try a positive integer")
21+
ErrStepTooSmall = httpgrpc.Errorf(http.StatusBadRequest, "%s", "exceeded maximum resolution of 11,000 points per timeseries. Try decreasing the query resolution (?step=XX)")
22+
)
23+
1824
func extractQueryOpts(r *http.Request) (promql.QueryOpts, error) {
1925
var duration time.Duration
2026

@@ -93,7 +99,7 @@ type apiFunc func(r *http.Request) apiFuncResult
9399

94100
func invalidParamError(err error, parameter string) apiFuncResult {
95101
return apiFuncResult{nil, &apiError{
96-
errorBadData, queryrange.DecorateWithParamName(err, parameter),
102+
errorBadData, DecorateWithParamName(err, parameter),
97103
}, nil, nil}
98104
}
99105

@@ -104,3 +110,11 @@ func convertMsToTime(unixMs int64) time.Time {
104110
func convertMsToDuration(unixMs int64) time.Duration {
105111
return time.Duration(unixMs) * time.Millisecond
106112
}
113+
114+
func DecorateWithParamName(err error, field string) error {
115+
errTmpl := "invalid parameter %q; %v"
116+
if status, ok := status.FromError(err); ok {
117+
return httpgrpc.Errorf(int(status.Code()), errTmpl, field, status.Message())
118+
}
119+
return fmt.Errorf(errTmpl, field, err)
120+
}

pkg/querier/tripperware/instantquery/instant_query.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package instantquery
33
import (
44
"bytes"
55
"context"
6-
"fmt"
76
"io"
87
"net/http"
98
"net/url"
@@ -17,8 +16,8 @@ import (
1716
"github.com/prometheus/common/model"
1817
v1 "github.com/prometheus/prometheus/web/api/v1"
1918
"github.com/weaveworks/common/httpgrpc"
20-
"google.golang.org/grpc/status"
2119

20+
"github.com/cortexproject/cortex/pkg/querier/queryapi"
2221
"github.com/cortexproject/cortex/pkg/querier/stats"
2322
"github.com/cortexproject/cortex/pkg/querier/tripperware"
2423
"github.com/cortexproject/cortex/pkg/util"
@@ -67,7 +66,7 @@ func (c instantQueryCodec) DecodeRequest(_ context.Context, r *http.Request, for
6766
var err error
6867
result.Time, err = util.ParseTimeParam(r, "time", c.now().Unix())
6968
if err != nil {
70-
return nil, decorateWithParamName(err, "time")
69+
return nil, queryapi.DecorateWithParamName(err, "time")
7170
}
7271

7372
result.Query = r.FormValue("query")
@@ -228,14 +227,6 @@ func (instantQueryCodec) MergeResponse(ctx context.Context, req tripperware.Requ
228227
return tripperware.MergeResponse(ctx, true, req, responses...)
229228
}
230229

231-
func decorateWithParamName(err error, field string) error {
232-
errTmpl := "invalid parameter %q; %v"
233-
if status, ok := status.FromError(err); ok {
234-
return httpgrpc.Errorf(int(status.Code()), errTmpl, field, status.Message())
235-
}
236-
return fmt.Errorf(errTmpl, field, err)
237-
}
238-
239230
func marshalResponse(resp *tripperware.PrometheusResponse, acceptHeader string) (string, []byte, error) {
240231
for _, clause := range goautoneg.ParseAccept(acceptHeader) {
241232
if jsonMIMEType.Satisfies(clause) {

pkg/querier/tripperware/queryrange/query_range.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ package queryrange
33
import (
44
"bytes"
55
"context"
6-
"fmt"
76
"io"
87
"net/http"
98
"net/url"
109
"strconv"
1110
"strings"
1211
"time"
1312

14-
"github.com/gogo/status"
1513
jsoniter "github.com/json-iterator/go"
1614
"github.com/opentracing/opentracing-go"
1715
otlog "github.com/opentracing/opentracing-go/log"
@@ -22,6 +20,7 @@ import (
2220
"github.com/cortexproject/cortex/pkg/querier/tripperware"
2321
"github.com/cortexproject/cortex/pkg/util"
2422

23+
"github.com/cortexproject/cortex/pkg/querier/queryapi"
2524
"github.com/cortexproject/cortex/pkg/util/limiter"
2625
"github.com/cortexproject/cortex/pkg/util/spanlogger"
2726
)
@@ -36,9 +35,6 @@ var (
3635
SortMapKeys: true,
3736
ValidateJsonRawMessage: false,
3837
}.Froze()
39-
ErrEndBeforeStart = httpgrpc.Errorf(http.StatusBadRequest, "%s", "end timestamp must not be before start time")
40-
ErrNegativeStep = httpgrpc.Errorf(http.StatusBadRequest, "%s", "zero or negative query resolution step widths are not accepted. Try a positive integer")
41-
ErrStepTooSmall = httpgrpc.Errorf(http.StatusBadRequest, "%s", "exceeded maximum resolution of 11,000 points per timeseries. Try decreasing the query resolution (?step=XX)")
4238

4339
// Name of the cache control header.
4440
cacheControlHeader = "Cache-Control"
@@ -104,31 +100,31 @@ func (c prometheusCodec) DecodeRequest(_ context.Context, r *http.Request, forwa
104100
var err error
105101
result.Start, err = util.ParseTime(r.FormValue("start"))
106102
if err != nil {
107-
return nil, DecorateWithParamName(err, "start")
103+
return nil, queryapi.DecorateWithParamName(err, "start")
108104
}
109105

110106
result.End, err = util.ParseTime(r.FormValue("end"))
111107
if err != nil {
112-
return nil, DecorateWithParamName(err, "end")
108+
return nil, queryapi.DecorateWithParamName(err, "end")
113109
}
114110

115111
if result.End < result.Start {
116-
return nil, ErrEndBeforeStart
112+
return nil, queryapi.ErrEndBeforeStart
117113
}
118114

119115
result.Step, err = util.ParseDurationMs(r.FormValue("step"))
120116
if err != nil {
121-
return nil, DecorateWithParamName(err, "step")
117+
return nil, queryapi.DecorateWithParamName(err, "step")
122118
}
123119

124120
if result.Step <= 0 {
125-
return nil, ErrNegativeStep
121+
return nil, queryapi.ErrNegativeStep
126122
}
127123

128124
// For safety, limit the number of returned points per timeseries.
129125
// This is sufficient for 60s resolution for a week or 1h resolution for a year.
130126
if (result.End-result.Start)/result.Step > 11000 {
131-
return nil, ErrStepTooSmall
127+
return nil, queryapi.ErrStepTooSmall
132128
}
133129

134130
result.Query = r.FormValue("query")
@@ -272,11 +268,3 @@ func (prometheusCodec) EncodeResponse(ctx context.Context, _ *http.Request, res
272268
func encodeDurationMs(d int64) string {
273269
return strconv.FormatFloat(float64(d)/float64(time.Second/time.Millisecond), 'f', -1, 64)
274270
}
275-
276-
func DecorateWithParamName(err error, field string) error {
277-
errTmpl := "invalid parameter %q; %v"
278-
if status, ok := status.FromError(err); ok {
279-
return httpgrpc.Errorf(int(status.Code()), errTmpl, field, status.Message())
280-
}
281-
return fmt.Errorf(errTmpl, field, err)
282-
}

pkg/querier/tripperware/queryrange/query_range_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/weaveworks/common/user"
2222

2323
"github.com/cortexproject/cortex/pkg/cortexpb"
24+
"github.com/cortexproject/cortex/pkg/querier/queryapi"
2425
"github.com/cortexproject/cortex/pkg/querier/tripperware"
2526
)
2627

@@ -55,19 +56,19 @@ func TestRequest(t *testing.T) {
5556
},
5657
{
5758
url: "api/v1/query_range?start=123&end=0",
58-
expectedErr: ErrEndBeforeStart,
59+
expectedErr: queryapi.ErrEndBeforeStart,
5960
},
6061
{
6162
url: "api/v1/query_range?start=123&end=456&step=baz",
6263
expectedErr: httpgrpc.Errorf(http.StatusBadRequest, "invalid parameter \"step\"; cannot parse \"baz\" to a valid duration"),
6364
},
6465
{
6566
url: "api/v1/query_range?start=123&end=456&step=-1",
66-
expectedErr: ErrNegativeStep,
67+
expectedErr: queryapi.ErrNegativeStep,
6768
},
6869
{
6970
url: "api/v1/query_range?start=0&end=11001&step=1",
70-
expectedErr: ErrStepTooSmall,
71+
expectedErr: queryapi.ErrStepTooSmall,
7172
},
7273
} {
7374
tc := tc

0 commit comments

Comments
 (0)