Skip to content

Commit 334a857

Browse files
authored
Merge pull request #997 from googs1025/monitor_log
feature: add custom message for systemlogmonitor rule
2 parents 93e64ac + f5433f4 commit 334a857

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

pkg/systemlogmonitor/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ with new rule definition:
3737
"type": "temporary/permanent",
3838
"condition": "NodeConditionOfPermanentIssue",
3939
"reason": "CamelCaseShortReason",
40-
"pattern": "regexp matching the issue in the log"
40+
"pattern": "regexp matching the issue in the log",
41+
"patternGeneratedMessageSuffix": "Please check the network connectivity and ensure that all required services are running. For more details, see our documentation at https://example.com/docs/troubleshooting."
4142
}
4243
```
4344

pkg/systemlogmonitor/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type MonitorConfig struct {
4646
EnableMetricsReporting *bool `json:"metricsReporting,omitempty"`
4747
}
4848

49-
// ApplyConfiguration applies default configurations.
49+
// ApplyDefaultConfiguration applies default configurations.
5050
func (mc *MonitorConfig) ApplyDefaultConfiguration() {
5151
if mc.BufferSize == 0 {
5252
mc.BufferSize = defaultBufferSize

pkg/systemlogmonitor/log_monitor.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package systemlogmonitor
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"os"
2223
"time"
2324

@@ -165,7 +166,7 @@ func (l *logMonitor) parseLog(log *systemlogtypes.Log) {
165166
func (l *logMonitor) generateStatus(logs []*systemlogtypes.Log, rule systemlogtypes.Rule) *types.Status {
166167
// We use the timestamp of the first log line as the timestamp of the status.
167168
timestamp := logs[0].Timestamp
168-
message := generateMessage(logs)
169+
message := generateMessage(logs, rule.PatternGeneratedMessageSuffix)
169170
var events []types.Event
170171
var changedConditions []*types.Condition
171172
if rule.Type == types.Temp {
@@ -250,10 +251,14 @@ func initialConditions(defaults []types.Condition) []types.Condition {
250251
return conditions
251252
}
252253

253-
func generateMessage(logs []*systemlogtypes.Log) string {
254+
func generateMessage(logs []*systemlogtypes.Log, patternGeneratedMessageSuffix string) string {
254255
messages := []string{}
255256
for _, log := range logs {
256257
messages = append(messages, log.Message)
257258
}
258-
return concatLogs(messages)
259+
logMessage := concatLogs(messages)
260+
if patternGeneratedMessageSuffix != "" {
261+
return fmt.Sprintf("%s; %s", logMessage, patternGeneratedMessageSuffix)
262+
}
263+
return logMessage
259264
}

pkg/systemlogmonitor/log_monitor_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"k8s.io/node-problem-detector/pkg/problemdaemon"
2727
"k8s.io/node-problem-detector/pkg/problemmetrics"
2828
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
29+
systemlogtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
2930
"k8s.io/node-problem-detector/pkg/types"
3031
"k8s.io/node-problem-detector/pkg/util"
3132
"k8s.io/node-problem-detector/pkg/util/metrics"
@@ -699,3 +700,40 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
699700
})
700701
}
701702
}
703+
704+
func TestGenerateMessage(t *testing.T) {
705+
tests := []struct {
706+
name string
707+
logs []*systemlogtypes.Log
708+
patternGeneratedMessageSuffix string
709+
want string
710+
}{
711+
{
712+
name: "No rule message",
713+
logs: []*systemlogtypes.Log{
714+
{Message: "First log message"},
715+
{Message: "Second log message"},
716+
},
717+
patternGeneratedMessageSuffix: "",
718+
want: "First log message\nSecond log message",
719+
},
720+
{
721+
name: "With rule message",
722+
logs: []*systemlogtypes.Log{
723+
{Message: "First log message"},
724+
{Message: "Second log message"},
725+
},
726+
patternGeneratedMessageSuffix: "refer www.foo.com/docs for playbook on how to fix the issue",
727+
want: "First log message\nSecond log message; refer www.foo.com/docs for playbook on how to fix the issue",
728+
},
729+
}
730+
731+
for _, tt := range tests {
732+
t.Run(tt.name, func(t *testing.T) {
733+
got := generateMessage(tt.logs, tt.patternGeneratedMessageSuffix)
734+
if got != tt.want {
735+
t.Errorf("generateMessage() = %v, want %v", got, tt.want)
736+
}
737+
})
738+
}
739+
}

pkg/systemlogmonitor/types/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ type Rule struct {
4242
// Pattern is the regular expression to match the problem in log.
4343
// Notice that the pattern must match to the end of the line.
4444
Pattern string `json:"pattern"`
45+
// PatternGeneratedMessageSuffix is an optional suffix appended to the matched pattern.
46+
// This suffix provides additional context or instructions for resolving the issue.
47+
// It can be used to include environment-specific details, links to documentation,
48+
// or any other information that helps users understand and address the problem.
49+
PatternGeneratedMessageSuffix string `json:"patternGeneratedMessageSuffix,omitempty"`
4550
}

0 commit comments

Comments
 (0)