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
25 changes: 25 additions & 0 deletions .chloggen/proxy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: exporter
hrittikhere marked this conversation as resolved.
Show resolved Hide resolved

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support proxy configuration field in all exporters
hrittikhere marked this conversation as resolved.
Show resolved Hide resolved

# One or more tracking issues or pull requests related to the change
issues: [5761]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
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, parseErr := url.ParseRequestURI(hcs.ProxyURL)
if parseErr != nil {
return nil, parseErr
}

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)
}

hrittikhere marked this conversation as resolved.
Show resolved Hide resolved
transport.DisableKeepAlives = hcs.DisableKeepAlives

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