Skip to content

Commit

Permalink
Support custom success codes in http input (#6549)
Browse files Browse the repository at this point in the history
  • Loading branch information
dheerajdwivedi authored and danielnelson committed Oct 21, 2019
1 parent 3802c8b commit a01d273
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
3 changes: 3 additions & 0 deletions etc/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2604,6 +2604,9 @@
# ## Amount of time allowed to complete the HTTP request
# # timeout = "5s"
#
# ## List of success status codes
# # success_status_codes = [200]
#
# ## Data format to consume.
# ## Each data format has its own unique set of configuration options, read
# ## more about them here:
Expand Down
3 changes: 3 additions & 0 deletions plugins/inputs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
## Amount of time allowed to complete the HTTP request
# timeout = "5s"

## List of success status codes
# success_status_codes = [200]

## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
Expand Down
25 changes: 21 additions & 4 deletions plugins/inputs/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type HTTP struct {
Password string `toml:"password"`
tls.ClientConfig

SuccessStatusCodes []int `toml:"success_status_codes"`

Timeout internal.Duration `toml:"timeout"`

client *http.Client
Expand Down Expand Up @@ -71,6 +73,9 @@ var sampleConfig = `
## Amount of time allowed to complete the HTTP request
# timeout = "5s"
## List of success status codes
# success_status_codes = [200]
## Data format to consume.
## Each data format has its own unique set of configuration options, read
## more about them here:
Expand Down Expand Up @@ -101,6 +106,11 @@ func (h *HTTP) Init() error {
},
Timeout: h.Timeout.Duration,
}

// Set default as [200]
if len(h.SuccessStatusCodes) == 0 {
h.SuccessStatusCodes = []int{200}
}
return nil
}

Expand Down Expand Up @@ -171,12 +181,19 @@ func (h *HTTP) gatherURL(
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Received status code %d (%s), expected %d (%s)",
responseHasSuccessCode := false
for _, statusCode := range h.SuccessStatusCodes {
if resp.StatusCode == statusCode {
responseHasSuccessCode = true
break
}
}

if !responseHasSuccessCode {
return fmt.Errorf("received status code %d (%s), expected any value out of %v",
resp.StatusCode,
http.StatusText(resp.StatusCode),
http.StatusOK,
http.StatusText(http.StatusOK))
h.SuccessStatusCodes)
}

b, err := ioutil.ReadAll(resp.Body)
Expand Down
24 changes: 24 additions & 0 deletions plugins/inputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ func TestInvalidStatusCode(t *testing.T) {
require.Error(t, acc.GatherError(plugin.Gather))
}

func TestSuccessStatusCodes(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusAccepted)
}))
defer fakeServer.Close()

url := fakeServer.URL + "/endpoint"
plugin := &plugin.HTTP{
URLs: []string{url},
SuccessStatusCodes: []int{200, 202},
}

metricName := "metricName"
p, _ := parsers.NewParser(&parsers.Config{
DataFormat: "json",
MetricName: metricName,
})
plugin.SetParser(p)

var acc testutil.Accumulator
plugin.Init()
require.NoError(t, acc.GatherError(plugin.Gather))
}

func TestMethod(t *testing.T) {
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
Expand Down

0 comments on commit a01d273

Please sign in to comment.