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 package http
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
@ -18,6 +19,7 @@ import (
) )
const ( const (
maxErrMsgLen = 1024
defaultURL = "http://127.0.0.1:8080/telegraf" defaultURL = "http://127.0.0.1:8080/telegraf"
) )
@ -182,11 +184,18 @@ func (h *HTTP) write(reqBody []byte) error {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if resp.StatusCode < 200 || resp.StatusCode >= 300 { 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 { if err != nil {
return fmt.Errorf("when writing to [%s] received error: %v", h.URL, err) return fmt.Errorf("when writing to [%s] received error: %v", h.URL, err)
} }