@@ -10,6 +10,7 @@ import (
1010 "net/http"
1111 "net/url"
1212 "strconv"
13+ "strings"
1314 "time"
1415
1516 "github.com/gogo/protobuf/proto"
@@ -74,7 +75,7 @@ func NewClient(
7475 querierAddress : querierAddress ,
7576 alertmanagerAddress : alertmanagerAddress ,
7677 rulerAddress : rulerAddress ,
77- timeout : 5 * time .Second ,
78+ timeout : 30 * time .Second ,
7879 httpClient : & http.Client {},
7980 querierClient : promv1 .NewAPI (querierAPIClient ),
8081 orgID : orgID ,
@@ -105,7 +106,7 @@ func NewPromQueryClient(address string) (*Client, error) {
105106 }
106107
107108 c := & Client {
108- timeout : 5 * time .Second ,
109+ timeout : 30 * time .Second ,
109110 httpClient : & http.Client {},
110111 querierClient : promv1 .NewAPI (querierAPIClient ),
111112 }
@@ -332,7 +333,26 @@ func (c *Client) OTLP(timeseries []prompb.TimeSeries) (*http.Response, error) {
332333
333334// Query runs an instant query.
334335func (c * Client ) Query (query string , ts time.Time ) (model.Value , error ) {
335- value , _ , err := c .querierClient .Query (context .Background (), query , ts )
336+ ctx := context .Background ()
337+ retries := backoff .New (ctx , backoff.Config {
338+ MinBackoff : 1 * time .Second ,
339+ MaxBackoff : 3 * time .Second ,
340+ MaxRetries : 5 ,
341+ })
342+ var (
343+ value model.Value
344+ err error
345+ )
346+ for retries .Ongoing () {
347+ value , _ , err = c .querierClient .Query (context .Background (), query , ts )
348+ if err == nil {
349+ break
350+ }
351+ if ! strings .Contains (err .Error (), "EOF" ) {
352+ break
353+ }
354+ retries .Wait ()
355+ }
336356 return value , err
337357}
338358
@@ -345,11 +365,31 @@ func (c *Client) QueryExemplars(query string, start, end time.Time) ([]promv1.Ex
345365
346366// QueryRange runs a query range.
347367func (c * Client ) QueryRange (query string , start , end time.Time , step time.Duration ) (model.Value , error ) {
348- value , _ , err := c .querierClient .QueryRange (context .Background (), query , promv1.Range {
349- Start : start ,
350- End : end ,
351- Step : step ,
368+ ctx := context .Background ()
369+ retries := backoff .New (ctx , backoff.Config {
370+ MinBackoff : 1 * time .Second ,
371+ MaxBackoff : 3 * time .Second ,
372+ MaxRetries : 5 ,
352373 })
374+ var (
375+ value model.Value
376+ err error
377+ )
378+ for retries .Ongoing () {
379+ value , _ , err = c .querierClient .QueryRange (context .Background (), query , promv1.Range {
380+ Start : start ,
381+ End : end ,
382+ Step : step ,
383+ })
384+ if err == nil {
385+ break
386+ }
387+ if ! strings .Contains (err .Error (), "EOF" ) {
388+ break
389+ }
390+ retries .Wait ()
391+ }
392+
353393 return value , err
354394}
355395
0 commit comments