Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pkg/stanza] SplitFunc naming cleanup #26631

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .chloggen/pkg-stanza-split-func-factory.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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: Rename syslog and tcp MultilineBuilders

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

# (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: |
- Rename syslog.OctetMultiLineBuilder to syslog.OctetSplitFuncBuilder
- Rename tc.MultilineBuilder to tcp.SplitFuncBuilder

# 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]
6 changes: 3 additions & 3 deletions pkg/stanza/fileconsumer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (c Config) Build(logger *zap.SugaredLogger, emit emit.Callback) (*Manager,
}

// Ensure that splitter is buildable
factory := splitter.NewMultilineFactory(c.SplitConfig, enc, int(c.MaxLogSize), c.TrimConfig.Func(), c.FlushPeriod)
if _, err := factory.Build(); err != nil {
factory := splitter.NewSplitFuncFactory(c.SplitConfig, enc, int(c.MaxLogSize), c.TrimConfig.Func(), c.FlushPeriod)
if _, err := factory.SplitFunc(); err != nil {
return nil, err
}

Expand All @@ -120,7 +120,7 @@ func (c Config) BuildWithSplitFunc(logger *zap.SugaredLogger, emit emit.Callback

// Ensure that splitter is buildable
factory := splitter.NewCustomFactory(splitFunc, c.FlushPeriod)
if _, err := factory.Build(); err != nil {
if _, err := factory.SplitFunc(); err != nil {
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/fileconsumer/internal/splitter/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewCustomFactory(splitFunc bufio.SplitFunc, flushPeriod time.Duration) Fact
}
}

// Build builds Multiline Splitter struct
func (f *customFactory) Build() (bufio.SplitFunc, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments should also be updated to SplitFunc

// SplitFunc builds a bufio.SplitFunc based on the configuration
func (f *customFactory) SplitFunc() (bufio.SplitFunc, error) {
return flush.WithPeriod(f.splitFunc, trim.Nop, f.flushPeriod), nil
}
2 changes: 1 addition & 1 deletion pkg/stanza/fileconsumer/internal/splitter/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCustomFactory(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
factory := NewCustomFactory(tt.splitter, tt.flushPeriod)
got, err := factory.Build()
got, err := factory.SplitFunc()
if (err != nil) != tt.wantErr {
t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/stanza/fileconsumer/internal/splitter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import (
)

type Factory interface {
Build() (bufio.SplitFunc, error)
SplitFunc() (bufio.SplitFunc, error)
}
36 changes: 18 additions & 18 deletions pkg/stanza/fileconsumer/internal/splitter/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,35 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)

type multilineFactory struct {
multilineCfg split.Config
encoding encoding.Encoding
maxLogSize int
trimFunc trim.Func
flushPeriod time.Duration
type splitFuncFactory struct {
splitConfig split.Config
encoding encoding.Encoding
maxLogSize int
trimFunc trim.Func
flushPeriod time.Duration
}

var _ Factory = (*multilineFactory)(nil)
var _ Factory = (*splitFuncFactory)(nil)

func NewMultilineFactory(
multilineCfg split.Config,
func NewSplitFuncFactory(
splitConfig split.Config,
encoding encoding.Encoding,
maxLogSize int,
trimFunc trim.Func,
flushPeriod time.Duration,
) Factory {
return &multilineFactory{
multilineCfg: multilineCfg,
encoding: encoding,
maxLogSize: maxLogSize,
trimFunc: trimFunc,
flushPeriod: flushPeriod,
return &splitFuncFactory{
splitConfig: splitConfig,
encoding: encoding,
maxLogSize: maxLogSize,
trimFunc: trimFunc,
flushPeriod: flushPeriod,
}
}

// Build builds Multiline Splitter struct
func (f *multilineFactory) Build() (bufio.SplitFunc, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

splitFunc, err := f.multilineCfg.Func(f.encoding, false, f.maxLogSize, f.trimFunc)
// SplitFunc builds a bufio.SplitFunc based on the configuration
func (f *splitFuncFactory) SplitFunc() (bufio.SplitFunc, error) {
splitFunc, err := f.splitConfig.Func(f.encoding, false, f.maxLogSize, f.trimFunc)
if err != nil {
return nil, err
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/stanza/fileconsumer/internal/splitter/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/trim"
)

func TestMultilineBuild(t *testing.T) {
func TestSplitFuncFactory(t *testing.T) {
tests := []struct {
name string
multilineCfg split.Config
encoding encoding.Encoding
maxLogSize int
flushPeriod time.Duration
wantErr bool
name string
splitConfig split.Config
encoding encoding.Encoding
maxLogSize int
flushPeriod time.Duration
wantErr bool
}{
{
name: "default configuration",
Expand All @@ -32,8 +32,8 @@ func TestMultilineBuild(t *testing.T) {
wantErr: false,
},
{
name: "Multiline error",
multilineCfg: split.Config{
name: "split config error",
splitConfig: split.Config{
LineStartPattern: "START",
LineEndPattern: "END",
},
Expand All @@ -45,10 +45,10 @@ func TestMultilineBuild(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
factory := NewMultilineFactory(tt.multilineCfg, tt.encoding, tt.maxLogSize, trim.Nop, tt.flushPeriod)
got, err := factory.Build()
factory := NewSplitFuncFactory(tt.splitConfig, tt.encoding, tt.maxLogSize, trim.Nop, tt.flushPeriod)
got, err := factory.SplitFunc()
if (err != nil) != tt.wantErr {
t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("SplitFunc() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/fileconsumer/reader_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type readerFactory struct {
}

func (f *readerFactory) newReader(file *os.File, fp *fingerprint.Fingerprint) (*reader, error) {
lineSplitFunc, err := f.splitterFactory.Build()
lineSplitFunc, err := f.splitterFactory.SplitFunc()
if err != nil {
return nil, err
}
Expand All @@ -44,7 +44,7 @@ func (f *readerFactory) copy(old *reader, newFile *os.File) (*reader, error) {
var err error
lineSplitFunc := old.lineSplitFunc
if lineSplitFunc == nil {
lineSplitFunc, err = f.splitterFactory.Build()
lineSplitFunc, err = f.splitterFactory.SplitFunc()
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/stanza/fileconsumer/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ func TestHeaderFingerprintIncluded(t *testing.T) {
func testReaderFactory(t *testing.T, sCfg split.Config, maxLogSize int, flushPeriod time.Duration) (*readerFactory, chan *emitParams) {
emitChan := make(chan *emitParams, 100)
enc, err := decode.LookupEncoding(defaultEncoding)
trimFunc := trim.Whitespace
require.NoError(t, err)
return &readerFactory{
SugaredLogger: testutil.Logger(t),
Expand All @@ -236,7 +235,7 @@ func testReaderFactory(t *testing.T, sCfg split.Config, maxLogSize int, flushPer
emit: testEmitFunc(emitChan),
},
fromBeginning: true,
splitterFactory: splitter.NewMultilineFactory(sCfg, enc, maxLogSize, trimFunc, flushPeriod),
splitterFactory: splitter.NewSplitFuncFactory(sCfg, enc, maxLogSize, trim.Whitespace, flushPeriod),
encoding: enc,
}, emitChan
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/stanza/operator/input/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
tcpInputCfg := tcp.NewConfigWithID(inputBase.ID() + "_internal_tcp")
tcpInputCfg.BaseConfig = *c.TCP
if syslogParserCfg.EnableOctetCounting {
tcpInputCfg.MultiLineBuilder = OctetMultiLineBuilder
tcpInputCfg.SplitFuncBuilder = OctetSplitFuncBuilder
}

tcpInput, err := tcpInputCfg.Build(logger)
Expand Down Expand Up @@ -144,7 +144,7 @@ func (t *Input) SetOutputs(operators []operator.Operator) error {
return t.parser.SetOutputs(operators)
}

func OctetMultiLineBuilder(_ encoding.Encoding) (bufio.SplitFunc, error) {
func OctetSplitFuncBuilder(_ encoding.Encoding) (bufio.SplitFunc, error) {
return newOctetFrameSplitFunc(true), nil
}

Expand Down
4 changes: 1 addition & 3 deletions pkg/stanza/operator/input/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,8 @@ func TestOctetFramingSplitFunc(t *testing.T) {
},
}
for _, tc := range testCases {
splitFunc, err := OctetMultiLineBuilder(nil)
splitFunc, err := OctetSplitFuncBuilder(nil)
require.NoError(t, err)
t.Run(tc.Name, tc.Run(splitFunc))
}
}

// TODO refactor test dependency away from internal?
12 changes: 6 additions & 6 deletions pkg/stanza/operator/input/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ type BaseConfig struct {
Encoding string `mapstructure:"encoding,omitempty"`
SplitConfig split.Config `mapstructure:"multiline,omitempty"`
TrimConfig trim.Config `mapstructure:",squash"`
MultiLineBuilder MultiLineBuilderFunc
SplitFuncBuilder SplitFuncBuilder
}

type MultiLineBuilderFunc func(enc encoding.Encoding) (bufio.SplitFunc, error)
type SplitFuncBuilder func(enc encoding.Encoding) (bufio.SplitFunc, error)

func (c Config) defaultMultilineBuilder(enc encoding.Encoding) (bufio.SplitFunc, error) {
trimFunc := c.TrimConfig.Func()
Expand Down Expand Up @@ -120,12 +120,12 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
return nil, err
}

if c.MultiLineBuilder == nil {
c.MultiLineBuilder = c.defaultMultilineBuilder
if c.SplitFuncBuilder == nil {
c.SplitFuncBuilder = c.defaultMultilineBuilder
}

// Build multiline
splitFunc, err := c.MultiLineBuilder(enc)
// Build split func
splitFunc, err := c.SplitFuncBuilder(enc)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/stanza/operator/input/udp/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ func (c Config) Build(logger *zap.SugaredLogger) (operator.Operator, error) {
return nil, err
}

// Build multiline
trimFunc := c.TrimConfig.Func()
splitFunc, err := c.SplitConfig.Func(enc, true, MaxUDPSize, trimFunc)
// Build SplitFunc
splitFunc, err := c.SplitConfig.Func(enc, true, MaxUDPSize, c.TrimConfig.Func())
if err != nil {
return nil, err
}
Expand Down