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.
This commit is contained in:
Aliaksandr Valialkin 2022-07-21 18:18:47 +03:00
parent 2767350561
commit aab1d62de8
No known key found for this signature in database
GPG Key ID: A72BEC6CD3D0DED1

View File

@ -81,6 +81,9 @@ func initPush(pushURL string, interval time.Duration, extraLabels string, writeM
if err := validateTags(extraLabels); err != nil { if err := validateTags(extraLabels); err != nil {
panic(fmt.Errorf("BUG: invalid extraLabels=%q: %s", extraLabels, err)) panic(fmt.Errorf("BUG: invalid extraLabels=%q: %s", extraLabels, err))
} }
c := &http.Client{
Timeout: interval,
}
go func() { go func() {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
var bb bytes.Buffer var bb bytes.Buffer
@ -93,7 +96,7 @@ func initPush(pushURL string, interval time.Duration, extraLabels string, writeM
bb.Reset() bb.Reset()
bb.Write(tmpBuf) bb.Write(tmpBuf)
} }
resp, err := http.Post(pushURL, "text/plain", &bb) resp, err := c.Post(pushURL, "text/plain", &bb)
if err != nil { if err != nil {
log.Printf("cannot push metrics to %q: %s", pushURL, err) log.Printf("cannot push metrics to %q: %s", pushURL, err)
continue continue