From dd65f9c3ee72c0e8295316db8296df183dc8baeb Mon Sep 17 00:00:00 2001 From: Levi Harrison Date: Sun, 12 Sep 2021 16:25:54 -0400 Subject: [PATCH 1/3] Add `WithDialTLSContextFunc` option Signed-off-by: Levi Harrison --- config/http_config.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/config/http_config.go b/config/http_config.go index 39650999..b39e1bf0 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -414,24 +414,36 @@ func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error { // by net.Dialer. type DialContextFunc func(context.Context, string, string) (net.Conn, error) +// DialTLSContextFunc defines the signature of the DialContext() function implemented +// by tls.Dialer. +type DialTLSContextFunc func(context.Context, string, string) (net.Conn, error) + type httpClientOptions struct { - dialContextFunc DialContextFunc - keepAlivesEnabled bool - http2Enabled bool - idleConnTimeout time.Duration - userAgent string + dialContextFunc DialContextFunc + dialTLSContextFunc DialTLSContextFunc + keepAlivesEnabled bool + http2Enabled bool + idleConnTimeout time.Duration + userAgent string } // HTTPClientOption defines an option that can be applied to the HTTP client. type HTTPClientOption func(options *httpClientOptions) -// WithDialContextFunc allows you to override func gets used for the actual dialing. The default is `net.Dialer.DialContext`. +// WithDialContextFunc allows you to override the func that gets used for the actual dialing. The default is `net.Dialer.DialContext`. func WithDialContextFunc(fn DialContextFunc) HTTPClientOption { return func(opts *httpClientOptions) { opts.dialContextFunc = fn } } +// WithDialTLSContextFunc allows you to override the func that gets used for the actual dialing. The default is `tls.Dialer.DialContext`. +func WithDialTLSContextFunc(fn DialTLSContextFunc) HTTPClientOption { + return func(opts *httpClientOptions) { + opts.dialTLSContextFunc = fn + } +} + // WithKeepAlivesDisabled allows to disable HTTP keepalive. func WithKeepAlivesDisabled() HTTPClientOption { return func(opts *httpClientOptions) { @@ -519,6 +531,7 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT TLSHandshakeTimeout: 10 * time.Second, ExpectContinueTimeout: 1 * time.Second, DialContext: dialContext, + DialTLSContext: opts.dialTLSContextFunc, } if opts.http2Enabled && cfg.EnableHTTP2 { // HTTP/2 support is golang had many problematic cornercases where From 3996b2eb3e3e327c10f9f7529ab8ae3412e076d8 Mon Sep 17 00:00:00 2001 From: Levi Harrison Date: Sun, 12 Sep 2021 16:26:11 -0400 Subject: [PATCH 2/3] Add tests Signed-off-by: Levi Harrison --- config/http_config_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/config/http_config_test.go b/config/http_config_test.go index fbe65037..99039b08 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -545,6 +545,23 @@ func TestCustomDialContextFunc(t *testing.T) { } } +func TestCustomDialTLSContextFunc(t *testing.T) { + dialFn := func(_ context.Context, _, _ string) (net.Conn, error) { + return nil, errors.New(ExpectedError) + } + + cfg := HTTPClientConfig{} + client, err := NewClientFromConfig(cfg, "test", WithDialTLSContextFunc(dialFn)) + if err != nil { + t.Fatalf("Can't create a client from this config: %+v", cfg) + } + + _, err = client.Get("https://localhost") + if err == nil || !strings.Contains(err.Error(), ExpectedError) { + t.Errorf("Expected error %q but got %q", ExpectedError, err) + } +} + func TestCustomIdleConnTimeout(t *testing.T) { timeout := time.Second * 5 From e12768bc892cc59682e5ee9f349fdc7dd98921d2 Mon Sep 17 00:00:00 2001 From: Levi Harrison Date: Sun, 12 Sep 2021 16:26:27 -0400 Subject: [PATCH 3/3] Add `__server_name__` label Signed-off-by: Levi Harrison --- model/labels.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/model/labels.go b/model/labels.go index ef895633..7248bc07 100644 --- a/model/labels.go +++ b/model/labels.go @@ -53,6 +53,10 @@ const ( // timeout used to scrape a target. ScrapeTimeoutLabel = "__scrape_timeout__" + // ServerNameLabel is the name of the label that holds the TLS server name + // used to scrape the target. + ServerNameLabel = "__server_name__" + // ReservedLabelPrefix is a prefix which is not legal in user-supplied // label names. ReservedLabelPrefix = "__"