feat: adds optional list of non retryable http statuscodes to http output plugin (#10186)
This commit is contained in:
parent
e00147ded3
commit
c6faf3d3b4
|
|
@ -89,6 +89,9 @@ batch format by default.
|
||||||
#role_session_name = ""
|
#role_session_name = ""
|
||||||
#profile = ""
|
#profile = ""
|
||||||
#shared_credential_file = ""
|
#shared_credential_file = ""
|
||||||
|
|
||||||
|
## Optional list of statuscodes (<200 or >300) upon which requests should not be retried
|
||||||
|
# non_retryable_statuscodes = [409, 413]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Optional Cookie Authentication Settings
|
### Optional Cookie Authentication Settings
|
||||||
|
|
|
||||||
|
|
@ -116,14 +116,15 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTP struct {
|
type HTTP struct {
|
||||||
URL string `toml:"url"`
|
URL string `toml:"url"`
|
||||||
Method string `toml:"method"`
|
Method string `toml:"method"`
|
||||||
Username string `toml:"username"`
|
Username string `toml:"username"`
|
||||||
Password string `toml:"password"`
|
Password string `toml:"password"`
|
||||||
Headers map[string]string `toml:"headers"`
|
Headers map[string]string `toml:"headers"`
|
||||||
ContentEncoding string `toml:"content_encoding"`
|
ContentEncoding string `toml:"content_encoding"`
|
||||||
UseBatchFormat bool `toml:"use_batch_format"`
|
UseBatchFormat bool `toml:"use_batch_format"`
|
||||||
AwsService string `toml:"aws_service"`
|
AwsService string `toml:"aws_service"`
|
||||||
|
NonRetryableStatusCodes []int `toml:"non_retryable_statuscodes"`
|
||||||
httpconfig.HTTPClientConfig
|
httpconfig.HTTPClientConfig
|
||||||
Log telegraf.Logger `toml:"-"`
|
Log telegraf.Logger `toml:"-"`
|
||||||
|
|
||||||
|
|
@ -277,6 +278,13 @@ func (h *HTTP) writeMetric(reqBody []byte) error {
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
|
for _, nonRetryableStatusCode := range h.NonRetryableStatusCodes {
|
||||||
|
if resp.StatusCode == nonRetryableStatusCode {
|
||||||
|
h.Log.Errorf("Received non-retryable status %v. Metrics are lost.", resp.StatusCode)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
errorLine := ""
|
errorLine := ""
|
||||||
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
|
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
|
||||||
if scanner.Scan() {
|
if scanner.Scan() {
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"github.com/influxdata/telegraf/plugins/serializers"
|
"github.com/influxdata/telegraf/plugins/serializers"
|
||||||
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
"github.com/influxdata/telegraf/plugins/serializers/influx"
|
||||||
"github.com/influxdata/telegraf/plugins/serializers/json"
|
"github.com/influxdata/telegraf/plugins/serializers/json"
|
||||||
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getMetric() telegraf.Metric {
|
func getMetric() telegraf.Metric {
|
||||||
|
|
@ -172,11 +173,22 @@ func TestStatusCode(t *testing.T) {
|
||||||
plugin: &HTTP{
|
plugin: &HTTP{
|
||||||
URL: u.String(),
|
URL: u.String(),
|
||||||
},
|
},
|
||||||
statusCode: http.StatusMultipleChoices,
|
statusCode: http.StatusBadRequest,
|
||||||
errFunc: func(t *testing.T, err error) {
|
errFunc: func(t *testing.T, err error) {
|
||||||
require.Error(t, err)
|
require.Error(t, err)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Do not retry on configured non-retryable statuscode",
|
||||||
|
plugin: &HTTP{
|
||||||
|
URL: u.String(),
|
||||||
|
NonRetryableStatusCodes: []int{409},
|
||||||
|
},
|
||||||
|
statusCode: http.StatusConflict,
|
||||||
|
errFunc: func(t *testing.T, err error) {
|
||||||
|
require.NoError(t, err)
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|
@ -190,6 +202,8 @@ func TestStatusCode(t *testing.T) {
|
||||||
err = tt.plugin.Connect()
|
err = tt.plugin.Connect()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tt.plugin.Log = testutil.Logger{}
|
||||||
|
|
||||||
err = tt.plugin.Write([]telegraf.Metric{getMetric()})
|
err = tt.plugin.Write([]telegraf.Metric{getMetric()})
|
||||||
tt.errFunc(t, err)
|
tt.errFunc(t, err)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue