Skip to content

Commit

Permalink
resolves issues where failed api calls lead to obscure errors (influx…
Browse files Browse the repository at this point in the history
  • Loading branch information
ssoroka authored and Mathieu Lecarme committed Apr 17, 2020
1 parent 7cc3fc3 commit c80fe5d
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 16 deletions.
1 change: 1 addition & 0 deletions internal/models/running_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func (ro *RunningOutput) WriteBatch() error {
return nil
}

// Close closes the output
func (r *RunningOutput) Close() {
err := r.Output.Close()
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions plugins/inputs/ecs/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ecs

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -50,10 +53,16 @@ func (c *EcsClient) Task() (*Task, error) {

req, _ := http.NewRequest("GET", c.taskURL, nil)
resp, err := c.client.Do(req)

if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.taskURL, resp.Status, body)
}

task, err := unmarshalTask(resp.Body)
if err != nil {
Expand All @@ -71,11 +80,18 @@ func (c *EcsClient) ContainerStats() (map[string]types.StatsJSON, error) {

req, _ := http.NewRequest("GET", c.statsURL, nil)
resp, err := c.client.Do(req)

if err != nil {
return map[string]types.StatsJSON{}, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return nil, fmt.Errorf("%s returned HTTP status %s: %q", c.statsURL, resp.Status, body)
}

statsMap, err := unmarshalStats(resp.Body)
if err != nil {
return map[string]types.StatsJSON{}, err
Expand Down
41 changes: 35 additions & 6 deletions plugins/inputs/ecs/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ func TestEcsClient_Task(t *testing.T) {
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(rc),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(rc),
}, nil
},
},
Expand All @@ -123,11 +124,24 @@ func TestEcsClient_Task(t *testing.T) {
wantErr: true,
},
{
name: "malformed resp",
name: "malformed 500 resp",
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
}, nil
},
},
wantErr: true,
},
{
name: "malformed 200 resp",
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
}, nil
},
},
Expand Down Expand Up @@ -164,7 +178,8 @@ func TestEcsClient_ContainerStats(t *testing.T) {
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(rc),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(rc),
}, nil
},
},
Expand All @@ -181,17 +196,31 @@ func TestEcsClient_ContainerStats(t *testing.T) {
wantErr: true,
},
{
name: "malformed resp",
name: "malformed 200 resp",
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
StatusCode: http.StatusOK,
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
}, nil
},
},
want: map[string]types.StatsJSON{},
wantErr: true,
},
{
name: "malformed 500 resp",
client: mockDo{
do: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: ioutil.NopCloser(bytes.NewReader([]byte("foo"))),
}, nil
},
},
want: nil,
wantErr: true,
},
}

for _, tt := range tests {
Expand Down
3 changes: 1 addition & 2 deletions plugins/inputs/fibaro/fibaro.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func (f *Fibaro) getJSON(path string, dataStruct interface{}) error {
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Response from url \"%s\" has status code %d (%s), expected %d (%s)",
Expand All @@ -108,8 +109,6 @@ func (f *Fibaro) getJSON(path string, dataStruct interface{}) error {
return err
}

defer resp.Body.Close()

dec := json.NewDecoder(resp.Body)
err = dec.Decode(&dataStruct)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions plugins/inputs/haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (g *haproxy) gatherServer(addr string, acc telegraf.Accumulator) error {
if err != nil {
return fmt.Errorf("Unable to connect to haproxy server '%s': %s", addr, err)
}
defer res.Body.Close()

if res.StatusCode != 200 {
return fmt.Errorf("Unable to get valid stat result from '%s', http response code : %d", addr, res.StatusCode)
Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/kibana/kibana.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package kibana
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -250,6 +252,12 @@ func (k *Kibana) gatherJsonData(url string, v interface{}) (host string, err err

defer response.Body.Close()

if response.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(response.Body, 200))
return request.Host, fmt.Errorf("%s returned HTTP status %s: %q", url, response.Status, body)
}

if err = json.NewDecoder(response.Body).Decode(v); err != nil {
return request.Host, err
}
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/logstash/logstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package logstash
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -197,6 +199,11 @@ func (logstash *Logstash) gatherJsonData(url string, value interface{}) error {
}

defer response.Body.Close()
if response.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(response.Body, 200))
return fmt.Errorf("%s returned HTTP status %s: %q", url, response.Status, body)
}

err = json.NewDecoder(response.Body).Decode(value)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/mailchimp/chimp_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -143,6 +144,12 @@ func runChimp(api *ChimpAPI, params ReportsParams) ([]byte, error) {
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return nil, fmt.Errorf("%s returned HTTP status %s: %q", api.url.String(), resp.Status, body)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
Expand Down
17 changes: 13 additions & 4 deletions plugins/inputs/nginx_upstream_check/nginx_upstream_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package nginx_upstream_check

import (
"encoding/json"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/internal/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)

const sampleConfig = `
Expand Down Expand Up @@ -148,6 +152,11 @@ func (check *NginxUpstreamCheck) gatherJsonData(url string, value interface{}) e
}

defer response.Body.Close()
if response.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(response.Body, 200))
return fmt.Errorf("%s returned HTTP status %s: %q", url, response.Status, body)
}

err = json.NewDecoder(response.Body).Decode(value)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions plugins/inputs/nsq_consumer/nsq_consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ func TestReadsMetricsFromNSQ(t *testing.T) {
assert.Equal(t, 0, len(acc.Metrics), "There should not be any points")
if err := consumer.Start(&acc); err != nil {
t.Fatal(err.Error())
} else {
defer consumer.Stop()
}

waitForPoint(&acc, t)
Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/salesforce/salesforce.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
Expand Down Expand Up @@ -200,6 +201,11 @@ func (s *Salesforce) login() error {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// ignore the err here; LimitReader returns io.EOF and we're not interested in read errors.
body, _ := ioutil.ReadAll(io.LimitReader(resp.Body, 200))
return fmt.Errorf("%s returned HTTP status %s: %q", loginEndpoint, resp.Status, body)
}

respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down

0 comments on commit c80fe5d

Please sign in to comment.