Skip to content

Commit

Permalink
WIP Ignore log patterns (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
codekoala authored Jun 19, 2024
1 parent 1fffb5c commit 2ab5a04
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Config struct {
// IgnorePodNames optional list of pod name regexp patterns to ignore
IgnorePodNames []string `yaml:"ignorePodNames"`

// IgnoreLogPatterns optional list of regexp patterns to ignore
IgnoreLogPatterns []string `yaml:"ignoreLogPatterns"`

// Alert is a map contains a map of each provider configuration
// e.g. {"slack": {"webhook": "URL"}}
Alert map[string]map[string]interface{} `yaml:"alert"`
Expand All @@ -58,6 +61,10 @@ type Config struct {
// Patterns are compiled from IgnorePodNames after populating
// IgnorePodNames configuration
IgnorePodNamePatterns []*regexp.Regexp

// Patterns are compiled from IgnoreLogPatterns after populating
// IgnoreLogPatterns configuration
IgnoreLogPatternsCompiled []*regexp.Regexp
}

// App confing struct
Expand Down
9 changes: 6 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func TestConfigFromFile(t *testing.T) {
Namespaces: []string{"default", "!kwatch"},
Reasons: []string{"default", "!kwatch"},
IgnorePodNames: []string{"my-fancy-pod-[.*"},
IgnoreLogPatterns: []string{"leaderelection lost"},
App: App{
ProxyURL: "https://localhost",
ClusterName: "development",
Expand All @@ -99,23 +100,25 @@ func TestConfigFromFile(t *testing.T) {
assert.NotNil(err)
}

func TestGetCompiledIgnorePodNamePatterns(t *testing.T) {
func TestGetCompiledIgnorePatterns(t *testing.T) {
assert := assert.New(t)

validPatterns := []string{
"my-fancy-pod-[0-9]",
"leaderelection lost",
}

compiledPatterns, err := getCompiledIgnorePodNamePatterns(validPatterns)
compiledPatterns, err := getCompiledIgnorePatterns(validPatterns)

assert.Nil(err)
assert.True(compiledPatterns[0].MatchString("my-fancy-pod-8"))
assert.True(compiledPatterns[1].MatchString(`controllermanager.go:272] "leaderelection lost"`))

invalidPatterns := []string{
"my-fancy-pod-[.*",
}

compiledPatterns, err = getCompiledIgnorePodNamePatterns(invalidPatterns)
compiledPatterns, err = getCompiledIgnorePatterns(invalidPatterns)

assert.NotNil(err)
}
11 changes: 9 additions & 2 deletions config/loadConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ func LoadConfig() (*Config, error) {

// Prepare ignored pod name patters
config.IgnorePodNamePatterns, err =
getCompiledIgnorePodNamePatterns(config.IgnorePodNames)
getCompiledIgnorePatterns(config.IgnorePodNames)
if err != nil {
logrus.Errorf("Failed to compile pod name pattern: %s", err.Error())
}

// Prepare ignored log patterns
config.IgnoreLogPatternsCompiled, err =
getCompiledIgnorePatterns(config.IgnoreLogPatterns)
if err != nil {
logrus.Errorf("Failed to compile log pattern: %s", err.Error())
}

// Parse proxy config
if len(config.App.ProxyURL) > 0 {
os.Setenv("HTTPS_PROXY", config.App.ProxyURL)
Expand All @@ -77,7 +84,7 @@ func getAllowForbidSlices(items []string) (allow []string, forbid []string) {
return allow, forbid
}

func getCompiledIgnorePodNamePatterns(patterns []string) (compiledPatterns []*regexp.Regexp, err error) {
func getCompiledIgnorePatterns(patterns []string) (compiledPatterns []*regexp.Regexp, err error) {
compiledPatterns = make([]*regexp.Regexp, 0)

for _, pattern := range patterns {
Expand Down
10 changes: 10 additions & 0 deletions filter/containerLogsFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package filter

import (
"github.com/abahmed/kwatch/util"
"github.com/sirupsen/logrus"
)

type ContainerLogsFilter struct{}
Expand All @@ -26,6 +27,15 @@ func (f ContainerLogsFilter) Execute(ctx *Context) bool {
previousLogs,
ctx.Config.MaxRecentLogLines)

for _, pattern := range ctx.Config.IgnoreLogPatternsCompiled {
if pattern.MatchString(logs) {
logrus.Infof(
"skipping container %s logs as it matches the ignore log pattern",
container.Name)
return true
}
}

ctx.Container.Logs = logs
return false
}

0 comments on commit 2ab5a04

Please sign in to comment.