Skip to content

Commit 521a7dc

Browse files
committed
Move grpc wrap functions to tripperware
Signed-off-by: SungJin1212 <[email protected]>
1 parent fa5dd6f commit 521a7dc

File tree

6 files changed

+45
-48
lines changed

6 files changed

+45
-48
lines changed

pkg/querier/tripperware/instantquery/instant_query.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func NewInstantQueryCodec(compressionStr string, defaultCodecTypeStr string) ins
6363
func (c instantQueryCodec) DecodeRequest(_ context.Context, r *http.Request, forwardHeaders []string) (tripperware.Request, error) {
6464
result := tripperware.PrometheusRequest{Headers: map[string][]string{}}
6565
var err error
66-
result.Time, err = api.ParseTimeParamMillis(r, "time", c.now())
66+
result.Time, err = tripperware.ParseTimeParamMillis(r, "time", c.now())
6767
if err != nil {
6868
return nil, api.DecorateWithParamName(err, "time")
6969
}

pkg/querier/tripperware/query_attribute_matcher.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func matchAttributeForMetadataQuery(attribute validation.QueryAttribute, op stri
172172
}
173173
}
174174

175-
startTime, _ := api.ParseTimeMillis(r.FormValue("start"))
176-
endTime, _ := api.ParseTimeMillis(r.FormValue("end"))
175+
startTime, _ := ParseTimeMillis(r.FormValue("start"))
176+
endTime, _ := ParseTimeMillis(r.FormValue("end"))
177177

