diff --git a/pkg/gatherers/common/gather_logs.go b/pkg/gatherers/common/gather_logs.go index 400497729..b0219059c 100644 --- a/pkg/gatherers/common/gather_logs.go +++ b/pkg/gatherers/common/gather_logs.go @@ -164,6 +164,11 @@ func FilterLogFromScanner(scanner *bufio.Scanner, messagesToSearch []string, reg cb func(lines []string) []string) (string, error) { var result []string + var messagesRegexp *regexp.Regexp + if regexSearch { + messagesRegexp = regexp.MustCompile(strings.Join(messagesToSearch, "|")) + } + for scanner.Scan() { line := scanner.Text() if len(messagesToSearch) == 0 { @@ -171,17 +176,16 @@ func FilterLogFromScanner(scanner *bufio.Scanner, messagesToSearch []string, reg continue } - for _, messageToSearch := range messagesToSearch { - if regexSearch { - matches, err := regexp.MatchString(messageToSearch, line) - if err != nil { - return "", err - } - if matches { + if regexSearch && messagesRegexp != nil { + matches := messagesRegexp.MatchString(line) + if matches { + result = append(result, line) + } + } else { + for _, messageToSearch := range messagesToSearch { + if strings.Contains(strings.ToLower(line), strings.ToLower(messageToSearch)) { result = append(result, line) } - } else if strings.Contains(strings.ToLower(line), strings.ToLower(messageToSearch)) { - result = append(result, line) } } } diff --git a/pkg/gatherers/common/gather_logs_test.go b/pkg/gatherers/common/gather_logs_test.go index 87730d5ab..3cd010057 100644 --- a/pkg/gatherers/common/gather_logs_test.go +++ b/pkg/gatherers/common/gather_logs_test.go @@ -1,8 +1,11 @@ package common import ( + "bufio" "context" "fmt" + "runtime" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -13,6 +16,29 @@ import ( "github.com/openshift/insights-operator/pkg/utils/marshal" ) +// nolint: lll, misspell +var testText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. +Ullamcorper eget nulla facilisi etiam dignissim diam quis enim. +Rhoncus mattis rhoncus urna neque viverra. +Tempus urna et pharetra pharetra massa. Enim tortor at auctor urna nunc. +Id volutpat lacus laoreet non curabitur. +Feugiat pretium nibh ipsum consequat nisl vel. +Morbi tristique senectus et netus. +Tellus mauris a diam maecenas sed enim ut sem viverra. +Nunc scelerisque viverra mauris in aliquam. +Facilisis volutpat est velit egestas. Et netus et malesuada fames ac turpis egestas. +Sapien eget mi proin sed libero enim sed. Urna id volutpat lacus laoreet non. +Scelerisque eu ultrices vitae auctor. +Volutpat maecenas volutpat blandit aliquam etiam. +Sit amet nisl purus in mollis nunc sed id. +Tortor at auctor urna nunc id. +Purus in mollis nunc sed. +Enim ut tellus elementum sagittis vitae et leo.Quis viverra nibh cras pulvinar mattis nunc sed blandit libero. +Morbi tempus iaculis urna id volutpat lacus laoreet. +Pellentesque elit ullamcorper dignissim cras tincidunt lobortis. +Vitae proin sagittis nisl rhoncus. +Tortor condimentum lacinia quis vel eros donec ac odio tempor.` + func testGatherLogs(t *testing.T, regexSearch bool, stringToSearch string, shouldExist bool) { const testPodName = "test" const testLogFileName = "errors" @@ -101,3 +127,65 @@ func Test_GatherLogs(t *testing.T) { testGatherLogs(t, true, "[0-9]99", false) }) } + +func Test_FilterLogFromScanner(t *testing.T) { + tests := []struct { + name string + messagesToSearch []string + isRegexSearch bool + expectedOutput string + }{ + { + name: "simple non-regex search", + messagesToSearch: []string{"Pellentesque"}, + isRegexSearch: false, + expectedOutput: "Pellentesque elit ullamcorper dignissim cras tincidunt lobortis.", + }, + { + name: "non-regex search with empty messages", + messagesToSearch: []string{}, + isRegexSearch: false, + expectedOutput: testText, + }, + { + name: "advanced non-regex search", + messagesToSearch: []string{"Pellentesque", "scelerisque", "this is not there"}, + isRegexSearch: false, + // nolint lll + expectedOutput: "Nunc scelerisque viverra mauris in aliquam.\nScelerisque eu ultrices vitae auctor.\nPellentesque elit ullamcorper dignissim cras tincidunt lobortis.", + }, + { + name: "Regex search with empty messages", + messagesToSearch: []string{}, + isRegexSearch: true, + expectedOutput: testText, + }, + { + name: "advanced regex search", + messagesToSearch: []string{"Pellentesque", "scelerisque", "this is not there"}, + isRegexSearch: true, + expectedOutput: "Nunc scelerisque viverra mauris in aliquam.\nPellentesque elit ullamcorper dignissim cras tincidunt lobortis.", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reader := strings.NewReader(testText) + result, err := FilterLogFromScanner(bufio.NewScanner(reader), tt.messagesToSearch, tt.isRegexSearch, nil) + assert.NoError(t, err) + assert.Equal(t, tt.expectedOutput, result) + }) + } +} + +func Benchmark_FilterLogFromScanner(b *testing.B) { + var m runtime.MemStats + messagesToSearch := []string{"Pellentesque", "scelerisque", "this is not there"} + reader := strings.NewReader(testText) + for i := 0; i <= b.N; i++ { + // nolint errcheck + FilterLogFromScanner(bufio.NewScanner(reader), messagesToSearch, true, nil) + runtime.ReadMemStats(&m) + b.Logf("Size of allocated heap objects: %d MB, Size of heap in use: %d MB", m.Alloc/1024/1024, m.HeapInuse/1024/1024) + } +}