Skip to content

Commit

Permalink
Allow to configure read/write buffer sizes for http Client (#1447)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
  • Loading branch information
bogdandrutu authored Jul 28, 2020
1 parent 39521ed commit 87b1c14
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type HTTPClientSettings struct {
// TLSSetting struct exposes TLS client configuration.
TLSSetting configtls.TLSClientSetting `mapstructure:",squash"`

// ReadBufferSize for HTTP client. See http.Transport.ReadBufferSize.
ReadBufferSize int `mapstructure:"read_buffer_size"`

// WriteBufferSize for HTTP client. See http.Transport.WriteBufferSize.
WriteBufferSize int `mapstructure:"write_buffer_size"`

// Timeout parameter configures `http.Client.Timeout`.
Timeout time.Duration `mapstructure:"timeout,omitempty"`
}
Expand All @@ -45,6 +51,12 @@ func (hcs *HTTPClientSettings) ToClient() (*http.Client, error) {
if tlsCfg != nil {
transport.TLSClientConfig = tlsCfg
}
if hcs.ReadBufferSize > 0 {
transport.ReadBufferSize = hcs.ReadBufferSize
}
if hcs.WriteBufferSize > 0 {
transport.WriteBufferSize = hcs.WriteBufferSize
}
return &http.Client{
Transport: transport,
Timeout: hcs.Timeout,
Expand Down
16 changes: 16 additions & 0 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ import (
"go.opentelemetry.io/collector/config/configtls"
)

func TestAllHTTPClientSettings(t *testing.T) {
hcs := &HTTPClientSettings{
Endpoint: "localhost:1234",
TLSSetting: configtls.TLSClientSetting{
Insecure: false,
},
ReadBufferSize: 1024,
WriteBufferSize: 512,
}
client, err := hcs.ToClient()
assert.NoError(t, err)
transport := client.Transport.(*http.Transport)
assert.EqualValues(t, 1024, transport.ReadBufferSize)
assert.EqualValues(t, 512, transport.WriteBufferSize)
}

func TestHTTPClientSettingsError(t *testing.T) {
tests := []struct {
settings HTTPClientSettings
Expand Down
2 changes: 2 additions & 0 deletions exporter/zipkinexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The following settings can be optionally configured:

- `defaultservicename` (default = <missing service name>): What to name services missing this information.
- `timeout` (default = 5s): How long to wait until the connection is close.
- `read_buffer_size` (default = 0): ReadBufferSize for HTTP client.
- `write_buffer_size` (default = 512 * 1024): WriteBufferSize for HTTP client.

Example:

Expand Down
2 changes: 2 additions & 0 deletions exporter/zipkinexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (f *Factory) CreateDefaultConfig() configmodels.Exporter {
},
HTTPClientSettings: confighttp.HTTPClientSettings{
Timeout: defaultTimeout,
// We almost read 0 bytes, so no need to tune ReadBufferSize.
WriteBufferSize: 512 * 1024,
},
Format: defaultFormat,
DefaultServiceName: defaultServiceName,
Expand Down

0 comments on commit 87b1c14

Please sign in to comment.