Skip to content

Commit

Permalink
[confighttp] Add support for proxy (open-telemetry#8339)
Browse files Browse the repository at this point in the history
**Description:**
Adding a feature to support proxy configuration field in all exporters

**Link to tracking Issue:**
open-telemetry#5761

---------

Signed-off-by: Hrittik Roy <[email protected]>
Signed-off-by: Juraci Paixão Kröhling <[email protected]>
Co-authored-by: Juraci Paixão Kröhling <[email protected]>
Co-authored-by: Juraci Paixão Kröhling <[email protected]>
Co-authored-by: Andrzej Stencel <[email protected]>
Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
5 people authored and pantuza committed Dec 8, 2023
1 parent f349a1b commit 6e13e45
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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: confighttp

# 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 that support confighttp

# 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 @@ import (
"io"
"net"
"net/http"
"net/url"
"time"

"github.com/rs/cors"
Expand All @@ -32,6 +33,9 @@ type HTTPClientSettings struct {
// 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 @@ func (hcs *HTTPClientSettings) ToClient(host component.Host, settings component.
transport.IdleConnTimeout = *hcs.IdleConnTimeout
}

// Setting the Proxy URL
if hcs.ProxyURL != "" {
proxyURL, parseErr := url.ParseRequestURI(hcs.ProxyURL)
if parseErr != nil {
return nil, parseErr
}
transport.Proxy = http.ProxyURL(proxyURL)
}

transport.DisableKeepAlives = hcs.DisableKeepAlives

clientTransport := (http.RoundTripper)(transport)
Expand Down
55 changes: 55 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,61 @@ 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
err bool
}{
{
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"},
},
{
desc: "proxy is invalid",
proxyURL: "://example.com",
err: true,
},
}
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)

if tC.err {
require.Error(t, err)
} else {
require.NoError(t, err)
}

if err == nil {
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

0 comments on commit 6e13e45

Please sign in to comment.