Files
5ef765b8bc Fix snappy/framed-snappy encoding/decoding (#12911)
#### Description
This is an alternative PR to #12825.

I'm taking all the commits from that PR and adding the feature gate on
the client side, as requested by reviews. The server behavior of peeking
into the initial bytes to identify the encoding is kept :)

If you've reviewed the previous PR, you can just review the latest
commit!

<!-- Issue number if applicable -->
#### Link to tracking issue
Fixes #10584

---------

Signed-off-by: Arthur Silva Sens <[email protected]>
Co-authored-by: Michael Graff <[email protected]>
Co-authored-by: Alex Boten <[email protected]>
Co-authored-by: Pablo Baeyens <[email protected]>
Co-authored-by: Jonathan <[email protected]>
2025-05-05 16:18:42 +00:00

77 lines
1.9 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package configcompression // import "go.opentelemetry.io/collector/config/configcompression"
import (
"compress/zlib"
"fmt"
)
// Type represents a compression method
type Type string
type Level int
type CompressionParams struct {
Level Level `mapstructure:"level"`
// prevent unkeyed literal initialization
_ struct{}
}
const (
TypeGzip Type = "gzip"
TypeZlib Type = "zlib"
TypeDeflate Type = "deflate"
TypeSnappy Type = "snappy"
TypeSnappyFramed Type = "x-snappy-framed"
TypeZstd Type = "zstd"
TypeLz4 Type = "lz4"
typeNone Type = "none"
typeEmpty Type = ""
DefaultCompressionLevel = zlib.DefaultCompression
)
// IsCompressed returns false if CompressionType is nil, none, or empty.
// Otherwise, returns true.
func (ct *Type) IsCompressed() bool {
return *ct != typeEmpty && *ct != typeNone
}
func (ct *Type) UnmarshalText(in []byte) error {
typ := Type(in)
if typ == TypeGzip ||
typ == TypeZlib ||
typ == TypeDeflate ||
typ == TypeSnappy ||
typ == TypeSnappyFramed ||
typ == TypeZstd ||
typ == TypeLz4 ||
typ == typeNone ||
typ == typeEmpty {
*ct = typ
return nil
}
return fmt.Errorf("unsupported compression type %q", typ)
}
func (ct *Type) ValidateParams(p CompressionParams) error {
switch *ct {
case TypeGzip, TypeZlib, TypeDeflate:
if p.Level == zlib.DefaultCompression ||
p.Level == zlib.HuffmanOnly ||
p.Level == zlib.NoCompression ||
(p.Level >= zlib.BestSpeed && p.Level <= zlib.BestCompression) {
return nil
}
case TypeZstd:
// Supports arbitrary levels: zstd will map any given
// level to the nearest internally supported level.
return nil
}
if p.Level != 0 {
return fmt.Errorf("unsupported parameters {Level:%+v} for compression type %q", p.Level, *ct)
}
return nil
}