From e67e78e10a47b52f73715ba29dc8b6822feb541a Mon Sep 17 00:00:00 2001 From: Dominik Rosiek Date: Wed, 7 Jul 2021 08:35:41 +0200 Subject: [PATCH] fix(multiline): use proper advance and strip new line whitespaces from beginning of log Signed-off-by: Dominik Rosiek --- operator/helper/multiline.go | 13 ++++++++----- operator/helper/multiline_test.go | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/operator/helper/multiline.go b/operator/helper/multiline.go index 5febc984..7aed4e2a 100644 --- a/operator/helper/multiline.go +++ b/operator/helper/multiline.go @@ -80,7 +80,7 @@ func NewLineStartSplitFunc(re *regexp.Regexp, flushAtEOF bool) bufio.SplitFunc { // Flush if no more data is expected if len(data) != 0 && atEOF && flushAtEOF { token = trimWhitespaces(data) - advance = len(token) + advance = len(data) return } return 0, nil, nil // read more data and try again. @@ -103,7 +103,7 @@ func NewLineStartSplitFunc(re *regexp.Regexp, flushAtEOF bool) bufio.SplitFunc { // Flush if no more data is expected if atEOF && flushAtEOF { token = trimWhitespaces(data) - advance = len(token) + advance = len(data) return } @@ -130,7 +130,7 @@ func NewLineEndSplitFunc(re *regexp.Regexp, flushAtEOF bool) bufio.SplitFunc { // Flush if no more data is expected if len(data) != 0 && atEOF && flushAtEOF { token = trimWhitespaces(data) - advance = len(token) + advance = len(data) return } return 0, nil, nil // read more data and try again @@ -175,7 +175,7 @@ func NewNewlineSplitFunc(encoding encoding.Encoding, flushAtEOF bool) (bufio.Spl // Flush if no more data is expected if atEOF && flushAtEOF { token = trimWhitespaces(data) - advance = len(token) + advance = len(data) return } @@ -197,5 +197,8 @@ func encodedCarriageReturn(encoding encoding.Encoding) ([]byte, error) { } func trimWhitespaces(data []byte) []byte { - return bytes.TrimRight(data, "\r\n\t ") + // TrimLeft to strip EOF whitespaces in case of using $ in regex + // For some reason newline and carriage return are being moved to beginning of next log + // TrimRight to strip all whitespaces from the end of log + return bytes.TrimLeft(bytes.TrimRight(data, "\r\n\t "), "\r\n") } diff --git a/operator/helper/multiline_test.go b/operator/helper/multiline_test.go index 92b1b52f..298b2de6 100644 --- a/operator/helper/multiline_test.go +++ b/operator/helper/multiline_test.go @@ -190,7 +190,7 @@ func TestLineEndSplitFunc(t *testing.T) { Raw: []byte("log1 LOGEND LOGEND\nlog2 LOGEND\n"), ExpectedTokenized: []string{ "log1 LOGEND LOGEND", - "\nlog2 LOGEND", + "log2 LOGEND", }, }, {