Skip to content

Commit

Permalink
Merge pull request #848 from linxiulei/revert
Browse files Browse the repository at this point in the history
Support revert-pattern in logcounter
  • Loading branch information
k8s-ci-robot committed Jan 17, 2024
2 parents 18630b6 + c225435 commit f262b50
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cmd/logcounter/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type LogCounterOptions struct {
Lookback string
Delay string
Pattern string
RevertPattern string
Count int
}

Expand All @@ -46,6 +47,8 @@ func (fedo *LogCounterOptions) AddFlags(fs *pflag.FlagSet) {
"The time duration log watcher delays after node boot time. This is useful when log watcher needs to wait for some time until the node is stable.")
fs.StringVar(&fedo.Pattern, "pattern", "",
"The regular expression to match the problem in log. The pattern must match to the end of the line.")
fs.StringVar(&fedo.RevertPattern, "revert-pattern", "",
"Similar to --pattern but conversely it decreases count value for every match. This is useful to discount a log when another log occurs.")
fs.IntVar(&fedo.Count, "count", 1,
"The number of times the pattern must be found to trigger the condition")
}
Expand Down
9 changes: 6 additions & 3 deletions config/systemd-monitor-counter.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"--lookback=20m",
"--delay=5m",
"--count=5",
"--pattern=Started (Kubernetes kubelet|kubelet.service|kubelet.service - Kubernetes kubelet)."
"--pattern=Started (Kubernetes kubelet|kubelet.service|kubelet.service - Kubernetes kubelet).",
"--revert-pattern=Stopping (Kubernetes kubelet|kubelet.service|kubelet.service - Kubernetes kubelet)..."
],
"timeout": "1m"
},
Expand All @@ -51,7 +52,8 @@
"--log-path=/var/log/journal",
"--lookback=20m",
"--count=5",
"--pattern=Starting (Docker Application Container Engine|docker.service|docker.service - Docker Application Container Engine)..."
"--pattern=Starting (Docker Application Container Engine|docker.service|docker.service - Docker Application Container Engine)...",
"--revert-pattern=Stopping (Docker Application Container Engine|docker.service|docker.service - Docker Application Container Engine)..."
],
"timeout": "1m"
},
Expand All @@ -65,7 +67,8 @@
"--log-path=/var/log/journal",
"--lookback=20m",
"--count=5",
"--pattern=Starting (containerd container runtime|containerd.service|containerd.service - containerd container runtime)..."
"--pattern=Starting (containerd container runtime|containerd.service|containerd.service - containerd container runtime)...",
"--revert-pattern=Stopping (containerd container runtime|containerd.service|containerd.service - containerd container runtime)..."
],
"timeout": "1m"
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/logcounter/log_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ const (
)

type logCounter struct {
logCh <-chan *systemtypes.Log
buffer systemlogmonitor.LogBuffer
pattern string
clock clock.Clock
logCh <-chan *systemtypes.Log
buffer systemlogmonitor.LogBuffer
pattern string
revertPattern string
clock clock.Clock
}

func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter, error) {
Expand All @@ -59,10 +60,11 @@ func NewJournaldLogCounter(options *options.LogCounterOptions) (types.LogCounter
return nil, fmt.Errorf("error watching journald: %v", err)
}
return &logCounter{
logCh: logCh,
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
pattern: options.Pattern,
clock: clock.RealClock{},
logCh: logCh,
buffer: systemlogmonitor.NewLogBuffer(bufferSize),
pattern: options.Pattern,
revertPattern: options.RevertPattern,
clock: clock.RealClock{},
}, nil
}

Expand All @@ -84,6 +86,9 @@ func (e *logCounter) Count() (count int, err error) {
if len(e.buffer.Match(e.pattern)) != 0 {
count++
}
if e.revertPattern != "" && len(e.buffer.Match(e.revertPattern)) != 0 {
count--
}
case <-e.clock.After(timeout):
// Don't block forever if we do not get any new messages
return
Expand Down

0 comments on commit f262b50

Please sign in to comment.