feat: adds optional list of non retryable http statuscodes to http output plugin (#10186)

This commit is contained in:
Nico Vinzens 2021-12-20 18:16:23 +01:00 committed by GitHub
parent e00147ded3
commit c6faf3d3b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 9 deletions

View File

@ -89,6 +89,9 @@ batch format by default.
#role_session_name = ""
#profile = ""
#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

View File

@ -116,14 +116,15 @@ const (
)
type HTTP struct {
URL string `toml:"url"`
Method string `toml:"method"`
Username string `toml:"username"`
Password string `toml:"password"`
Headers map[string]string `toml:"headers"`
ContentEncoding string `toml:"content_encoding"`
UseBatchFormat bool `toml:"use_batch_format"`
AwsService string `toml:"aws_service"`
URL string `toml:"url"`
Method string `toml:"method"`
Username string `toml:"username"`
Password string `toml:"password"`
Headers map[string]string `toml:"headers"`
ContentEncoding string `toml:"content_encoding"`
UseBatchFormat bool `toml:"use_batch_format"`
AwsService string `toml:"aws_service"`
NonRetryableStatusCodes []int `toml:"non_retryable_statuscodes"`
httpconfig.HTTPClientConfig
Log telegraf.Logger `toml:"-"`
@ -277,6 +278,13 @@ func (h *HTTP) writeMetric(reqBody []byte) error {
defer resp.Body.Close()
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 := ""
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
if scanner.Scan() {

View File

@ -21,6 +21,7 @@ import (
"github.com/influxdata/telegraf/plugins/serializers"
"github.com/influxdata/telegraf/plugins/serializers/influx"
"github.com/influxdata/telegraf/plugins/serializers/json"
"github.com/influxdata/telegraf/testutil"
)
func getMetric() telegraf.Metric {
@ -172,11 +173,22 @@ func TestStatusCode(t *testing.T) {
plugin: &HTTP{
URL: u.String(),
},
statusCode: http.StatusMultipleChoices,
statusCode: http.StatusBadRequest,
errFunc: func(t *testing.T, err error) {
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 {
@ -190,6 +202,8 @@ func TestStatusCode(t *testing.T) {
err = tt.plugin.Connect()
require.NoError(t, err)
tt.plugin.Log = testutil.Logger{}
err = tt.plugin.Write([]telegraf.Metric{getMetric()})
tt.errFunc(t, err)
})