diff --git a/config/configcompression/compressiontype.go b/config/configcompression/compressiontype.go index feef1cf9434..8d3cea47695 100644 --- a/config/configcompression/compressiontype.go +++ b/config/configcompression/compressiontype.go @@ -37,12 +37,10 @@ func (ct *Type) IsCompressed() bool { } func (ct *TypeWithLevel) UnmarshalText(in []byte) error { - var compressionTyp Type - var level int var err error parts := strings.Split(string(in), "/") - compressionTyp = Type(parts[0]) - level = zlib.DefaultCompression + compressionTyp := Type(parts[0]) + level := zlib.DefaultCompression if len(parts) == 2 { level, err = strconv.Atoi(parts[1]) if err != nil { diff --git a/config/confighttp/README.md b/config/confighttp/README.md index 23dbe845375..2bd327ac80d 100644 --- a/config/confighttp/README.md +++ b/config/confighttp/README.md @@ -31,17 +31,17 @@ README](../configtls/README.md). - NoCompression: `gzip/0` - BestSpeed: `gzip/1` - BestCompression: `gzip/9` - - DefaultCompression: `gzip/-1` + - DefaultCompression: `gzip` - `zlib` - NoCompression: `zlib/0` - BestSpeed: `zlib/1` - BestCompression: `zlib/9` - - DefaultCompression: `zlib/-1` + - DefaultCompression: `zlib` - `deflate` - NoCompression: `deflate/0` - BestSpeed: `deflate/1` - BestCompression: `deflate/9` - - DefaultCompression: `deflate/-1` + - DefaultCompression: `deflate` - `zstd` - SpeedFastest: `zstd/1` - SpeedDefault: `zstd/3` diff --git a/config/confighttp/compression_test.go b/config/confighttp/compression_test.go index 229d64da0f7..e020b7ee581 100644 --- a/config/confighttp/compression_test.go +++ b/config/confighttp/compression_test.go @@ -104,10 +104,10 @@ func TestHTTPClientCompression(t *testing.T) { req, err := http.NewRequest(http.MethodGet, srv.URL, reqBody) require.NoError(t, err, "failed to create request to test handler") - + compression := configcompression.TypeWithLevel{Type: tt.encoding, Level: gzip.BestSpeed} clientSettings := ClientConfig{ Endpoint: srv.URL, - Compression: tt.encoding, + Compression: compression, } client, err := clientSettings.ToClient(context.Background(), componenttest.NewNopHost(), componenttest.NewNopTelemetrySettings()) if tt.shouldError { diff --git a/config/confighttp/confighttp.go b/config/confighttp/confighttp.go index ca1432c32a1..ae0110f494a 100644 --- a/config/confighttp/confighttp.go +++ b/config/confighttp/confighttp.go @@ -67,7 +67,7 @@ type ClientConfig struct { Auth *configauth.Authentication `mapstructure:"auth"` // The compression key for supported compression types within collector. - Compression configcompression.Type `mapstructure:"compression"` + Compression configcompression.TypeWithLevel `mapstructure:"compression"` // MaxIdleConns is used to set a limit to the maximum idle HTTP connections the client can keep open. // By default, it is set to 100. @@ -216,13 +216,8 @@ func (hcs *ClientConfig) ToClient(ctx context.Context, host component.Host, sett // 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. - var compressionTypeWithLevel configcompression.TypeWithLevel - err = compressionTypeWithLevel.UnmarshalText([]byte(hcs.Compression)) - if err != nil { - return nil, err - } - if hcs.Compression.IsCompressed() { - clientTransport, err = newCompressRoundTripper(clientTransport, compressionTypeWithLevel) + if hcs.Compression.Type.IsCompressed() { + clientTransport, err = newCompressRoundTripper(clientTransport, hcs.Compression) if err != nil { return nil, err } diff --git a/config/confighttp/confighttp_test.go b/config/confighttp/confighttp_test.go index f0cd7f6fcd1..6810e2910c5 100644 --- a/config/confighttp/confighttp_test.go +++ b/config/confighttp/confighttp_test.go @@ -84,7 +84,7 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - Compression: "", + Compression: configcompression.TypeWithLevel{Type: "", Level: 1}, DisableKeepAlives: true, Cookies: &CookiesConfig{Enabled: true}, HTTP2ReadIdleTimeout: idleConnTimeout, @@ -105,7 +105,7 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - Compression: "none", + Compression: configcompression.TypeWithLevel{Type: "none", Level: 1}, DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, HTTP2PingTimeout: http2PingTimeout, @@ -125,7 +125,7 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - Compression: "gzip", + Compression: configcompression.TypeWithLevel{Type: "gzip", Level: 1}, DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, HTTP2PingTimeout: http2PingTimeout, @@ -145,33 +145,13 @@ func TestAllHTTPClientSettings(t *testing.T) { MaxIdleConnsPerHost: &maxIdleConnsPerHost, MaxConnsPerHost: &maxConnsPerHost, IdleConnTimeout: &idleConnTimeout, - Compression: "gzip", + Compression: configcompression.TypeWithLevel{Type: "gzip", Level: 1}, DisableKeepAlives: true, HTTP2ReadIdleTimeout: idleConnTimeout, HTTP2PingTimeout: http2PingTimeout, }, shouldError: false, }, - { - name: "invalid_settings_http2_health_check", - settings: ClientConfig{ - Endpoint: "localhost:1234", - TLSSetting: configtls.ClientConfig{ - Insecure: false, - }, - ReadBufferSize: 1024, - WriteBufferSize: 512, - MaxIdleConns: &maxIdleConns, - MaxIdleConnsPerHost: &maxIdleConnsPerHost, - MaxConnsPerHost: &maxConnsPerHost, - IdleConnTimeout: &idleConnTimeout, - Compression: "gzip/ten", - DisableKeepAlives: true, - HTTP2ReadIdleTimeout: idleConnTimeout, - HTTP2PingTimeout: http2PingTimeout, - }, - shouldError: true, - }, } for _, tt := range tests { @@ -432,7 +412,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) { settings: ClientConfig{ Endpoint: "localhost:1234", Auth: &configauth.Authentication{AuthenticatorID: component.MustNewID("mock")}, - Compression: configcompression.TypeGzip, + Compression: configcompression.TypeWithLevel{Type: configcompression.TypeGzip, Level: -1}, }, shouldErr: false, host: &mockHost{ @@ -469,10 +449,10 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) { transport := client.Transport // Compression should wrap Auth, unwrap it - if tt.settings.Compression.IsCompressed() { + if tt.settings.Compression.Type.IsCompressed() { ct, ok := transport.(*compressRoundTripper) assert.True(t, ok) - assert.Equal(t, tt.settings.Compression, ct.compressionType.Type) + assert.Equal(t, tt.settings.Compression.Type, ct.compressionType.Type) transport = ct.rt }