Skip to content

Commit

Permalink
[chore][pkg/stanza] Minor cleanup in split package (open-telemetry#27075
Browse files Browse the repository at this point in the history
)
djaglowski authored and jmsnll committed Nov 12, 2023
1 parent 685cf42 commit be9fefb
Showing 2 changed files with 33 additions and 29 deletions.
42 changes: 23 additions & 19 deletions pkg/stanza/split/split.go
Original file line number Diff line number Diff line change
@@ -20,33 +20,38 @@ type Config struct {
}

// Func will return a bufio.SplitFunc based on the config
func (c Config) Func(enc encoding.Encoding, flushAtEOF bool, maxLogSize int) (splitFunc bufio.SplitFunc, err error) {
switch {
case c.LineEndPattern != "" && c.LineStartPattern != "":
return nil, fmt.Errorf("only one of line_start_pattern or line_end_pattern can be set")
case enc == encoding.Nop && (c.LineEndPattern != "" || c.LineStartPattern != ""):
return nil, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding")
case enc == encoding.Nop:
return NoSplitFunc(maxLogSize), nil
case c.LineEndPattern == "" && c.LineStartPattern == "":
splitFunc, err = NewlineSplitFunc(enc, flushAtEOF)
if err != nil {
return nil, err
func (c Config) Func(enc encoding.Encoding, flushAtEOF bool, maxLogSize int) (bufio.SplitFunc, error) {
if enc == encoding.Nop {
if c.LineEndPattern != "" {
return nil, fmt.Errorf("line_end_pattern should not be set when using nop encoding")
}
if c.LineStartPattern != "" {
return nil, fmt.Errorf("line_start_pattern should not be set when using nop encoding")
}
case c.LineEndPattern != "":
return NoSplitFunc(maxLogSize), nil
}

if c.LineEndPattern == "" && c.LineStartPattern == "" {
return NewlineSplitFunc(enc, flushAtEOF)
}

if c.LineEndPattern != "" && c.LineStartPattern == "" {
re, err := regexp.Compile("(?m)" + c.LineEndPattern)
if err != nil {
return nil, fmt.Errorf("compile line end regex: %w", err)
}
splitFunc = LineEndSplitFunc(re, c.OmitPattern, flushAtEOF)
case c.LineStartPattern != "":
return LineEndSplitFunc(re, c.OmitPattern, flushAtEOF), nil
}

if c.LineEndPattern == "" && c.LineStartPattern != "" {
re, err := regexp.Compile("(?m)" + c.LineStartPattern)
if err != nil {
return nil, fmt.Errorf("compile line start regex: %w", err)
}
splitFunc = LineStartSplitFunc(re, c.OmitPattern, flushAtEOF)
return LineStartSplitFunc(re, c.OmitPattern, flushAtEOF), nil
}
return splitFunc, nil

return nil, fmt.Errorf("only one of line_start_pattern or line_end_pattern can be set")
}

// LineStartSplitFunc creates a bufio.SplitFunc that splits an incoming stream into
@@ -61,8 +66,7 @@ func LineStartSplitFunc(re *regexp.Regexp, omitPattern bool, flushAtEOF bool) bu
}
return 0, nil, nil // read more data and try again.
}
firstMatchStart := firstLoc[0]
firstMatchEnd := firstLoc[1]
firstMatchStart, firstMatchEnd := firstLoc[0], firstLoc[1]

if firstMatchStart != 0 {
// the beginning of the file does not match the start pattern, so return a token up to the first match so we don't lose data
20 changes: 10 additions & 10 deletions pkg/stanza/split/split_test.go
Original file line number Diff line number Diff line change
@@ -41,6 +41,16 @@ func TestConfigFunc(t *testing.T) {
assert.Equal(t, raw[:maxLogSize], token)
})

t.Run("NopEncodingError", func(t *testing.T) {
endCfg := Config{LineEndPattern: "\n"}
_, err := endCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_end_pattern should not be set when using nop encoding"))

startCfg := Config{LineStartPattern: "\n"}
_, err = startCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern should not be set when using nop encoding"))
})

t.Run("Newline", func(t *testing.T) {
cfg := Config{}
f, err := cfg.Func(unicode.UTF8, false, maxLogSize)
@@ -778,16 +788,6 @@ func TestNoSplitFunc(t *testing.T) {
}
}

func TestNoopEncodingError(t *testing.T) {
endCfg := Config{LineEndPattern: "\n"}
_, err := endCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding"))

startCfg := Config{LineStartPattern: "\n"}
_, err = startCfg.Func(encoding.Nop, false, 0)
require.Equal(t, err, fmt.Errorf("line_start_pattern or line_end_pattern should not be set when using nop encoding"))
}

func TestNewlineSplitFunc_Encodings(t *testing.T) {
cases := []struct {
name string

0 comments on commit be9fefb

Please sign in to comment.