Skip to content

Commit

Permalink
Changed the compression type in client configuration to be TypeWithLevel
Browse files Browse the repository at this point in the history
  • Loading branch information
rnishtala-sumo committed Oct 8, 2024
1 parent 9ed1c64 commit bc6ea67
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 44 deletions.
6 changes: 2 additions & 4 deletions config/configcompression/compressiontype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions config/confighttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 3 additions & 8 deletions config/confighttp/confighttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
34 changes: 7 additions & 27 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit bc6ea67

Please sign in to comment.