Skip to content

Commit 2db0d73

Browse files
dheerajdwivediMathieu Lecarme
authored andcommitted
Support custom success codes in http input (influxdata#6549)
1 parent b338dfa commit 2db0d73

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

etc/telegraf.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,9 @@
26042604
# ## Amount of time allowed to complete the HTTP request
26052605
# # timeout = "5s"
26062606
#
2607+
# ## List of success status codes
2608+
# # success_status_codes = [200]
2609+
#
26072610
# ## Data format to consume.
26082611
# ## Each data format has its own unique set of configuration options, read
26092612
# ## more about them here:

plugins/inputs/http/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ The HTTP input plugin collects metrics from one or more HTTP(S) endpoints. The
4040
## Amount of time allowed to complete the HTTP request
4141
# timeout = "5s"
4242

43+
## List of success status codes
44+
# success_status_codes = [200]
45+
4346
## Data format to consume.
4447
## Each data format has its own unique set of configuration options, read
4548
## more about them here:

plugins/inputs/http/http.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type HTTP struct {
2929
Password string `toml:"password"`
3030
tls.ClientConfig
3131

32+
SuccessStatusCodes []int `toml:"success_status_codes"`
33+
3234
Timeout internal.Duration `toml:"timeout"`
3335

3436
client *http.Client
@@ -71,6 +73,9 @@ var sampleConfig = `
7173
## Amount of time allowed to complete the HTTP request
7274
# timeout = "5s"
7375
76+
## List of success status codes
77+
# success_status_codes = [200]
78+
7479
## Data format to consume.
7580
## Each data format has its own unique set of configuration options, read
7681
## more about them here:
@@ -101,6 +106,11 @@ func (h *HTTP) Init() error {
101106
},
102107
Timeout: h.Timeout.Duration,
103108
}
109+
110+
// Set default as [200]
111+
if len(h.SuccessStatusCodes) == 0 {
112+
h.SuccessStatusCodes = []int{200}
113+
}
104114
return nil
105115
}
106116

@@ -171,12 +181,19 @@ func (h *HTTP) gatherURL(
171181
}
172182
defer resp.Body.Close()
173183

174-
if resp.StatusCode != http.StatusOK {
175-
return fmt.Errorf("Received status code %d (%s), expected %d (%s)",
184+
responseHasSuccessCode := false
185+
for _, statusCode := range h.SuccessStatusCodes {
186+
if resp.StatusCode == statusCode {
187+
responseHasSuccessCode = true
188+
break
189+
}
190+
}
191+
192+
if !responseHasSuccessCode {
193+
return fmt.Errorf("received status code %d (%s), expected any value out of %v",
176194
resp.StatusCode,
177195
http.StatusText(resp.StatusCode),
178-
http.StatusOK,
179-
http.StatusText(http.StatusOK))
196+
h.SuccessStatusCodes)
180197
}
181198

182199
b, err := ioutil.ReadAll(resp.Body)

plugins/inputs/http/http_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,30 @@ func TestInvalidStatusCode(t *testing.T) {
106106
require.Error(t, acc.GatherError(plugin.Gather))
107107
}
108108

109+
func TestSuccessStatusCodes(t *testing.T) {
110+
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111+
w.WriteHeader(http.StatusAccepted)
112+
}))
113+
defer fakeServer.Close()
114+
115+
url := fakeServer.URL + "/endpoint"
116+
plugin := &plugin.HTTP{
117+
URLs: []string{url},
118+
SuccessStatusCodes: []int{200, 202},
119+
}
120+
121+
metricName := "metricName"
122+
p, _ := parsers.NewParser(&parsers.Config{
123+
DataFormat: "json",
124+
MetricName: metricName,
125+
})
126+
plugin.SetParser(p)
127+
128+
var acc testutil.Accumulator
129+
plugin.Init()
130+
require.NoError(t, acc.GatherError(plugin.Gather))
131+
}
132+
109133
func TestMethod(t *testing.T) {
110134
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
111135
if r.Method == "POST" {

0 commit comments

Comments
 (0)