fix(output.datadog): log response in case of non 2XX response from API (#12201)
This commit is contained in:
parent
960a1f7b14
commit
6f407c5949
|
|
@ -6,6 +6,7 @@ import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
@ -168,7 +169,9 @@ func (d *Datadog) Write(metrics []telegraf.Metric) error {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode > 209 {
|
if resp.StatusCode < 200 || resp.StatusCode > 209 {
|
||||||
return fmt.Errorf("received bad status code, %d", resp.StatusCode)
|
// err can be ignored
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
return fmt.Errorf("received bad status code, %d: %s", resp.StatusCode, string(body))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ var (
|
||||||
func NewDatadog(url string) *Datadog {
|
func NewDatadog(url string) *Datadog {
|
||||||
return &Datadog{
|
return &Datadog{
|
||||||
URL: url,
|
URL: url,
|
||||||
|
Log: testutil.Logger{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -67,14 +68,10 @@ func TestCompressionOverride(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBadStatusCode(t *testing.T) {
|
func TestBadStatusCode(t *testing.T) {
|
||||||
|
errorString := `{"errors": ["Something bad happened to the server.", "Your query made the server very sad."]}`
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
//nolint:errcheck,revive // Ignore the returned error as the test will fail anyway
|
fmt.Fprint(w, errorString)
|
||||||
json.NewEncoder(w).Encode(`{ 'errors': [
|
|
||||||
'Something bad happened to the server.',
|
|
||||||
'Your query made the server very sad.'
|
|
||||||
]
|
|
||||||
}`)
|
|
||||||
}))
|
}))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
|
|
||||||
|
|
@ -86,7 +83,7 @@ func TestBadStatusCode(t *testing.T) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("error expected but none returned")
|
t.Errorf("error expected but none returned")
|
||||||
} else {
|
} else {
|
||||||
require.EqualError(t, fmt.Errorf("received bad status code, 500"), err.Error())
|
require.EqualError(t, err, fmt.Sprintf("received bad status code, %v: %s", http.StatusInternalServerError, errorString))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue