Skip to content

Commit

Permalink
Using HTTP Proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Paskal <[email protected]>
  • Loading branch information
maksim-paskal committed Jul 29, 2024
1 parent cde1c54 commit f10355f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
)

var httpClient = &http.Client{
Transport: metrics.NewInstrumenter("events", false).InstrumentedRoundTripper(),
Transport: metrics.NewInstrumenter("events").InstrumentedRoundTripper(),
}

type Reader struct {
Expand Down
40 changes: 31 additions & 9 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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_<identifier>_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{
Expand Down Expand Up @@ -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),
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion pkg/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f10355f

Please sign in to comment.