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

add support for proxy #8339

Merged
merged 13 commits into from
Nov 22, 2023
13 changes: 13 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"io"
"net"
"net/http"
"net/url"
"time"

"github.com/rs/cors"
Expand All @@ -32,6 +33,9 @@
// The target URL to send data to (e.g.: http://some.url:9411/v1/traces).
Endpoint string `mapstructure:"endpoint"`

// ProxyURL setting for the collector
ProxyURL string `mapstructure:"proxy_url"`

// TLSSetting struct exposes TLS client configuration.
TLSSetting configtls.TLSClientSetting `mapstructure:"tls"`

Expand Down Expand Up @@ -132,6 +136,15 @@
transport.IdleConnTimeout = *hcs.IdleConnTimeout
}

// Setting the Proxy URL
if hcs.ProxyURL != "" {
proxyURL, err := url.Parse(hcs.ProxyURL)
hrittikhere marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

Check warning on line 144 in config/confighttp/confighttp.go

View check run for this annotation

Codecov / codecov/patch

config/confighttp/confighttp.go#L143-L144

Added lines #L143 - L144 were not covered by tests
transport.Proxy = http.ProxyURL(proxyURL)
}

transport.DisableKeepAlives = hcs.DisableKeepAlives

hrittikhere marked this conversation as resolved.
Show resolved Hide resolved
clientTransport := (http.RoundTripper)(transport)
Expand Down
42 changes: 42 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,48 @@ func TestDefaultHTTPClientSettings(t *testing.T) {
assert.EqualValues(t, 90*time.Second, *httpClientSettings.IdleConnTimeout)
}

func TestProxyURL(t *testing.T) {
testCases := []struct {
desc string
proxyURL string
expectedURL *url.URL
}{
{
desc: "default config",
expectedURL: nil,
},
{
desc: "proxy is set",
proxyURL: "http://proxy.example.com:8080",
expectedURL: &url.URL{Scheme: "http", Host: "proxy.example.com:8080"},
},
hrittikhere marked this conversation as resolved.
Show resolved Hide resolved
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
s := NewDefaultHTTPClientSettings()
s.ProxyURL = tC.proxyURL

tt := componenttest.NewNopTelemetrySettings()
tt.TracerProvider = nil
client, err := s.ToClient(componenttest.NewNopHost(), tt)
require.NoError(t, err)

transport := client.Transport.(*http.Transport)
require.NotNil(t, transport.Proxy)

url, err := transport.Proxy(&http.Request{URL: &url.URL{Scheme: "http", Host: "example.com"}})
require.NoError(t, err)

if tC.expectedURL == nil {
assert.Nil(t, url)
} else {
require.NotNil(t, url)
assert.Equal(t, tC.expectedURL, url)
}
})
}
}

func TestHTTPClientSettingsError(t *testing.T) {
host := &mockHost{
ext: map[component.ID]component.Component{},
Expand Down
Loading