规则名称支持变量 (#1217)

* 规则名称支持变量

* parse rule_name
This commit is contained in:
zheng 2022-10-20 20:18:15 +08:00 committed by GitHub
parent fbff60eefb
commit 15892d6e57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

View File

@ -63,10 +63,11 @@ type AggrRule struct {
Value string Value string
} }
func (e *AlertCurEvent) ParseRuleNote() error { func (e *AlertCurEvent) ParseRule(field string) error {
e.RuleNote = strings.TrimSpace(e.RuleNote) f := e.GetField(field)
f = strings.TrimSpace(f)
if e.RuleNote == "" { if f == "" {
return nil return nil
} }
@ -75,7 +76,7 @@ func (e *AlertCurEvent) ParseRuleNote() error {
"{{$value := .TriggerValue}}", "{{$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) t, err := template.New(fmt.Sprint(e.RuleId)).Funcs(tplx.TemplateFuncMap).Parse(text)
if err != nil { if err != nil {
return err return err
@ -87,7 +88,13 @@ func (e *AlertCurEvent) ParseRuleNote() error {
return err return err
} }
e.RuleNote = body.String() if field == "rule_name" {
e.RuleName = body.String()
}
if field == "rule_note" {
e.RuleNote = body.String()
}
return nil return nil
} }
@ -133,6 +140,8 @@ func (e *AlertCurEvent) GetField(field string) string {
return fmt.Sprint(e.RuleId) return fmt.Sprint(e.RuleId)
case "rule_name": case "rule_name":
return e.RuleName return e.RuleName
case "rule_note":
return e.RuleNote
case "severity": case "severity":
return fmt.Sprint(e.Severity) return fmt.Sprint(e.Severity)
case "runbook_url": case "runbook_url":

View File

@ -45,7 +45,11 @@ func consume(events []interface{}, sema *semaphore.Semaphore) {
func consumeOne(event *models.AlertCurEvent) { func consumeOne(event *models.AlertCurEvent) {
LogEvent(event, "consume") 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) event.RuleNote = fmt.Sprintf("failed to parse rule note: %v", err)
} }

View File

@ -46,7 +46,11 @@ func pushEventToQueue(c *gin.Context) {
return 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) event.RuleNote = fmt.Sprintf("failed to parse rule note: %v", err)
} }