fix: Add error message logging to outputs.http (#9727)

This commit is contained in:
Goutham Veeramachaneni 2021-09-14 23:04:34 +02:00 committed by GitHub
parent cfd50de57c
commit 357959f087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package http
import (
"bufio"
"bytes"
"context"
"fmt"
@ -18,7 +19,8 @@ import (
)
const (
defaultURL = "http://127.0.0.1:8080/telegraf"
maxErrMsgLen = 1024
defaultURL = "http://127.0.0.1:8080/telegraf"
)
var sampleConfig = `
@ -182,11 +184,18 @@ func (h *HTTP) write(reqBody []byte) error {
return err
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("when writing to [%s] received status code: %d", h.URL, resp.StatusCode)
errorLine := ""
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
if scanner.Scan() {
errorLine = scanner.Text()
}
return fmt.Errorf("when writing to [%s] received status code: %d. body: %s", h.URL, resp.StatusCode, errorLine)
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("when writing to [%s] received error: %v", h.URL, err)
}