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

Added functional option to allow to customize DialContext() in HTTP client #291

Merged

Conversation

pracucci
Copy link
Contributor

@pracucci pracucci commented Apr 16, 2021

In Cortex we have a use case where we would need to customize the dialer used by the HTTP client in the upstream Prometheus alertmanager. Specifically, we're trying to customise the dialer used by HTTP client used by Alertmanager notifiers (eg. webhook). Unfortunately we can't do it without changing it upstream so, in this PR, I'm proposing an alternative option to #290.

This PR is the base for the Alertmanager changes proposed in prometheus/alertmanager#2547.

@pracucci
Copy link
Contributor Author

@beorn7 WDYT about this alternative approach?

// NewClient returns a http.Client using the specified http.RoundTripper.
func newClient(rt http.RoundTripper) *http.Client {
return &http.Client{Transport: rt}
}

// NewClientFromConfig returns a new HTTP client configured for the
// given config.HTTPClientConfig. The name is used as go-conntrack metric label.
func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, enableHTTP2 bool) (*http.Client, error) {
rt, err := NewRoundTripperFromConfig(cfg, name, disableKeepAlives, enableHTTP2)
func NewClientFromConfig(cfg HTTPClientConfig, name string, disableKeepAlives, enableHTTP2 bool, optFuncs ...HTTPClientOption) (*http.Client, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If this design makes sense to you, I would move disableKeepAlives and enableHTTP2 to functional options too.

Copy link
Member

Choose a reason for hiding this comment

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

Yes it makes sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@roidelapluie I moved them to functional options. The default I've picked is both keepalive and HTTP2 enabled. To make it more explicit, the function to change these options allow only to disable it. WDYT?


var dialContext func(ctx context.Context, network, addr string) (net.Conn, error)

if opts.dialContextFunc != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Can we build up a list and append to the list when needed, to avoid the repetition?

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 tried that in first place but the data type is not exported. Not sure how I could do it.

Copy link
Member

Choose a reason for hiding this comment

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

Build the list 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.

Which data type should have the slice?

@pracucci
Copy link
Contributor Author

@roidelapluie
Copy link
Member

It is an interface we can replicate I think, it should work

@pracucci
Copy link
Contributor Author

It is an interface we can replicate I think, it should work

@roidelapluie I'm sorry to bother you but I can't understand how we can replicate it. The interface dialerOpt is based on a data type dialerOpts which is not exported. Am I missing anything?

type dialerOpts struct {
	name                  string
	monitoring            bool
	tracing               bool
	parentDialContextFunc dialerContextFunc
}

type dialerOpt func(*dialerOpts)

@roidelapluie
Copy link
Member

Oh sorry, you are right. I will give this a final review later on.

@pracucci
Copy link
Contributor Author

Oh sorry, you are right. I will give this a final review later on.

Thanks. If this change will be accepted, I will then open a PR to conntrack to propose to expose DialerOpt. But I would prefer to not block on it, if it's not a big deal for you.

@roidelapluie roidelapluie merged commit 3b362f5 into prometheus:main Apr 21, 2021
@pracucci pracucci deleted the add-custom-dialer-option-to-http-client branch April 21, 2021 09:54
SuperQ added a commit to prometheus-community/json_exporter that referenced this pull request Aug 5, 2021
* Update to latest modules.
* Use new syntax for http client[0].

[0]: prometheus/common#291

Signed-off-by: SuperQ <[email protected]>
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.

2 participants