Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore log patterns #317

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading