Skip to content

Commit

Permalink
[pkg/stanza] Deprecate EncodingConfig (#25846)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Aug 17, 2023
1 parent 0bcaca9 commit 40d8176
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 79 deletions.
27 changes: 27 additions & 0 deletions .chloggen/pkg-stanza-deprecate-encodingconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: deprecation

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Deprecate 'helper.EncodingConfig' and 'helper.NewEncodingConfig'

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25846]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
27 changes: 27 additions & 0 deletions .chloggen/pkg-stanza-deprecate-encodingconfig2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: breaking

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: pkg/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Remove deprecated 'helper.Encoding' and 'helper.EncodingConfig.Build'

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [25846]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
11 changes: 8 additions & 3 deletions pkg/stanza/fileconsumer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c Config) buildManager(logger *zap.SugaredLogger, emit emit.Callback, fact

var hCfg *header.Config
if c.Header != nil {
enc, err := helper.LookupEncoding(c.Splitter.EncodingConfig.Encoding)
enc, err := helper.LookupEncoding(c.Splitter.Encoding)
if err != nil {
return nil, fmt.Errorf("failed to create encoding: %w", err)
}
Expand All @@ -146,6 +146,11 @@ func (c Config) buildManager(logger *zap.SugaredLogger, emit emit.Callback, fact
return nil, err
}

enc, err := helper.LookupEncoding(c.Splitter.Encoding)
if err != nil {
return nil, err
}

return &Manager{
SugaredLogger: logger.With("component", "fileconsumer"),
cancel: func() {},
Expand All @@ -162,7 +167,7 @@ func (c Config) buildManager(logger *zap.SugaredLogger, emit emit.Callback, fact
},
fromBeginning: startAtBeginning,
splitterFactory: factory,
encodingConfig: c.Splitter.EncodingConfig,
encoding: enc,
headerConfig: hCfg,
},
fileMatcher: fileMatcher,
Expand Down Expand Up @@ -213,7 +218,7 @@ func (c Config) validate() error {
return errors.New("`max_batches` must not be negative")
}

enc, err := helper.LookupEncoding(c.Splitter.EncodingConfig.Encoding)
enc, err := helper.LookupEncoding(c.Splitter.Encoding)
if err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/stanza/fileconsumer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,15 @@ func TestUnmarshal(t *testing.T) {
Name: "encoding_lower",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "utf-16le"}
cfg.Splitter.Encoding = "utf-16le"
return newMockOperatorConfig(cfg)
}(),
},
{
Name: "encoding_upper",
Expect: func() *mockOperatorConfig {
cfg := NewConfig()
cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "UTF-16lE"}
cfg.Splitter.Encoding = "UTF-16lE"
return newMockOperatorConfig(cfg)
}(),
},
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestBuild(t *testing.T) {
{
"InvalidEncoding",
func(f *Config) {
f.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "UTF-3233"}
f.Splitter.Encoding = "UTF-3233"
},
require.Error,
nil,
Expand Down Expand Up @@ -680,7 +680,7 @@ func TestBuildWithSplitFunc(t *testing.T) {
{
"InvalidEncoding",
func(f *Config) {
f.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "UTF-3233"}
f.Splitter.Encoding = "UTF-3233"
},
require.Error,
nil,
Expand Down
6 changes: 3 additions & 3 deletions pkg/stanza/fileconsumer/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func TestReadUsingNopEncoding(t *testing.T) {
cfg := NewConfig().includeDir(tempDir)
cfg.StartAt = "beginning"
cfg.MaxLogSize = 8
cfg.Splitter.EncodingConfig.Encoding = "nop"
cfg.Splitter.Encoding = "nop"
operator, emitCalls := buildTestManager(t, cfg)

// Create a file, then start
Expand Down Expand Up @@ -419,7 +419,7 @@ func TestNopEncodingDifferentLogSizes(t *testing.T) {
cfg := NewConfig().includeDir(tempDir)
cfg.StartAt = "beginning"
cfg.MaxLogSize = tc.maxLogSize
cfg.Splitter.EncodingConfig.Encoding = "nop"
cfg.Splitter.Encoding = "nop"
operator, emitCalls := buildTestManager(t, cfg)

// Create a file, then start
Expand Down Expand Up @@ -1287,7 +1287,7 @@ func TestEncodings(t *testing.T) {
tempDir := t.TempDir()
cfg := NewConfig().includeDir(tempDir)
cfg.StartAt = "beginning"
cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: tc.encoding}
cfg.Splitter.Encoding = tc.encoding
operator, emitCalls := buildTestManager(t, cfg)

// Populate the file
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/fileconsumer/internal/splitter/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewMultilineFactory(splitter helper.SplitterConfig) Factory {

// Build builds Multiline Splitter struct
func (factory *multilineFactory) Build(maxLogSize int) (bufio.SplitFunc, error) {
enc, err := helper.LookupEncoding(factory.EncodingConfig.Encoding)
enc, err := helper.LookupEncoding(factory.Encoding)
if err != nil {
return nil, err
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/stanza/fileconsumer/internal/splitter/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ func TestMultilineBuild(t *testing.T) {
{
name: "eoncoding error",
splitterConfig: helper.SplitterConfig{
EncodingConfig: helper.EncodingConfig{
Encoding: "error",
},
Encoding: "error",
Flusher: helper.NewFlusherConfig(),
Multiline: helper.NewMultilineConfig(),
},
Expand All @@ -46,8 +44,8 @@ func TestMultilineBuild(t *testing.T) {
{
name: "Multiline error",
splitterConfig: helper.SplitterConfig{
EncodingConfig: helper.NewEncodingConfig(),
Flusher: helper.NewFlusherConfig(),
Encoding: "utf-8",
Flusher: helper.NewFlusherConfig(),
Multiline: helper.MultilineConfig{
LineStartPattern: "START",
LineEndPattern: "END",
Expand Down
9 changes: 3 additions & 6 deletions pkg/stanza/fileconsumer/reader_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime"

"go.uber.org/zap"
"golang.org/x/text/encoding"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/fingerprint"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/fileconsumer/internal/header"
Expand All @@ -23,7 +24,7 @@ type readerFactory struct {
readerConfig *readerConfig
fromBeginning bool
splitterFactory splitter.Factory
encodingConfig helper.EncodingConfig
encoding encoding.Encoding
headerConfig *header.Config
}

Expand Down Expand Up @@ -86,11 +87,7 @@ func (b readerBuilder) build() (r *reader, err error) {
}
}

encoding, err := helper.LookupEncoding(b.encodingConfig.Encoding)
if err != nil {
return nil, err
}
r.decoder = helper.NewDecoder(encoding)
r.decoder = helper.NewDecoder(b.encoding)

if b.headerConfig == nil || b.readerMetadata.HeaderFinalized {
r.splitFunc = r.lineSplitFunc
Expand Down
15 changes: 7 additions & 8 deletions pkg/stanza/fileconsumer/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func TestTokenizationTooLongWithLineStartPattern(t *testing.T) {
mlc := helper.NewMultilineConfig()
mlc.LineStartPattern = `\d+-\d+-\d+`
f.splitterFactory = splitter.NewMultilineFactory(helper.SplitterConfig{
EncodingConfig: helper.NewEncodingConfig(),
Flusher: helper.NewFlusherConfig(),
Multiline: mlc,
Encoding: "utf-8",
Flusher: helper.NewFlusherConfig(),
Multiline: mlc,
})
f.readerConfig.maxLogSize = 15

Expand Down Expand Up @@ -173,10 +173,7 @@ func TestHeaderFingerprintIncluded(t *testing.T) {
regexConf := regex.NewConfig()
regexConf.Regex = "^#(?P<header>.*)"

encodingConf := helper.EncodingConfig{
Encoding: "utf-8",
}
enc, err := helper.LookupEncoding(encodingConf.Encoding)
enc, err := helper.LookupEncoding("utf-8")
require.NoError(t, err)

h, err := header.NewConfig("^#", []operator.Config{{Builder: regexConf}}, enc)
Expand All @@ -202,6 +199,8 @@ func TestHeaderFingerprintIncluded(t *testing.T) {
func testReaderFactory(t *testing.T) (*readerFactory, chan *emitParams) {
emitChan := make(chan *emitParams, 100)
splitterConfig := helper.NewSplitterConfig()
enc, err := helper.LookupEncoding(splitterConfig.Encoding)
require.NoError(t, err)
return &readerFactory{
SugaredLogger: testutil.Logger(t),
readerConfig: &readerConfig{
Expand All @@ -211,7 +210,7 @@ func testReaderFactory(t *testing.T) (*readerFactory, chan *emitParams) {
},
fromBeginning: true,
splitterFactory: splitter.NewMultilineFactory(splitterConfig),
encodingConfig: splitterConfig.EncodingConfig,
encoding: enc,
}, emitChan
}

Expand Down
25 changes: 2 additions & 23 deletions pkg/stanza/operator/helper/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,18 @@ import (
"golang.org/x/text/transform"
)

// NewEncodingConfig creates a new Encoding config
// Deprecated: [v0.84.0]
func NewEncodingConfig() EncodingConfig {
return EncodingConfig{
Encoding: "utf-8",
}
}

// EncodingConfig is the configuration of an Encoding helper
// Deprecated: [v0.84.0]
type EncodingConfig struct {
Encoding string `mapstructure:"encoding,omitempty"`
}

// Deprecated: [v0.83.0] Use NewDecoder instead.
func (c EncodingConfig) Build() (Encoding, error) {
enc, err := LookupEncoding(c.Encoding)
if err != nil {
return Encoding{}, err
}

return Encoding{
Encoding: enc,
decodeBuffer: make([]byte, 1<<12),
decoder: enc.NewDecoder(),
}, nil
}

// Deprecated: [v0.83.0] Use Decoder instead.
type Encoding struct {
Encoding encoding.Encoding
decoder *encoding.Decoder
decodeBuffer []byte
}

type Decoder struct {
encoding encoding.Encoding
decoder *encoding.Decoder
Expand Down
10 changes: 5 additions & 5 deletions pkg/stanza/operator/helper/splitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "bufio"

// SplitterConfig consolidates MultilineConfig and FlusherConfig
type SplitterConfig struct {
EncodingConfig EncodingConfig `mapstructure:",squash,omitempty"`
Encoding string `mapstructure:"encoding,omitempty"`
Flusher FlusherConfig `mapstructure:",squash,omitempty"`
Multiline MultilineConfig `mapstructure:"multiline,omitempty"`
PreserveLeadingWhitespaces bool `mapstructure:"preserve_leading_whitespaces,omitempty"`
Expand All @@ -17,15 +17,15 @@ type SplitterConfig struct {
// NewSplitterConfig returns default SplitterConfig
func NewSplitterConfig() SplitterConfig {
return SplitterConfig{
EncodingConfig: NewEncodingConfig(),
Multiline: NewMultilineConfig(),
Flusher: NewFlusherConfig(),
Encoding: "utf-8",
Multiline: NewMultilineConfig(),
Flusher: NewFlusherConfig(),
}
}

// Build builds Splitter struct
func (c *SplitterConfig) Build(flushAtEOF bool, maxLogSize int) (*Splitter, error) {
enc, err := LookupEncoding(c.EncodingConfig.Encoding)
enc, err := LookupEncoding(c.Encoding)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/input/file/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
var toBody toBodyFunc = func(token []byte) interface{} {
return string(token)
}
if helper.IsNop(c.Config.Splitter.EncodingConfig.Encoding) {
if helper.IsNop(c.Config.Splitter.Encoding) {
toBody = func(token []byte) interface{} {
copied := make([]byte, len(token))
copy(copied, token)
Expand Down
6 changes: 3 additions & 3 deletions pkg/stanza/operator/input/file/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func TestUnmarshal(t *testing.T) {
ExpectErr: false,
Expect: func() *Config {
cfg := NewConfig()
cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "utf-16le"}
cfg.Splitter.Encoding = "utf-16le"
return cfg
}(),
},
Expand All @@ -421,7 +421,7 @@ func TestUnmarshal(t *testing.T) {
ExpectErr: false,
Expect: func() *Config {
cfg := NewConfig()
cfg.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "UTF-16lE"}
cfg.Splitter.Encoding = "UTF-16lE"
return cfg
}(),
},
Expand Down Expand Up @@ -509,7 +509,7 @@ func TestBuild(t *testing.T) {
{
"InvalidEncoding",
func(f *Config) {
f.Splitter.EncodingConfig = helper.EncodingConfig{Encoding: "UTF-3233"}
f.Splitter.Encoding = "UTF-3233"
},
require.Error,
nil,
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/operator/input/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestReadUsingNopEncoding(t *testing.T) {
t.Run(tc.testName, func(t *testing.T) {
operator, logReceived, tempDir := newTestFileOperator(t, func(cfg *Config) {
cfg.MaxLogSize = 8
cfg.Splitter.EncodingConfig.Encoding = "nop"
cfg.Splitter.Encoding = "nop"
})
// Create a file, then start
temp := openTemp(t, tempDir)
Expand Down
6 changes: 2 additions & 4 deletions pkg/stanza/operator/input/syslog/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func TestUnmarshal(t *testing.T) {
cfg.TCP.MaxLogSize = 1000000
cfg.TCP.ListenAddress = "10.0.0.1:9000"
cfg.TCP.AddAttributes = true
cfg.TCP.Encoding = helper.NewEncodingConfig()
cfg.TCP.Encoding.Encoding = "utf-16"
cfg.TCP.Encoding = "utf-16"
cfg.TCP.Multiline = helper.NewMultilineConfig()
cfg.TCP.Multiline.LineStartPattern = "ABC"
cfg.TCP.TLS = &configtls.TLSServerSetting{
Expand All @@ -62,8 +61,7 @@ func TestUnmarshal(t *testing.T) {
cfg.UDP = &udp.NewConfig().BaseConfig
cfg.UDP.ListenAddress = "10.0.0.1:9000"
cfg.UDP.AddAttributes = true
cfg.UDP.Encoding = helper.NewEncodingConfig()
cfg.UDP.Encoding.Encoding = "utf-16"
cfg.UDP.Encoding = "utf-16"
cfg.UDP.Multiline = helper.NewMultilineConfig()
cfg.UDP.Multiline.LineStartPattern = "ABC"
return cfg
Expand Down
Loading

0 comments on commit 40d8176

Please sign in to comment.