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

change HTTPClientSettings.ToClient to use component.Host as input #5584

Merged
merged 5 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

### 💡 Enhancements 💡

- Add new function `ToClientWithHost` first, waiting to replace `ToClient` in the future. (#5584)
fatsheep9146 marked this conversation as resolved.
Show resolved Hide resolved
- 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
85 changes: 85 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,91 @@ 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to deprecate ToClient in favor of this function (see paragraph about "deprecation notice" here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

tlsCfg, err := hcs.TLSSetting.LoadTLSConfig()
if err != nil {
return nil, err
}
transport := http.DefaultTransport.(*http.Transport).Clone()
if tlsCfg != nil {
transport.TLSClientConfig = tlsCfg
}
if hcs.ReadBufferSize > 0 {
transport.ReadBufferSize = hcs.ReadBufferSize
}
if hcs.WriteBufferSize > 0 {
transport.WriteBufferSize = hcs.WriteBufferSize
}

if hcs.MaxIdleConns != nil {
transport.MaxIdleConns = *hcs.MaxIdleConns
}

if hcs.MaxIdleConnsPerHost != nil {
transport.MaxIdleConnsPerHost = *hcs.MaxIdleConnsPerHost
}

if hcs.MaxConnsPerHost != nil {
transport.MaxConnsPerHost = *hcs.MaxConnsPerHost
}

if hcs.IdleConnTimeout != nil {
transport.IdleConnTimeout = *hcs.IdleConnTimeout
}

clientTransport := (http.RoundTripper)(transport)
if len(hcs.Headers) > 0 {
clientTransport = &headerRoundTripper{
transport: transport,
headers: hcs.Headers,
}
}
// wrapping http transport with otelhttp transport to enable otel instrumenetation
if settings.TracerProvider != nil && settings.MeterProvider != nil {
clientTransport = otelhttp.NewTransport(
clientTransport,
otelhttp.WithTracerProvider(settings.TracerProvider),
otelhttp.WithMeterProvider(settings.MeterProvider),
otelhttp.WithPropagators(otel.GetTextMapPropagator()),
)
}

// Compress the body using specified compression methods if non-empty string is provided.
// Supporting gzip, zlib, deflate, snappy, and zstd; none is treated as uncompressed.
if configcompression.IsCompressed(hcs.Compression) {
clientTransport = newCompressRoundTripper(clientTransport, hcs.Compression)
}

if hcs.Auth != nil {
if host.GetExtensions() == nil {
return nil, errors.New("extensions configuration not found")
}

httpCustomAuthRoundTripper, aerr := hcs.Auth.GetClientAuthenticator(host.GetExtensions())
if aerr != nil {
return nil, aerr
}

clientTransport, err = httpCustomAuthRoundTripper.RoundTripper(clientTransport)
if err != nil {
return nil, err
}
}

if hcs.CustomRoundTripper != nil {
clientTransport, err = hcs.CustomRoundTripper(clientTransport)
if err != nil {
return nil, err
}
}

return &http.Client{
Transport: clientTransport,
Timeout: hcs.Timeout,
}, nil
}

// 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