Skip to content

Commit

Permalink
move ToClientWithHost to ToClient (#5737)
Browse files Browse the repository at this point in the history
* move ToClientWithHost to ToClient

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

* fix bugs

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

* add change log

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

* Update CHANGELOG.md

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

* fix for comments

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

Co-authored-by: Pablo Baeyens <[email protected]>
  • Loading branch information
fatsheep9146 and mx-psi authored Jul 26, 2022
1 parent e6b5e25 commit 06599bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

### 🛑 Breaking changes 🛑

- Change`confighttp.ToClient` to accept a `component.Host` (#5737)

### 🚩 Deprecations 🚩

### 💡 Enhancements 💡

- `ocb` now exits with an error if it fails to load the build configuration. (#5731)
- Deprecate `HTTPClientSettings.ToClientWithHost` (#5737)

### 🧰 Bug fixes 🧰

Expand Down
9 changes: 4 additions & 5 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,6 +154,7 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext
}

if hcs.Auth != nil {
ext := host.GetExtensions()
if ext == nil {
return nil, errors.New("extensions configuration not found")
}
Expand Down Expand Up @@ -184,9 +183,9 @@ func (hcs *HTTPClientSettings) ToClient(ext map[config.ComponentID]component.Ext
}, nil
}

// ToClientWithHost creates an HTTP client.
// Deprecated: [v0.57.0] use ToClient.
func (hcs *HTTPClientSettings) ToClientWithHost(host component.Host, settings component.TelemetrySettings) (*http.Client, error) {
return hcs.ToClient(host.GetExtensions(), settings)
return hcs.ToClient(host, settings)
}

// Custom RoundTripper that adds headers.
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
2 changes: 1 addition & 1 deletion exporter/otlphttpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func newExporter(cfg config.Exporter, set component.ExporterCreateSettings) (*ex
// start actually creates the HTTP client. The client construction is deferred till this point as this
// is the only place we get hold of Extensions which are required to construct auth round tripper.
func (e *exporter) start(_ context.Context, host component.Host) error {
client, err := e.config.HTTPClientSettings.ToClient(host.GetExtensions(), e.settings)
client, err := e.config.HTTPClientSettings.ToClient(host, e.settings)
if err != nil {
return err
}
Expand Down

0 comments on commit 06599bc

Please sign in to comment.