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

x-pack/filebeat/input/cel: suppress and log max http request retry errors #37160

Merged
merged 3 commits into from
Nov 22, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Setting environmental variable ELASTIC_NETINFO:false in Elastic Agent pod will d
- Made Azure Blob Storage input GA and updated docs accordingly. {pull}37128[37128]
- Add request trace logging to http_endpoint input. {issue}36951[36951] {pull}36957[36957]
- Made GCS input GA and updated docs accordingly. {pull}37127[37127]
- Suppress and log max HTTP request retry errors in CEL input. {pull}37160[37160]

*Auditbeat*

Expand Down
15 changes: 14 additions & 1 deletion x-pack/filebeat/input/cel/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,14 +730,16 @@ func newClient(ctx context.Context, cfg config, log *logp.Logger) (*http.Client,
c.CheckRedirect = checkRedirect(cfg.Resource, log)

if cfg.Resource.Retry.getMaxAttempts() > 1 {
maxAttempts := cfg.Resource.Retry.getMaxAttempts()
c = (&retryablehttp.Client{
HTTPClient: c,
Logger: newRetryLog(log),
RetryWaitMin: cfg.Resource.Retry.getWaitMin(),
RetryWaitMax: cfg.Resource.Retry.getWaitMax(),
RetryMax: cfg.Resource.Retry.getMaxAttempts(),
RetryMax: maxAttempts,
CheckRetry: retryablehttp.DefaultRetryPolicy,
Backoff: retryablehttp.DefaultBackoff,
ErrorHandler: retryErrorHandler(maxAttempts, log),
bhapas marked this conversation as resolved.
Show resolved Hide resolved
}).StandardClient()
}

Expand Down Expand Up @@ -831,6 +833,17 @@ func checkRedirect(cfg *ResourceConfig, log *logp.Logger) func(*http.Request, []
}
}

// retryErrorHandler returns a retryablehttp.ErrorHandler that will log retry resignation
// but return the last retry attempt's response and a nil error so that the CEL code
bhapas marked this conversation as resolved.
Show resolved Hide resolved
// can evaluate the response status itself. Any error passed to the retryablehttp.ErrorHandler
// is returned unaltered.
func retryErrorHandler(max int, log *logp.Logger) retryablehttp.ErrorHandler {
return func(resp *http.Response, err error, numTries int) (*http.Response, error) {
bhapas marked this conversation as resolved.
Show resolved Hide resolved
log.Warnw("giving up retries", "method", resp.Request.Method, "url", resp.Request.URL, "retries", max+1)
return resp, err
}
}

func newRateLimiterFromConfig(cfg *ResourceConfig) *rate.Limiter {
r := rate.Inf
b := 1
Expand Down
31 changes: 31 additions & 0 deletions x-pack/filebeat/input/cel/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,9 @@
</item>
</order>
`
io.ReadAll(r.Body)

Check failure on line 430 in x-pack/filebeat/input/cel/input_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `io.ReadAll` is not checked (errcheck)

Check failure on line 430 in x-pack/filebeat/input/cel/input_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `io.ReadAll` is not checked (errcheck)
r.Body.Close()
w.Write([]byte(text))

Check failure on line 432 in x-pack/filebeat/input/cel/input_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `w.Write` is not checked (errcheck)

Check failure on line 432 in x-pack/filebeat/input/cel/input_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

Error return value of `w.Write` is not checked (errcheck)
})
server := httptest.NewServer(r)
config["resource.url"] = server.URL
Expand Down Expand Up @@ -683,6 +683,37 @@
{"hello": "world"},
},
},
{
name: "retry_failure_no_success",
server: newTestServer(httptest.NewServer),
config: map[string]interface{}{
"interval": 1,
"resource": map[string]interface{}{
"retry": map[string]interface{}{
"max_attempts": 2,
},
},
"program": `
get(state.url).as(resp, {
"url": state.url,
"events": [
bytes(resp.Body).decode_json(),
{"status": resp.StatusCode},
],
})
`,
},
handler: func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusGatewayTimeout)
//nolint:errcheck // No point checking errors in test server.
w.Write([]byte(`{"error":"we were too slow"}`))
},
want: []map[string]interface{}{
{"error": "we were too slow"},
{"status": float64(504)}, // Float because of JSON.
},
},

{
name: "POST_request",
Expand Down
Loading