Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP input plugin: Supporting custom success codes #6549

Merged
merged 4 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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