mirror of
https://github.com/kubernetes/node-problem-detector.git
synced 2026-02-14 18:09:57 +00:00
Merge pull request #997 from googs1025/monitor_log
feature: add custom message for systemlogmonitor rule
This commit is contained in:
@@ -37,7 +37,8 @@ with new rule definition:
|
||||
"type": "temporary/permanent",
|
||||
"condition": "NodeConditionOfPermanentIssue",
|
||||
"reason": "CamelCaseShortReason",
|
||||
"pattern": "regexp matching the issue in the log"
|
||||
"pattern": "regexp matching the issue in the log",
|
||||
"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."
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ type MonitorConfig struct {
|
||||
EnableMetricsReporting *bool `json:"metricsReporting,omitempty"`
|
||||
}
|
||||
|
||||
// ApplyConfiguration applies default configurations.
|
||||
// ApplyDefaultConfiguration applies default configurations.
|
||||
func (mc *MonitorConfig) ApplyDefaultConfiguration() {
|
||||
if mc.BufferSize == 0 {
|
||||
mc.BufferSize = defaultBufferSize
|
||||
|
||||
@@ -18,6 +18,7 @@ package systemlogmonitor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -165,7 +166,7 @@ func (l *logMonitor) parseLog(log *systemlogtypes.Log) {
|
||||
func (l *logMonitor) generateStatus(logs []*systemlogtypes.Log, rule systemlogtypes.Rule) *types.Status {
|
||||
// We use the timestamp of the first log line as the timestamp of the status.
|
||||
timestamp := logs[0].Timestamp
|
||||
message := generateMessage(logs)
|
||||
message := generateMessage(logs, rule.PatternGeneratedMessageSuffix)
|
||||
var events []types.Event
|
||||
var changedConditions []*types.Condition
|
||||
if rule.Type == types.Temp {
|
||||
@@ -250,10 +251,14 @@ func initialConditions(defaults []types.Condition) []types.Condition {
|
||||
return conditions
|
||||
}
|
||||
|
||||
func generateMessage(logs []*systemlogtypes.Log) string {
|
||||
func generateMessage(logs []*systemlogtypes.Log, patternGeneratedMessageSuffix string) string {
|
||||
messages := []string{}
|
||||
for _, log := range logs {
|
||||
messages = append(messages, log.Message)
|
||||
}
|
||||
return concatLogs(messages)
|
||||
logMessage := concatLogs(messages)
|
||||
if patternGeneratedMessageSuffix != "" {
|
||||
return fmt.Sprintf("%s; %s", logMessage, patternGeneratedMessageSuffix)
|
||||
}
|
||||
return logMessage
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
"k8s.io/node-problem-detector/pkg/problemdaemon"
|
||||
"k8s.io/node-problem-detector/pkg/problemmetrics"
|
||||
logtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
|
||||
systemlogtypes "k8s.io/node-problem-detector/pkg/systemlogmonitor/types"
|
||||
"k8s.io/node-problem-detector/pkg/types"
|
||||
"k8s.io/node-problem-detector/pkg/util"
|
||||
"k8s.io/node-problem-detector/pkg/util/metrics"
|
||||
@@ -699,3 +700,40 @@ func TestInitializeProblemMetricsOrDie(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateMessage(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
logs []*systemlogtypes.Log
|
||||
patternGeneratedMessageSuffix string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "No rule message",
|
||||
logs: []*systemlogtypes.Log{
|
||||
{Message: "First log message"},
|
||||
{Message: "Second log message"},
|
||||
},
|
||||
patternGeneratedMessageSuffix: "",
|
||||
want: "First log message\nSecond log message",
|
||||
},
|
||||
{
|
||||
name: "With rule message",
|
||||
logs: []*systemlogtypes.Log{
|
||||
{Message: "First log message"},
|
||||
{Message: "Second log message"},
|
||||
},
|
||||
patternGeneratedMessageSuffix: "refer www.foo.com/docs for playbook on how to fix the issue",
|
||||
want: "First log message\nSecond log message; refer www.foo.com/docs for playbook on how to fix the issue",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := generateMessage(tt.logs, tt.patternGeneratedMessageSuffix)
|
||||
if got != tt.want {
|
||||
t.Errorf("generateMessage() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,4 +42,9 @@ type Rule struct {
|
||||
// Pattern is the regular expression to match the problem in log.
|
||||
// Notice that the pattern must match to the end of the line.
|
||||
Pattern string `json:"pattern"`
|
||||
// PatternGeneratedMessageSuffix is an optional suffix appended to the matched pattern.
|
||||
// This suffix provides additional context or instructions for resolving the issue.
|
||||
// It can be used to include environment-specific details, links to documentation,
|
||||
// or any other information that helps users understand and address the problem.
|
||||
PatternGeneratedMessageSuffix string `json:"patternGeneratedMessageSuffix,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user