Skip to content

Commit 48a686a

Browse files
Update query API to support timeouts (#1014)
* Add timeout parameter for queries Signed-off-by: Joseph Woodward <[email protected]> * Update api/prometheus/v1/api.go Co-authored-by: Kemal Akkoyun <[email protected]> Signed-off-by: Joseph Woodward <[email protected]> * Update api/prometheus/v1/api.go Co-authored-by: Kemal Akkoyun <[email protected]> Signed-off-by: Joseph Woodward <[email protected]> * Pass timeout as stringified time.Duration instead of millisecond value Signed-off-by: Joseph Woodward <[email protected]> * Update QueryRange API to support timeouts Signed-off-by: Joseph Woodward <[email protected]> * Add timeout to test request params Signed-off-by: Joseph Woodward <[email protected]> Co-authored-by: Kemal Akkoyun <[email protected]>
1 parent 11ee9ad commit 48a686a

File tree

3 files changed

+53
-18
lines changed

3 files changed

+53
-18
lines changed

api/prometheus/v1/api.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ type API interface {
238238
// LabelValues performs a query for the values of the given label, time range and matchers.
239239
LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error)
240240
// Query performs a query for the given time.
241-
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
241+
Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error)
242242
// QueryRange performs a query for the given range.
243-
QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error)
243+
QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error)
244244
// QueryExemplars performs a query for exemplars by the given query and time range.
245245
QueryExemplars(ctx context.Context, query string, startTime time.Time, endTime time.Time) ([]ExemplarQueryResult, error)
246246
// Buildinfo returns various build information properties about the Prometheus server
@@ -818,10 +818,33 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin
818818
return labelValues, w, json.Unmarshal(body, &labelValues)
819819
}
820820

821-
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error) {
821+
type apiOptions struct {
822+
timeout time.Duration
823+
}
824+
825+
type Option func(c *apiOptions)
826+
827+
func WithTimeout(timeout time.Duration) Option {
828+
return func(o *apiOptions) {
829+
o.timeout = timeout
830+
}
831+
}
832+
833+
func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {
834+
822835
u := h.client.URL(epQuery, nil)
823836
q := u.Query()
824837

838+
opt := &apiOptions{}
839+
for _, o := range opts {
840+
o(opt)
841+
}
842+
843+
d := opt.timeout
844+
if d > 0 {
845+
q.Set("timeout", d.String())
846+
}
847+
825848
q.Set("query", query)
826849
if !ts.IsZero() {
827850
q.Set("time", formatTime(ts))
@@ -836,7 +859,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.
836859
return model.Value(qres.v), warnings, json.Unmarshal(body, &qres)
837860
}
838861

839-
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error) {
862+
func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range, opts ...Option) (model.Value, Warnings, error) {
840863
u := h.client.URL(epQueryRange, nil)
841864
q := u.Query()
842865

@@ -845,6 +868,16 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
845868
q.Set("end", formatTime(r.End))
846869
q.Set("step", strconv.FormatFloat(r.Step.Seconds(), 'f', -1, 64))
847870

871+
opt := &apiOptions{}
872+
for _, o := range opts {
873+
o(opt)
874+
}
875+
876+
d := opt.timeout
877+
if d > 0 {
878+
q.Set("timeout", d.String())
879+
}
880+
848881
_, body, warnings, err := h.client.DoGetFallback(ctx, u, q)
849882
if err != nil {
850883
return nil, warnings, err

api/prometheus/v1/api_test.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,15 @@ func TestAPIs(t *testing.T) {
170170
}
171171
}
172172

173-
doQuery := func(q string, ts time.Time) func() (interface{}, Warnings, error) {
173+
doQuery := func(q string, ts time.Time, opts ...Option) func() (interface{}, Warnings, error) {
174174
return func() (interface{}, Warnings, error) {
175-
return promAPI.Query(context.Background(), q, ts)
175+
return promAPI.Query(context.Background(), q, ts, opts...)
176176
}
177177
}
178178

179-
doQueryRange := func(q string, rng Range) func() (interface{}, Warnings, error) {
179+
doQueryRange := func(q string, rng Range, opts ...Option) func() (interface{}, Warnings, error) {
180180
return func() (interface{}, Warnings, error) {
181-
return promAPI.QueryRange(context.Background(), q, rng)
181+
return promAPI.QueryRange(context.Background(), q, rng, opts...)
182182
}
183183
}
184184

@@ -246,7 +246,7 @@ func TestAPIs(t *testing.T) {
246246

247247
queryTests := []apiTest{
248248
{
249-
do: doQuery("2", testTime),
249+
do: doQuery("2", testTime, WithTimeout(5*time.Second)),
250250
inRes: &queryResult{
251251
Type: model.ValScalar,
252252
Result: &model.Scalar{
@@ -258,8 +258,9 @@ func TestAPIs(t *testing.T) {
258258
reqMethod: "POST",
259259
reqPath: "/api/v1/query",
260260
reqParam: url.Values{
261-
"query": []string{"2"},
262-
"time": []string{testTime.Format(time.RFC3339Nano)},
261+
"query": []string{"2"},
262+
"time": []string{testTime.Format(time.RFC3339Nano)},
263+
"timeout": []string{(5 * time.Second).String()},
263264
},
264265
res: &model.Scalar{
265266
Value: 2,
@@ -365,16 +366,17 @@ func TestAPIs(t *testing.T) {
365366
Start: testTime.Add(-time.Minute),
366367
End: testTime,
367368
Step: time.Minute,
368-
}),
369+
}, WithTimeout(5*time.Second)),
369370
inErr: fmt.Errorf("some error"),
370371

371372
reqMethod: "POST",
372373
reqPath: "/api/v1/query_range",
373374
reqParam: url.Values{
374-
"query": []string{"2"},
375-
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
376-
"end": []string{testTime.Format(time.RFC3339Nano)},
377-
"step": []string{time.Minute.String()},
375+
"query": []string{"2"},
376+
"start": []string{testTime.Add(-time.Minute).Format(time.RFC3339Nano)},
377+
"end": []string{testTime.Format(time.RFC3339Nano)},
378+
"step": []string{time.Minute.String()},
379+
"timeout": []string{(5 * time.Second).String()},
378380
},
379381
err: fmt.Errorf("some error"),
380382
},

api/prometheus/v1/example_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func ExampleAPI_query() {
3939
v1api := v1.NewAPI(client)
4040
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
4141
defer cancel()
42-
result, warnings, err := v1api.Query(ctx, "up", time.Now())
42+
result, warnings, err := v1api.Query(ctx, "up", time.Now(), v1.WithTimeout(5*time.Second))
4343
if err != nil {
4444
fmt.Printf("Error querying Prometheus: %v\n", err)
4545
os.Exit(1)
@@ -67,7 +67,7 @@ func ExampleAPI_queryRange() {
6767
End: time.Now(),
6868
Step: time.Minute,
6969
}
70-
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r)
70+
result, warnings, err := v1api.QueryRange(ctx, "rate(prometheus_tsdb_head_samples_appended_total[5m])", r, v1.WithTimeout(5*time.Second))
7171
if err != nil {
7272
fmt.Printf("Error querying Prometheus: %v\n", err)
7373
os.Exit(1)

0 commit comments

Comments
 (0)