From a3c338831e11317174ddd64dab8909f2edd7ef65 Mon Sep 17 00:00:00 2001 From: Qiannan Lyu Date: Wed, 1 Apr 2020 20:17:35 +1300 Subject: [PATCH] UCP #1753: add timeout config for query metrics from Prometheus Signed-off-by: Qiannan Lyu --- pkg/autoscaler/autoscaler/calculate/calculate.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pkg/autoscaler/autoscaler/calculate/calculate.go b/pkg/autoscaler/autoscaler/calculate/calculate.go index b01132fec1..81f31a000b 100644 --- a/pkg/autoscaler/autoscaler/calculate/calculate.go +++ b/pkg/autoscaler/autoscaler/calculate/calculate.go @@ -14,11 +14,13 @@ package calculate import ( + "context" "encoding/json" "fmt" "math" "net/http" "strconv" + "time" "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" promClient "github.com/prometheus/client_golang/api" @@ -33,6 +35,7 @@ const ( queryPath = "/api/v1/query" float64EqualityThreshold = 1e-9 + httpRequestTimeout = 5 ) type SingleQuery struct { @@ -46,7 +49,10 @@ type SingleQuery struct { func queryMetricsFromPrometheus(tac *v1alpha1.TidbClusterAutoScaler, client promClient.Client, sq *SingleQuery, resp *Response) error { query := sq.Quary timestamp := sq.Timestamp - req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", sq.Endpoint, queryPath), nil) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second*httpRequestTimeout) + defer cancel() + req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s%s", sq.Endpoint, queryPath), nil) if err != nil { return err }