178178
if attribute.TimeWindow.Start != 0 || attribute.TimeWindow.End != 0 {
179179
matched = true
@@ -236,8 +236,7 @@ func isWithinTimeRangeAttribute(limit validation.TimeRangeLimit, startTime, endT
236236
}
237237

238238
func isWithinQueryStepLimit(queryStepLimit validation.QueryStepLimit, r *http.Request) bool {
239-
240-
step, err := api.ParseDurationMillis(r.FormValue("step"))
239+
step, err := ParseDurationMillis(r.FormValue("step"))
241240
if err != nil {
242241
return false
243242
}

pkg/querier/tripperware/queryrange/query_range.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func (c prometheusCodec) MergeResponse(ctx context.Context, req tripperware.Requ
9696
func (c prometheusCodec) DecodeRequest(_ context.Context, r *http.Request, forwardHeaders []string) (tripperware.Request, error) {
9797
result := tripperware.PrometheusRequest{Headers: map[string][]string{}}
9898
var err error
99-
result.Start, err = api.ParseTimeMillis(r.FormValue("start"))
99+
result.Start, err = tripperware.ParseTimeMillis(r.FormValue("start"))
100100
if err != nil {
101101
return nil, api.DecorateWithParamName(err, "start")
102102
}
103103

104-
result.End, err = api.ParseTimeMillis(r.FormValue("end"))
104+
result.End, err = tripperware.ParseTimeMillis(r.FormValue("end"))
105105
if err != nil {
106106
return nil, api.DecorateWithParamName(err, "end")
107107
}
@@ -110,7 +110,7 @@ func (c prometheusCodec) DecodeRequest(_ context.Context, r *http.Request, forwa
110110
return nil, api.ErrEndBeforeStart
111111
}
112112

113-
result.Step, err = api.ParseDurationMillis(r.FormValue("step"))
113+
result.Step, err = tripperware.ParseDurationMillis(r.FormValue("step"))
114114
if err != nil {
115115
return nil, api.DecorateWithParamName(err, "step")
116116
}

pkg/querier/tripperware/util.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package tripperware
33
import (
44
"context"
55
"net/http"
6+
"time"
67

78
"github.com/weaveworks/common/httpgrpc"
89

910
"github.com/cortexproject/cortex/pkg/querier/stats"
1011
"github.com/cortexproject/cortex/pkg/tenant"
12+
"github.com/cortexproject/cortex/pkg/util"
13+
"github.com/cortexproject/cortex/pkg/util/api"
1114
"github.com/cortexproject/cortex/pkg/util/validation"
1215
)
1316

@@ -87,3 +90,30 @@ func SetQueryResponseStats(a *PrometheusResponse, queryStats *stats.QueryStats)
8790
}
8891
}
8992
}
93+
94+
func ParseTimeParamMillis(r *http.Request, paramName string, defaultValue time.Time) (int64, error) {
95+
t, err := api.ParseTimeParam(r, paramName, defaultValue)
96+
if err != nil {
97+
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
98+
}
99+
100+
return util.TimeToMillis(t), nil
101+
}
102+
103+
func ParseTimeMillis(s string) (int64, error) {
104+
t, err := api.ParseTime(s)
105+
if err != nil {
106+
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
107+
}
108+
109+
return util.TimeToMillis(t), nil
110+
}
111+
112+
func ParseDurationMillis(s string) (int64, error) {
113+
d, err := api.ParseDuration(s)
114+
if err != nil {
115+
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
116+
}
117+
118+
return int64(d / (time.Millisecond / time.Nanosecond)), nil
119+
}

pkg/util/api/parse.go

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ import (
1111
"github.com/prometheus/common/model"
1212
"github.com/prometheus/prometheus/promql"
1313
"github.com/prometheus/prometheus/promql/parser"
14-
"github.com/weaveworks/common/httpgrpc"
15-
16-
"github.com/cortexproject/cortex/pkg/util"
1714
)
1815

1916
var (
@@ -62,15 +59,6 @@ func ParseTime(s string) (time.Time, error) {
6259
return time.Time{}, fmt.Errorf("cannot parse %q to a valid timestamp", s)
6360
}
6461

65-
func ParseTimeMillis(s string) (int64, error) {
66-
t, err := ParseTime(s)
67-
if err != nil {
68-
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
69-
}
70-
71-
return util.TimeToMillis(t), nil
72-
}
73-
7462
func ParseDuration(s string) (time.Duration, error) {
7563
if d, err := strconv.ParseFloat(s, 64); err == nil {
7664
ts := d * float64(time.Second)
@@ -85,15 +73,6 @@ func ParseDuration(s string) (time.Duration, error) {
8573
return 0, fmt.Errorf("cannot parse %q to a valid duration", s)
8674
}
8775

88-
func ParseDurationMillis(s string) (int64, error) {
89-
d, err := ParseDuration(s)
90-
if err != nil {
91-
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
92-
}
93-
94-
return int64(d / (time.Millisecond / time.Nanosecond)), nil
95-
}
96-
9776
func ParseTimeParam(r *http.Request, paramName string, defaultValue time.Time) (time.Time, error) {
9877
val := r.FormValue(paramName)
9978
if val == "" {
@@ -106,30 +85,21 @@ func ParseTimeParam(r *http.Request, paramName string, defaultValue time.Time) (
10685
return result, nil
10786
}
10887

109-
func ParseTimeParamMillis(r *http.Request, paramName string, defaultValue time.Time) (int64, error) {
110-
t, err := ParseTimeParam(r, paramName, defaultValue)
111-
if err != nil {
112-
return 0, httpgrpc.Errorf(http.StatusBadRequest, "%s", err.Error())
113-
}
114-
115-
return util.TimeToMillis(t), nil
116-
}
117-
11888
// FindMinMaxTime returns the time in milliseconds of the earliest and latest point in time the statement will try to process.
11989
// This takes into account offsets, @ modifiers, and range selectors.
12090
// If the expression does not select series, then FindMinMaxTime returns (0, 0).
12191
func FindMinMaxTime(r *http.Request, expr parser.Expr, lookbackDelta time.Duration, now time.Time) (int64, int64) {
12292
isQuery := strings.HasSuffix(r.URL.Path, "/query")
12393

124-
var startTime, endTime int64
94+
var startTime, endTime time.Time
12595
if isQuery {
126-
if t, err := ParseTimeParamMillis(r, "time", now); err == nil {
96+
if t, err := ParseTimeParam(r, "time", now); err == nil {
12797
startTime = t
12898
endTime = t
12999
}
130100
} else {
131-
if st, err := ParseTimeMillis(r.FormValue("start")); err == nil {
132-
if et, err := ParseTimeMillis(r.FormValue("end")); err == nil {
101+
if st, err := ParseTime(r.FormValue("start")); err == nil {
102+
if et, err := ParseTime(r.FormValue("end")); err == nil {
133103
startTime = st
134104
endTime = et
135105
}
@@ -138,8 +108,8 @@ func FindMinMaxTime(r *http.Request, expr parser.Expr, lookbackDelta time.Durati
138108

139109
es := &parser.EvalStmt{
140110
Expr: expr,
141-
Start: util.TimeFromMillis(startTime),
142-
End: util.TimeFromMillis(endTime),
111+
Start: startTime,
112+
End: endTime,
143113
LookbackDelta: lookbackDelta,
144114
}
145115

pkg/util/api/parse_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"github.com/prometheus/prometheus/promql/parser"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13-
14-
"github.com/cortexproject/cortex/pkg/util"
1513
)
1614

1715
func TestFindMinMaxTime(t *testing.T) {
@@ -106,13 +104,13 @@ func TestParseTime(t *testing.T) {
106104
}
107105

108106
for _, test := range tests {
109-
ts, err := ParseTimeMillis(test.input)
107+
ts, err := ParseTime(test.input)
110108
if test.fail {
111109
require.Error(t, err)
112110
continue
113111
}
114112

115113
require.NoError(t, err)
116-
assert.Equal(t, util.TimeToMillis(test.result), ts)
114+
assert.Equal(t, test.result.UnixMilli(), ts.UnixMilli())
117115
}
118116
}

0 commit comments

Comments
 (0)