diff --git a/pkg/events/events.go b/pkg/events/events.go index 4961fb3..a018ac5 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -34,7 +34,7 @@ const ( ) var httpClient = &http.Client{ - Transport: metrics.NewInstrumenter("events", false).InstrumentedRoundTripper(), + Transport: metrics.NewInstrumenter("events").InstrumentedRoundTripper(), } type Reader struct { diff --git a/pkg/metrics/metrics.go b/pkg/metrics/metrics.go index 67e7e94..954804d 100644 --- a/pkg/metrics/metrics.go +++ b/pkg/metrics/metrics.go @@ -33,17 +33,35 @@ const namespace = "aks_node_termination_handler" type Instrumenter struct { subsystemIdentifier string insecureSkipVerify bool + useProxyFromEnv bool } // New creates a new Instrumenter. The subsystemIdentifier will be used as part of // the metric names (e.g. http__requests_total). -func NewInstrumenter(subsystemIdentifier string, insecureSkipVerify bool) *Instrumenter { +func NewInstrumenter(subsystemIdentifier string) *Instrumenter { return &Instrumenter{ subsystemIdentifier: subsystemIdentifier, - insecureSkipVerify: insecureSkipVerify, } } +type InstrumenterWith string + +const ( + ProxyFromEnv InstrumenterWith = "proxy_from_env" + InsecureSkipVerify InstrumenterWith = "insecure_skip_verify" +) + +func (i *Instrumenter) With(options InstrumenterWith) *Instrumenter { + switch options { + case InsecureSkipVerify: + i.insecureSkipVerify = true + case ProxyFromEnv: + i.useProxyFromEnv = true + } + + return i +} + // InstrumentedRoundTripper returns an instrumented round tripper. func (i *Instrumenter) InstrumentedRoundTripper() http.RoundTripper { inFlightRequestsGauge := promauto.NewGauge(prometheus.GaugeOpts{ @@ -71,15 +89,19 @@ func (i *Instrumenter) InstrumentedRoundTripper() http.RoundTripper { []string{"method"}, ) + defaultTransport := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: i.insecureSkipVerify, //nolint:gosec + }, + } + + if i.useProxyFromEnv { + defaultTransport.Proxy = http.ProxyFromEnvironment + } + return promhttp.InstrumentRoundTripperInFlight(inFlightRequestsGauge, promhttp.InstrumentRoundTripperDuration(requestLatencyHistogram, - i.instrumentRoundTripperEndpoint(requestsPerEndpointCounter, - &http.Transport{ - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: i.insecureSkipVerify, //nolint:gosec - }, - }, - ), + i.instrumentRoundTripperEndpoint(requestsPerEndpointCounter, defaultTransport), ), ) } diff --git a/pkg/metrics/metrics_test.go b/pkg/metrics/metrics_test.go index 8ccb385..7eb6178 100644 --- a/pkg/metrics/metrics_test.go +++ b/pkg/metrics/metrics_test.go @@ -77,7 +77,7 @@ func TestKubernetesMetrics(t *testing.T) { func TestInstrumenter(t *testing.T) { t.Parallel() - instrumenter := metrics.NewInstrumenter("test", false) + instrumenter := metrics.NewInstrumenter("test") r, err := instrumenter.InstrumentedRoundTripper().RoundTrip(httptest.NewRequest(http.MethodGet, ts.URL, nil)) if err != nil { diff --git a/pkg/webhook/webhook.go b/pkg/webhook/webhook.go index 75f3674..d36873e 100644 --- a/pkg/webhook/webhook.go +++ b/pkg/webhook/webhook.go @@ -27,7 +27,10 @@ import ( ) var client = &http.Client{ - Transport: metrics.NewInstrumenter("webhook", true).InstrumentedRoundTripper(), + Transport: metrics.NewInstrumenter("webhook"). + With(metrics.InsecureSkipVerify). + With(metrics.ProxyFromEnv). + InstrumentedRoundTripper(), } var errHTTPNotOK = errors.New("http result not OK")