From 357959f0876985c3b2e19c9fec19fb7d26b1c734 Mon Sep 17 00:00:00 2001 From: Goutham Veeramachaneni Date: Tue, 14 Sep 2021 23:04:34 +0200 Subject: [PATCH] fix: Add error message logging to outputs.http (#9727) --- plugins/outputs/http/http.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/plugins/outputs/http/http.go b/plugins/outputs/http/http.go index 83faef0da..edaae3f6e 100644 --- a/plugins/outputs/http/http.go +++ b/plugins/outputs/http/http.go @@ -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) }