Skip to content

Commit

Permalink
Additional cleanup relating to multiline rename
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Sep 12, 2023
1 parent 984e850 commit 15ab4ad
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 32 deletions.
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]
4 changes: 2 additions & 2 deletions pkg/stanza/fileconsumer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (c Config) Build(logger *zap.SugaredLogger, emit emit.Callback) (*Manager,

// Ensure that splitter is buildable
factory := splitter.NewSplitFuncFactory(c.SplitConfig, enc, int(c.MaxLogSize), c.TrimConfig.Func(), c.FlushPeriod)
if _, err := factory.Build(); err != nil {
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) {
// 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)
}
4 changes: 2 additions & 2 deletions pkg/stanza/fileconsumer/internal/splitter/multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func NewSplitFuncFactory(
}
}

// Build builds Multiline Splitter struct
func (f *splitFuncFactory) Build() (bufio.SplitFunc, error) {
// 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
12 changes: 6 additions & 6 deletions pkg/stanza/fileconsumer/internal/splitter/multiline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestSplitFuncFactory(t *testing.T) {
tests := []struct {
name string
splitFunc split.Config
splitConfig split.Config
encoding encoding.Encoding
maxLogSize int
flushPeriod time.Duration
Expand All @@ -32,8 +32,8 @@ func TestSplitFuncFactory(t *testing.T) {
wantErr: false,
},
{
name: "Multiline error",
splitFunc: split.Config{
name: "split config error",
splitConfig: split.Config{
LineStartPattern: "START",
LineEndPattern: "END",
},
Expand All @@ -45,10 +45,10 @@ func TestSplitFuncFactory(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
factory := NewSplitFuncFactory(tt.splitFunc, 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.NewSplitFuncFactory(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

0 comments on commit 15ab4ad

Please sign in to comment.