Skip to content

Commit

Permalink
Add timeout parameter for queries
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Woodward <joseph.woodward@xeuse.com>
  • Loading branch information
josephwoodward committed Apr 13, 2022
1 parent 11ee9ad commit 80099df
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
27 changes: 25 additions & 2 deletions api/prometheus/v1/api.go
Expand Up @@ -238,7 +238,7 @@ type API interface {
// LabelValues performs a query for the values of the given label, time range and matchers.
LabelValues(ctx context.Context, label string, matches []string, startTime time.Time, endTime time.Time) (model.LabelValues, Warnings, error)
// Query performs a query for the given time.
Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error)
Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error)
// QueryRange performs a query for the given range.
QueryRange(ctx context.Context, query string, r Range) (model.Value, Warnings, error)
// QueryExemplars performs a query for exemplars by the given query and time range.
Expand Down Expand Up @@ -818,10 +818,33 @@ func (h *httpAPI) LabelValues(ctx context.Context, label string, matches []strin
return labelValues, w, json.Unmarshal(body, &labelValues)
}

func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.Value, Warnings, error) {
type apiOptions struct {
timeout time.Duration
}

type Option func(c *apiOptions)

func WithTimeout(timeout time.Duration) Option {
return func(o *apiOptions) {
o.timeout = timeout
}
}

func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time, opts ...Option) (model.Value, Warnings, error) {

u := h.client.URL(epQuery, nil)
q := u.Query()

opt := &apiOptions{}
for _, o := range opts {
o(opt)
}

t := opt.timeout.Milliseconds()
if t > 0 {
q.Set("timeout", fmt.Sprint(t))
}

q.Set("query", query)
if !ts.IsZero() {
q.Set("time", formatTime(ts))
Expand Down
6 changes: 3 additions & 3 deletions api/prometheus/v1/api_test.go
Expand Up @@ -170,9 +170,9 @@ func TestAPIs(t *testing.T) {
}
}

doQuery := func(q string, ts time.Time) func() (interface{}, Warnings, error) {
doQuery := func(q string, ts time.Time, opts ...Option) func() (interface{}, Warnings, error) {
return func() (interface{}, Warnings, error) {
return promAPI.Query(context.Background(), q, ts)
return promAPI.Query(context.Background(), q, ts, opts...)
}
}

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

queryTests := []apiTest{
{
do: doQuery("2", testTime),
do: doQuery("2", testTime, WithTimeout(5*time.Second)),
inRes: &queryResult{
Type: model.ValScalar,
Result: &model.Scalar{
Expand Down
2 changes: 1 addition & 1 deletion api/prometheus/v1/example_test.go
Expand Up @@ -39,7 +39,7 @@ func ExampleAPI_query() {
v1api := v1.NewAPI(client)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
result, warnings, err := v1api.Query(ctx, "up", time.Now())
result, warnings, err := v1api.Query(ctx, "up", time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
fmt.Printf("Error querying Prometheus: %v\n", err)
os.Exit(1)
Expand Down

0 comments on commit 80099df

Please sign in to comment.