From aab1d62de832926cb7e95de48dd68f6e6eddf4e8 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 21 Jul 2022 18:18:47 +0300 Subject: [PATCH] push.go: limit the maximum timeout for pushing metrics to the provided interval between pushes This should guarantee that metrics are pushed regularly with the provided interval. If the remote storage cannot keep up with push frequency, then timeout errors will be logged. --- push.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/push.go b/push.go index 121971d..b16be28 100644 --- a/push.go +++ b/push.go @@ -81,6 +81,9 @@ func initPush(pushURL string, interval time.Duration, extraLabels string, writeM if err := validateTags(extraLabels); err != nil { panic(fmt.Errorf("BUG: invalid extraLabels=%q: %s", extraLabels, err)) } + c := &http.Client{ + Timeout: interval, + } go func() { ticker := time.NewTicker(interval) var bb bytes.Buffer @@ -93,7 +96,7 @@ func initPush(pushURL string, interval time.Duration, extraLabels string, writeM bb.Reset() bb.Write(tmpBuf) } - resp, err := http.Post(pushURL, "text/plain", &bb) + resp, err := c.Post(pushURL, "text/plain", &bb) if err != nil { log.Printf("cannot push metrics to %q: %s", pushURL, err) continue