Skip to content

Commit

Permalink
x-pack/filebeat/input/{cel,httpjson}: elide unneeded retyable HTTP cl…
Browse files Browse the repository at this point in the history
…ient construction
  • Loading branch information
efd6 committed Oct 19, 2023
1 parent deb7d42 commit 29329c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-developer.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ The list below covers the major changes between 7.0.0-rc2 and main only.
- Make Filebeat HTTPJSON input process responses sequentially. {pull}36493[36493]
- Add initial infrastructure for a caching enrichment processor. {pull}36619[36619]
- Add file-backed cache for cache enrichment processor. {pull}36686[36686] {pull}36696[36696]
- Elide retryable HTTP client construction in Filebeat HTTPJSON and CEL inputs if not needed. {pull}[]

==== Deprecated

Expand Down
22 changes: 12 additions & 10 deletions x-pack/filebeat/input/cel/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,25 +707,27 @@ func newClient(ctx context.Context, cfg config, log *logp.Logger) (*http.Client,

c.CheckRedirect = checkRedirect(cfg.Resource, log)

client := &retryablehttp.Client{
HTTPClient: c,
Logger: newRetryLog(log),
RetryWaitMin: cfg.Resource.Retry.getWaitMin(),
RetryWaitMax: cfg.Resource.Retry.getWaitMax(),
RetryMax: cfg.Resource.Retry.getMaxAttempts(),
CheckRetry: retryablehttp.DefaultRetryPolicy,
Backoff: retryablehttp.DefaultBackoff,
if cfg.Resource.Retry.getMaxAttempts() > 1 {
c = (&retryablehttp.Client{
HTTPClient: c,
Logger: newRetryLog(log),
RetryWaitMin: cfg.Resource.Retry.getWaitMin(),
RetryWaitMax: cfg.Resource.Retry.getWaitMax(),
RetryMax: cfg.Resource.Retry.getMaxAttempts(),
CheckRetry: retryablehttp.DefaultRetryPolicy,
Backoff: retryablehttp.DefaultBackoff,
}).StandardClient()
}

if cfg.Auth.OAuth2.isEnabled() {
authClient, err := cfg.Auth.OAuth2.client(ctx, client.StandardClient())
authClient, err := cfg.Auth.OAuth2.client(ctx, c)
if err != nil {
return nil, err
}
return authClient, nil
}

return client.StandardClient(), nil
return c, nil
}

func wantClient(cfg config) bool {
Expand Down
26 changes: 14 additions & 12 deletions x-pack/filebeat/input/httpjson/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ func newNetHTTPClient(ctx context.Context, cfg *requestConfig, log *logp.Logger,
}

func newChainHTTPClient(ctx context.Context, authCfg *authConfig, requestCfg *requestConfig, log *logp.Logger, reg *monitoring.Registry, p ...*Policy) (*httpClient, error) {
// Make retryable HTTP client
netHTTPClient, err := newNetHTTPClient(ctx, requestCfg, log, reg)
client, err := newNetHTTPClient(ctx, requestCfg, log, reg)
if err != nil {
return nil, err
}
Expand All @@ -268,27 +267,30 @@ func newChainHTTPClient(ctx context.Context, authCfg *authConfig, requestCfg *re
retryPolicyFunc = retryablehttp.DefaultRetryPolicy
}

client := &retryablehttp.Client{
HTTPClient: netHTTPClient,
Logger: newRetryLogger(log),
RetryWaitMin: requestCfg.Retry.getWaitMin(),
RetryWaitMax: requestCfg.Retry.getWaitMax(),
RetryMax: requestCfg.Retry.getMaxAttempts(),
CheckRetry: retryPolicyFunc,
Backoff: retryablehttp.DefaultBackoff,
if requestCfg.Retry.getMaxAttempts() > 1 {
// Make retryable HTTP client if needed.
client = (&retryablehttp.Client{
HTTPClient: client,
Logger: newRetryLogger(log),
RetryWaitMin: requestCfg.Retry.getWaitMin(),
RetryWaitMax: requestCfg.Retry.getWaitMax(),
RetryMax: requestCfg.Retry.getMaxAttempts(),
CheckRetry: retryPolicyFunc,
Backoff: retryablehttp.DefaultBackoff,
}).StandardClient()
}

limiter := newRateLimiterFromConfig(requestCfg.RateLimit, log)

if authCfg != nil && authCfg.OAuth2.isEnabled() {
authClient, err := authCfg.OAuth2.client(ctx, client.StandardClient())
authClient, err := authCfg.OAuth2.client(ctx, client)
if err != nil {
return nil, err
}
return &httpClient{client: authClient, limiter: limiter}, nil
}

return &httpClient{client: client.StandardClient(), limiter: limiter}, nil
return &httpClient{client: client, limiter: limiter}, nil
}

// clientOption returns constructed client configuration options, including
Expand Down

0 comments on commit 29329c2

Please sign in to comment.