Skip to content

Commit 50016aa

Browse files
committed
fix: Allow capturing groups in rule.Pattern with non-templated rule.Reason
1 parent 0fd1dff commit 50016aa

File tree

2 files changed

+39
-14
lines changed

2 files changed

+39
-14
lines changed

pkg/systemlogmonitor/log_monitor.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,22 +176,24 @@ func (l *logMonitor) generateStatus(logs []*systemlogtypes.Log, rule systemlogty
176176
var events []types.Event
177177
var changedConditions []*types.Condition
178178

179-
// Support configuring rule.Reason as a Sprintf format string and formatting it with the matched capturing groups in rule.Pattern.
180-
re := regexp.MustCompile(rule.Pattern)
181-
matches := re.FindStringSubmatch(message)
182179
reason := rule.Reason
183-
formatArgs := make([]interface{}, 0)
184-
if len(matches) > 1 {
185-
// Use the matched capturing groups as the arguments for Sprintf.
186-
for _, value := range matches[1:] {
187-
formatArgs = append(formatArgs, value)
180+
// Support configuring rule.Reason as a Sprintf format string and formatting it with the matched capturing groups in rule.Pattern.
181+
if strings.Contains(reason, "%") {
182+
re := regexp.MustCompile(rule.Pattern)
183+
matches := re.FindStringSubmatch(message)
184+
formatArgs := make([]interface{}, 0)
185+
if len(matches) > 1 {
186+
// Use the matched capturing groups as the arguments for Sprintf.
187+
for _, value := range matches[1:] {
188+
formatArgs = append(formatArgs, value)
189+
}
190+
}
191+
reason = fmt.Sprintf(rule.Reason, formatArgs...)
192+
// If fmt.Sprintf fails, it will add "%!" for each failed template in the result string.
193+
if strings.Contains(reason, "%!") {
194+
klog.Errorf("Got wrong string %q for reason %q with pattern %q", reason, rule.Reason, rule.Pattern)
195+
return nil
188196
}
189-
}
190-
reason = fmt.Sprintf(rule.Reason, formatArgs...)
191-
// If fmt.Sprintf fails, it will add "%!" for each failed template in the result string.
192-
if strings.Contains(reason, "%!") {
193-
klog.Errorf("Got wrong string %q for reason %q with pattern %q", reason, rule.Reason, rule.Pattern)
194-
return nil
195197
}
196198

197199
if rule.Type == types.Temp {

pkg/systemlogmonitor/log_monitor_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,29 @@ func TestGenerateStatusForEvents(t *testing.T) {
537537
}},
538538
},
539539
},
540+
{
541+
name: "using matching groups in Pattern but not in Reason",
542+
rule: logtypes.Rule{
543+
Type: types.Temp,
544+
Reason: "OOMKilling",
545+
Pattern: "Killed process \\d+ (.+) total-vm:\\d+kB, anon-rss:\\d+kB, file-rss:\\d+kB.*",
546+
},
547+
logs: []*logtypes.Log{
548+
{
549+
Timestamp: time.Unix(1000, 1000),
550+
Message: "kernel: Killed process 13357 (mysqld), UID 27, total-vm:4977676kB, anon-rss:3256736kB, file-rss:0kB, shmem-rss:0kB",
551+
},
552+
},
553+
expected: &types.Status{
554+
Source: testSource,
555+
Events: []types.Event{{
556+
Severity: types.Warn,
557+
Timestamp: time.Unix(1000, 1000),
558+
Reason: "OOMKilling",
559+
Message: "kernel: Killed process 13357 (mysqld), UID 27, total-vm:4977676kB, anon-rss:3256736kB, file-rss:0kB, shmem-rss:0kB",
560+
}},
561+
},
562+
},
540563
} {
541564
l := &logMonitor{
542565
config: MonitorConfig{

0 commit comments

Comments
 (0)