diff --git a/src/models/alert_cur_event.go b/src/models/alert_cur_event.go index bff29ae8..1e161ef3 100644 --- a/src/models/alert_cur_event.go +++ b/src/models/alert_cur_event.go @@ -63,10 +63,11 @@ type AggrRule struct { Value string } -func (e *AlertCurEvent) ParseRuleNote() error { - e.RuleNote = strings.TrimSpace(e.RuleNote) +func (e *AlertCurEvent) ParseRule(field string) error { + f := e.GetField(field) + f = strings.TrimSpace(f) - if e.RuleNote == "" { + if f == "" { return nil } @@ -75,7 +76,7 @@ func (e *AlertCurEvent) ParseRuleNote() error { "{{$value := .TriggerValue}}", } - text := strings.Join(append(defs, e.RuleNote), "") + text := strings.Join(append(defs, f), "") t, err := template.New(fmt.Sprint(e.RuleId)).Funcs(tplx.TemplateFuncMap).Parse(text) if err != nil { return err @@ -87,7 +88,13 @@ func (e *AlertCurEvent) ParseRuleNote() error { return err } - e.RuleNote = body.String() + if field == "rule_name" { + e.RuleName = body.String() + } + + if field == "rule_note" { + e.RuleNote = body.String() + } return nil } @@ -133,6 +140,8 @@ func (e *AlertCurEvent) GetField(field string) string { return fmt.Sprint(e.RuleId) case "rule_name": return e.RuleName + case "rule_note": + return e.RuleNote case "severity": return fmt.Sprint(e.Severity) case "runbook_url": diff --git a/src/server/engine/consume.go b/src/server/engine/consume.go index 273c8d84..91f6c885 100644 --- a/src/server/engine/consume.go +++ b/src/server/engine/consume.go @@ -45,7 +45,11 @@ func consume(events []interface{}, sema *semaphore.Semaphore) { func consumeOne(event *models.AlertCurEvent) { LogEvent(event, "consume") - if err := event.ParseRuleNote(); err != nil { + if err := event.ParseRule("rule_name"); err != nil { + event.RuleName = fmt.Sprintf("failed to parse rule name: %v", err) + } + + if err := event.ParseRule("rule_note"); err != nil { event.RuleNote = fmt.Sprintf("failed to parse rule note: %v", err) } diff --git a/src/server/router/router_event.go b/src/server/router/router_event.go index b5aaa5bf..03e60518 100644 --- a/src/server/router/router_event.go +++ b/src/server/router/router_event.go @@ -46,7 +46,11 @@ func pushEventToQueue(c *gin.Context) { return } - if err := event.ParseRuleNote(); err != nil { + if err := event.ParseRule("rule_name"); err != nil { + event.RuleName = fmt.Sprintf("failed to parse rule name: %v", err) + } + + if err := event.ParseRule("rule_note"); err != nil { event.RuleNote = fmt.Sprintf("failed to parse rule note: %v", err) }