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

feat(confighttp): add max_redirects configuration option #10877

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

rogercoll
Copy link
Contributor

@rogercoll rogercoll commented Aug 13, 2024

Description

Link to tracking issue

#10870

Testing

Unit testing

Documentation

README.md

Copy link

codecov bot commented Aug 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.45%. Comparing base (7f6dc01) to head (76807a3).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10877   +/-   ##
=======================================
  Coverage   91.45%   91.45%           
=======================================
  Files         438      438           
  Lines       23827    23837   +10     
=======================================
+ Hits        21791    21801   +10     
  Misses       1658     1658           
  Partials      378      378           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@atoulme
Copy link
Contributor

atoulme commented Aug 13, 2024

Changes look great, if you would please reduce the diff to avoid having git history be impacted too much.
As you point out, also need to add the option to README.

@rogercoll
Copy link
Contributor Author

@atoulme The lint CI is failing after removing the auto formatting changes:

confighttp_test.go:40: File is not `gofmt`-ed with `-s` (gofmt)
type customRoundTripper struct{

Should we fix them here? Or in a new PR?

@rogercoll
Copy link
Contributor Author

The lint CI is failing after removing the auto formatting changes

My bad, it was because of a removed space in the diffs. Moving the PR ready for review

@rogercoll rogercoll marked this pull request as ready for review August 14, 2024 14:39
@rogercoll rogercoll requested review from a team and atoulme August 14, 2024 14:39
@rogercoll
Copy link
Contributor Author

@atoulme Any update on this PR? Any additional feedback would be really appreciated. Thanks for taking a look.

Copy link
Contributor

@atoulme atoulme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the edits

@rogercoll rogercoll requested a review from a team as a code owner September 19, 2024 13:13
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rogercoll, just one question

func makeCheckRedirect(max *int) func(*http.Request, []*http.Request) error {
if max == nil {
return nil
} else if *max == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case needed here? if max is set to 0, will the check at line 266 return on the first call the same as is this block here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Actually, this branch is not needed and resulted in the same output when max_redirects was set to either 0 or 1. The max redirects function is always called when a redirect response is returned, meaning that at least one request has already been made. The variable via []*http.Request on line 265 will always be > 0.

If max_redirects is set to 0, the http client should not follow any redirect response (one request). If max_redirects is 1, only one redirect response should be followed (2 total requests). max_redirects = total_requests - 1

Fixed in c98fe0a

Thanks!

config/confighttp/confighttp.go Outdated Show resolved Hide resolved
@@ -105,6 +105,9 @@ type ClientConfig struct {
HTTP2PingTimeout time.Duration `mapstructure:"http2_ping_timeout"`
// Cookies configures the cookie management of the HTTP client.
Cookies *CookiesConfig `mapstructure:"cookies"`

// Maximum number of redirections to follow, if not defined, the Client uses its default policy, which is to stop after 10 consecutive requests.
MaxRedirects *int `mapstructure:"max_redirects"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can even keep it int with 10 by default. That should be cleaner. The only problem is it'll be a significant breaking change (no redirects) for a component that doesn't use NewDefaultClientConfig, but I don't think we have any of those in contrib. Maybe we can check... All components should use that anyway. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am totally open to adding this, but I am concert about receivers initiating the config without using the default method (max_redirects will be set to 0). Also, the only difference is that maybe we should align with the Go library error message after 10 redirects:

func defaultCheckRedirect(req *Request, via []*Request) error {
	if len(via) >= 10 {
		return errors.New("stopped after 10 redirects")
	}
	return nil
}

There is this ongoing effort to use the NewDefaultClientConfig: open-telemetry/opentelemetry-collector-contrib#35457

Co-authored-by: Dmitrii Anoshin <anoshindx@gmail.com>
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Oct 24, 2024
@github-actions github-actions bot removed the Stale label Oct 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants