Skip to content

Commit

Permalink
change HTTPClientSettings.ToClient to use component.Host as input (#5584
Browse files Browse the repository at this point in the history
)

* change confighttp.HTTPClientSettings.ToClient to use component.Host as input

Signed-off-by: Ziqi Zhao <[email protected]>

* rewrite the pr on the basis of principle for breaking change

Signed-off-by: Ziqi Zhao <[email protected]>

* add issue number to changelog

Signed-off-by: Ziqi Zhao <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Pablo Baeyens <[email protected]>

* fix for review

Signed-off-by: Ziqi Zhao <[email protected]>

Co-authored-by: Pablo Baeyens <[email protected]>
  • Loading branch information
fatsheep9146 and mx-psi authored Jun 27, 2022
1 parent adc0fd9 commit bd7e054
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

### 💡 Enhancements 💡

- Deprecate `HTTPClientSettings.ToClient` in favor of `HTTPClientSettings.ToClientWithHost` (#5584)
- Use OpenCensus `metric` package for process metrics instead of `stats` package (#5486)
- Update OTLP to v0.18.0 (#5530)
- Log histogram min/max fields with `logging` exporter (#5520)
Expand Down
6 changes: 6 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func NewDefaultHTTPClientSettings() HTTPClientSettings {
}

// ToClient creates an HTTP client.
// Deprecated: [v0.55.0] Use ToClientWithHost instead.
func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Extension, settings component.TelemetrySettings) (*http.Client, error) {
tlsCfg, err := hcs.TLSSetting.LoadTLSConfig()
if err != nil {
Expand Down Expand Up @@ -183,6 +184,11 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext
}, nil
}

// ToClientWithHost creates an HTTP client.
func (hcs *HTTPClientSettings) ToClientWithHost(host component.Host, settings component.TelemetrySettings) (*http.Client, error) {
return hcs.ToClient(host.GetExtensions(), settings)
}

// Custom RoundTripper that adds headers.
type headerRoundTripper struct {
transport http.RoundTripper
Expand Down
70 changes: 44 additions & 26 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ func (c *customRoundTripper) RoundTrip(request *http.Request) (*http.Response, e
}

func TestAllHTTPClientSettings(t *testing.T) {
ext := map[config.ComponentID]component.Extension{
config.NewComponentID("testauth"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
host := &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("testauth"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
},
}

maxIdleConns := 50
maxIdleConnsPerHost := 40
maxConnsPerHost := 45
Expand Down Expand Up @@ -133,7 +136,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
tt := componenttest.NewNopTelemetrySettings()
tt.TracerProvider = nil
client, err := test.settings.ToClient(ext, tt)
client, err := test.settings.ToClientWithHost(host, tt)
if test.shouldError {
assert.Error(t, err)
return
Expand All @@ -155,9 +158,12 @@ func TestAllHTTPClientSettings(t *testing.T) {
}

func TestPartialHTTPClientSettings(t *testing.T) {
ext := map[config.ComponentID]component.Extension{
config.NewComponentID("testauth"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
host := &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("testauth"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
},
}

tests := []struct {
name string
settings HTTPClientSettings
Expand All @@ -182,7 +188,7 @@ func TestPartialHTTPClientSettings(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
tt := componenttest.NewNopTelemetrySettings()
tt.TracerProvider = nil
client, err := test.settings.ToClient(ext, tt)
client, err := test.settings.ToClientWithHost(host, tt)
assert.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
Expand All @@ -203,6 +209,9 @@ func TestDefaultHTTPClientSettings(t *testing.T) {
}

func TestHTTPClientSettingsError(t *testing.T) {
host := &mockHost{
ext: map[config.ComponentID]component.Extension{},
}
tests := []struct {
settings HTTPClientSettings
err string
Expand Down Expand Up @@ -243,18 +252,18 @@ func TestHTTPClientSettingsError(t *testing.T) {
}
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
_, err := test.settings.ToClient(map[config.ComponentID]component.Extension{}, componenttest.NewNopTelemetrySettings())
_, err := test.settings.ToClientWithHost(host, componenttest.NewNopTelemetrySettings())
assert.Regexp(t, test.err, err)
})
}
}

func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
tests := []struct {
name string
shouldErr bool
settings HTTPClientSettings
extensionMap map[config.ComponentID]component.Extension
name string
shouldErr bool
settings HTTPClientSettings
host component.Host
}{
{
name: "no_auth_extension_enabled",
Expand All @@ -263,9 +272,11 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
Auth: nil,
},
shouldErr: false,
extensionMap: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{
ResultRoundTripper: &customRoundTripper{},
host: &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{
ResultRoundTripper: &customRoundTripper{},
},
},
},
},
Expand All @@ -276,8 +287,10 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("dummy")},
},
shouldErr: true,
extensionMap: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
host: &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
},
},
},
{
Expand All @@ -287,6 +300,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("dummy")},
},
shouldErr: true,
host: componenttest.NewNopHost(),
},
{
name: "with_auth_configuration_has_extension",
Expand All @@ -295,8 +309,10 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("mock")},
},
shouldErr: false,
extensionMap: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
host: &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{ResultRoundTripper: &customRoundTripper{}},
},
},
},
{
Expand All @@ -306,15 +322,17 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
Auth: &configauth.Authentication{AuthenticatorID: config.NewComponentID("mock")},
},
shouldErr: true,
extensionMap: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{
ResultRoundTripper: &customRoundTripper{}, MustError: true},
host: &mockHost{
ext: map[config.ComponentID]component.Extension{
config.NewComponentID("mock"): &configauth.MockClientAuthenticator{
ResultRoundTripper: &customRoundTripper{}, MustError: true},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, err := test.settings.ToClient(test.extensionMap, componenttest.NewNopTelemetrySettings())
client, err := test.settings.ToClientWithHost(test.host, componenttest.NewNopTelemetrySettings())
if test.shouldErr {
assert.Error(t, err)
return
Expand Down Expand Up @@ -540,7 +558,7 @@ func TestHttpReception(t *testing.T) {
return rt, nil
}
}
client, errClient := hcs.ToClient(map[config.ComponentID]component.Extension{}, component.TelemetrySettings{})
client, errClient := hcs.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(t, errClient)

resp, errResp := client.Get(hcs.Endpoint)
Expand Down Expand Up @@ -792,7 +810,7 @@ func TestHttpHeaders(t *testing.T) {
"header1": "value1",
},
}
client, _ := setting.ToClient(map[config.ComponentID]component.Extension{}, componenttest.NewNopTelemetrySettings())
client, _ := setting.ToClientWithHost(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
req, err := http.NewRequest("GET", setting.Endpoint, nil)
assert.NoError(t, err)
_, err = client.Do(req)
Expand Down Expand Up @@ -1030,12 +1048,12 @@ func BenchmarkHttpRequest(b *testing.B) {
b.Run(bb.name, func(b *testing.B) {
var c *http.Client
if !bb.clientPerThread {
c, err = hcs.ToClient(map[config.ComponentID]component.Extension{}, component.TelemetrySettings{})
c, err = hcs.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(b, err)
}
b.RunParallel(func(pb *testing.PB) {
if c == nil {
c, err = hcs.ToClient(map[config.ComponentID]component.Extension{}, component.TelemetrySettings{})
c, err = hcs.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(b, err)
}
for pb.Next() {
Expand Down
2 changes: 1 addition & 1 deletion exporter/otlphttpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const (
maxHTTPResponseReadBytes = 64 * 1024
)

// Crete new exporter.
// Create new exporter.
func newExporter(cfg config.Exporter, set component.ExporterCreateSettings) (*exporter, error) {
oCfg := cfg.(*Config)

Expand Down

0 comments on commit bd7e054

Please sign in to comment.