From 15892d6e576f3957be23f0625f6a5675c933cdf5 Mon Sep 17 00:00:00 2001 From: zheng Date: Thu, 20 Oct 2022 20:18:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E5=88=99=E5=90=8D=E7=A7=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8F=98=E9=87=8F=20(#1217)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 规则名称支持变量 * parse rule_name --- src/models/alert_cur_event.go | 19 ++++++++++++++----- src/server/engine/consume.go | 6 +++++- src/server/router/router_event.go | 6 +++++- 3 files changed, 24 insertions(+), 7 deletions(-) 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) }