| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package configcompression // import "go.opentelemetry.io/collector/config/configcompression" |
| 5 |
|
| 6 |
import ( |
| 7 |
"compress/zlib" |
| 8 |
"fmt" |
| 9 |
) |
| 10 |
|
| 11 |
// Type represents a compression method |
| 12 |
type Type string |
| 13 |
|
| 14 |
type Level int |
| 15 |
|
| 16 |
type CompressionParams struct { |
| 17 |
Level Level `mapstructure:"level"` |
| 18 |
// prevent unkeyed literal initialization |
| 19 |
_ struct{} |
| 20 |
} |
| 21 |
|
| 22 |
const ( |
| 23 |
TypeGzip Type = "gzip" |
| 24 |
TypeZlib Type = "zlib" |
| 25 |
TypeDeflate Type = "deflate" |
| 26 |
TypeSnappy Type = "snappy" |
| 27 |
TypeSnappyFramed Type = "x-snappy-framed" |
| 28 |
TypeZstd Type = "zstd" |
| 29 |
TypeLz4 Type = "lz4" |
| 30 |
typeNone Type = "none" |
| 31 |
typeEmpty Type = "" |
| 32 |
DefaultCompressionLevel = zlib.DefaultCompression |
| 33 |
) |
| 34 |
|
| 35 |
// IsCompressed returns false if CompressionType is nil, none, or empty. |
| 36 |
// Otherwise, returns true. |
| 37 |
func (ct *Type) IsCompressed() bool { |
| 38 |
return *ct != typeEmpty && *ct != typeNone |
| 39 |
} |
| 40 |
|
| 41 |
func (ct *Type) UnmarshalText(in []byte) error { |
| 42 |
typ := Type(in) |
| 43 |
if typ == TypeGzip || |
| 44 |
typ == TypeZlib || |
| 45 |
typ == TypeDeflate || |
| 46 |
typ == TypeSnappy || |
| 47 |
typ == TypeSnappyFramed || |
| 48 |
typ == TypeZstd || |
| 49 |
typ == TypeLz4 || |
| 50 |
typ == typeNone || |
| 51 |
typ == typeEmpty { |
| 52 |
*ct = typ |
| 53 |
return nil |
| 54 |
} |
| 55 |
return fmt.Errorf("unsupported compression type %q", typ) |
| 56 |
} |
| 57 |
|
| 58 |
func (ct *Type) ValidateParams(p CompressionParams) error { |
| 59 |
switch *ct { |
| 60 |
case TypeGzip, TypeZlib, TypeDeflate: |
| 61 |
if p.Level == zlib.DefaultCompression || |
| 62 |
p.Level == zlib.HuffmanOnly || |
| 63 |
p.Level == zlib.NoCompression || |
| 64 |
(p.Level >= zlib.BestSpeed && p.Level <= zlib.BestCompression) { |
| 65 |
return nil |
| 66 |
} |
| 67 |
case TypeZstd: |
| 68 |
// Supports arbitrary levels: zstd will map any given |
| 69 |
// level to the nearest internally supported level. |
| 70 |
return nil |
| 71 |
} |
| 72 |
if p.Level != 0 { |
| 73 |
return fmt.Errorf("unsupported parameters {Level:%+v} for compression type %q", p.Level, *ct) |
| 74 |
} |
| 75 |
return nil |
| 76 |
} |