fix: Add error message logging to outputs.http (#9727)
This commit is contained in:
parent
cfd50de57c
commit
357959f087
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue