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

move ToClientWithHost to ToClient #5737

Merged
merged 5 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
move ToClientWithHost to ToClient
Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 26, 2022
commit 83118eaeb80df2281d9398ff392d3124c0f0001d
13 changes: 3 additions & 10 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"golang.org/x/net/http2"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configauth"
"go.opentelemetry.io/collector/config/configcompression"
"go.opentelemetry.io/collector/config/configtls"
Expand Down Expand Up @@ -99,8 +98,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) {
func (hcs *HTTPClientSettings) ToClient(host component.Host, settings component.TelemetrySettings) (*http.Client, error) {
tlsCfg, err := hcs.TLSSetting.LoadTLSConfig()
if err != nil {
return nil, err
Expand Down Expand Up @@ -156,11 +154,11 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext
}

if hcs.Auth != nil {
if ext == nil {
if host.GetExtensions() == nil {
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.New("extensions configuration not found")
}

httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticator(ext)
httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticator(host.GetExtensions())
if aerr != nil {
return nil, aerr
}
Expand All @@ -184,11 +182,6 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext
}, nil
}

// ToClientWithHost creates an HTTP client.
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved
func (hcs *HTTPClientSettings) ToClientWithHost(host component.Host, settings component.TelemetrySettings) (*http.Client, error) {
return hcs.ToClient(host.GetExtensions(), settings)
}
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved

// Custom RoundTripper that adds headers.
type headerRoundTripper struct {
transport http.RoundTripper
Expand Down
16 changes: 8 additions & 8 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,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.ToClientWithHost(host, tt)
client, err := test.settings.ToClient(host, tt)
if test.shouldError {
assert.Error(t, err)
return
Expand Down Expand Up @@ -188,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.ToClientWithHost(host, tt)
client, err := test.settings.ToClient(host, tt)
assert.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
Expand Down Expand Up @@ -252,7 +252,7 @@ func TestHTTPClientSettingsError(t *testing.T) {
}
for _, test := range tests {
t.Run(test.err, func(t *testing.T) {
_, err := test.settings.ToClientWithHost(host, componenttest.NewNopTelemetrySettings())
_, err := test.settings.ToClient(host, componenttest.NewNopTelemetrySettings())
assert.Regexp(t, test.err, err)
})
}
Expand Down Expand Up @@ -332,7 +332,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
client, err := test.settings.ToClientWithHost(test.host, componenttest.NewNopTelemetrySettings())
client, err := test.settings.ToClient(test.host, componenttest.NewNopTelemetrySettings())
if test.shouldErr {
assert.Error(t, err)
return
Expand Down Expand Up @@ -558,7 +558,7 @@ func TestHttpReception(t *testing.T) {
return rt, nil
}
}
client, errClient := hcs.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
client, errClient := hcs.ToClient(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(t, errClient)

resp, errResp := client.Get(hcs.Endpoint)
Expand Down Expand Up @@ -810,7 +810,7 @@ func TestHttpHeaders(t *testing.T) {
"header1": "value1",
},
}
client, _ := setting.ToClientWithHost(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
client, _ := setting.ToClient(componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings())
req, err := http.NewRequest("GET", setting.Endpoint, nil)
assert.NoError(t, err)
_, err = client.Do(req)
Expand Down Expand Up @@ -1048,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.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
c, err = hcs.ToClient(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(b, err)
}
b.RunParallel(func(pb *testing.PB) {
if c == nil {
c, err = hcs.ToClientWithHost(componenttest.NewNopHost(), component.TelemetrySettings{})
c, err = hcs.ToClient(componenttest.NewNopHost(), component.TelemetrySettings{})
require.NoError(b, err)
}
for pb.Next() {
Expand Down