Skip to content

feat: Support configuring rule.Reason as a Sprintf format string #1068

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 28 additions & 4 deletions pkg/systemlogmonitor/log_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"time"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -157,6 +159,9 @@ func (l *logMonitor) parseLog(log *systemlogtypes.Log) {
continue
}
status := l.generateStatus(matched, rule)
if status == nil {
continue
}
klog.Infof("New status generated: %+v", status)
l.output <- status
}
Expand All @@ -169,12 +174,31 @@ func (l *logMonitor) generateStatus(logs []*systemlogtypes.Log, rule systemlogty
message := generateMessage(logs, rule.PatternGeneratedMessageSuffix)
var events []types.Event
var changedConditions []*types.Condition

// Support configuring rule.Reason as a Sprintf format string and formatting it with the matched capturing groups in rule.Pattern.
re := regexp.MustCompile(rule.Pattern)
matches := re.FindStringSubmatch(message)
reason := rule.Reason
formatArgs := make([]interface{}, 0)
if len(matches) > 1 {
// Use the matched capturing groups as the arguments for Sprintf.
for _, value := range matches[1:] {
formatArgs = append(formatArgs, value)
}
}
reason = fmt.Sprintf(rule.Reason, formatArgs...)
// If fmt.Sprintf fails, it will add "%!" for each failed template in the result string.
if strings.Contains(reason, "%!") {
klog.Errorf("Got wrong string %q for reason %q with pattern %q", reason, rule.Reason, rule.Pattern)
return nil
}

if rule.Type == types.Temp {
// For temporary error only generate event
events = append(events, types.Event{
Severity: types.Warn,
Timestamp: timestamp,
Reason: rule.Reason,
Reason: reason,
Message: message,
})
} else {
Expand All @@ -185,19 +209,19 @@ func (l *logMonitor) generateStatus(logs []*systemlogtypes.Log, rule systemlogty
// Update transition timestamp and message when the condition
// changes. Condition is considered to be changed only when
// status or reason changes.
if condition.Status == types.False || condition.Reason != rule.Reason {
if condition.Status == types.False || condition.Reason != reason {
condition.Transition = timestamp
condition.Message = message
events = append(events, util.GenerateConditionChangeEvent(
condition.Type,
types.True,
rule.Reason,
reason,
message,
timestamp,
))
}
condition.Status = types.True
condition.Reason = rule.Reason
condition.Reason = reason
changedConditions = append(changedConditions, condition)
break
}
Expand Down
144 changes: 144 additions & 0 deletions pkg/systemlogmonitor/log_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,150 @@ func TestGenerateStatusForMetrics(t *testing.T) {
}
}

func TestGenerateStatusForEvents(t *testing.T) {
for c, test := range []struct {
name string
rule logtypes.Rule
expected *types.Status
logs []*logtypes.Log
}{
{
name: "without matching group",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): \\d+,.*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: &types.Status{
Source: testSource,
Events: []types.Event{{
Severity: types.Warn,
Timestamp: time.Unix(1000, 1000),
Reason: "NvidiaGPUXid",
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
}},
},
},
{
name: "one matching group",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid%s",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): (\\d+),.*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: &types.Status{
Source: testSource,
Events: []types.Event{{
Severity: types.Warn,
Timestamp: time.Unix(1000, 1000),
Reason: "NvidiaGPUXid45",
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
}},
},
},
{
name: "two matching groups",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid%s, Ch%s",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): (\\d+), Ch (\\d+).*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: &types.Status{
Source: testSource,
Events: []types.Event{{
Severity: types.Warn,
Timestamp: time.Unix(1000, 1000),
Reason: "NvidiaGPUXid45, Ch00000010",
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
}},
},
},
{
name: "not enough matching groups 1",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid%s, Ch%s",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): (\\d+),.*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: nil,
},
{
name: "not enough matching groups 2",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid%s",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): \\d+,.*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: nil,
},
{
name: "indexed matching groups",
rule: logtypes.Rule{
Type: types.Temp,
Reason: "NvidiaGPUXid%[1]s, Ch%[2]s",
Pattern: "NVRM: Xid \\(PCI:[^)]+\\): (\\d+), Ch (\\d+).*",
},
logs: []*logtypes.Log{
{
Timestamp: time.Unix(1000, 1000),
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
},
},
expected: &types.Status{
Source: testSource,
Events: []types.Event{{
Severity: types.Warn,
Timestamp: time.Unix(1000, 1000),
Reason: "NvidiaGPUXid45, Ch00000010",
Message: "[...] NVRM: Xid (PCI:0000:00:00.0): 45, Ch 00000010",
}},
},
},
} {
l := &logMonitor{
config: MonitorConfig{
Source: testSource,
},
}
(&l.config).ApplyDefaultConfiguration()
got := l.generateStatus(test.logs, test.rule)

if !reflect.DeepEqual(test.expected, got) {
t.Errorf("case %d %s: expected status %+v, got %+v", c+1, test.name, test.expected, got)
}
}
}

func TestInitializeProblemMetricsOrDie(t *testing.T) {
testCases := []struct {
name string
Expand Down