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

fix(metricprovider): reuse http.Transport for http.Client #3780

Merged
merged 1 commit into from
Aug 12, 2024
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
28 changes: 19 additions & 9 deletions metricproviders/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,21 @@ func NewPrometheusProvider(api v1.API, logCtx log.Entry, metric v1alpha1.Metric)
return provider, nil
}

func newHTTPTransport(insecureSkipVerify bool) *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
}
}

var secureTransport *http.Transport = newHTTPTransport(false)
var insecureTransport *http.Transport = newHTTPTransport(true)

// NewPrometheusAPI generates a prometheus API from the metric configuration
func NewPrometheusAPI(metric v1alpha1.Metric) (v1.API, error) {
envValuesByKey := make(map[string]string)
Expand All @@ -232,15 +247,10 @@ func NewPrometheusAPI(metric v1alpha1.Metric) (v1.API, error) {
}

var roundTripper http.RoundTripper

roundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: metric.Provider.Prometheus.Insecure},
if metric.Provider.Prometheus.Insecure {
roundTripper = insecureTransport
} else {
roundTripper = secureTransport
}

// attach custom headers to api requests, if specified
Expand Down
9 changes: 5 additions & 4 deletions metricproviders/webmetric/webmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ func (p *Provider) GarbageCollect(run *v1alpha1.AnalysisRun, metric v1alpha1.Met
return nil
}

var insecureTransport *http.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

func NewWebMetricHttpClient(metric v1alpha1.Metric) (*http.Client, error) {
var timeout time.Duration
var oauthCfg clientcredentials.Config
Expand All @@ -191,10 +195,7 @@ func NewWebMetricHttpClient(metric v1alpha1.Metric) (*http.Client, error) {
Timeout: timeout,
}
if metric.Provider.Web.Insecure {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c.Transport = tr
c.Transport = insecureTransport
}
if metric.Provider.Web.Authentication.OAuth2.TokenURL != "" {
if metric.Provider.Web.Authentication.OAuth2.ClientID == "" || metric.Provider.Web.Authentication.OAuth2.ClientSecret == "" {
Expand Down
Loading