From 31174d719e56692dc5c442bb1b3bfc6c9ec948dd Mon Sep 17 00:00:00 2001 From: ning <710leo@gmail.com> Date: Mon, 22 Jul 2024 11:45:17 +0800 Subject: [PATCH] refactor: event relabel --- alert/dispatch/consume.go | 50 -------------------------------------- alert/process/process.go | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/alert/dispatch/consume.go b/alert/dispatch/consume.go index 9a5b94763..0e5fcf180 100644 --- a/alert/dispatch/consume.go +++ b/alert/dispatch/consume.go @@ -14,9 +14,7 @@ import ( "github.com/ccfos/nightingale/v6/pkg/poster" promsdk "github.com/ccfos/nightingale/v6/pkg/prom" "github.com/ccfos/nightingale/v6/prom" - "github.com/ccfos/nightingale/v6/pushgw/writer" - "github.com/prometheus/prometheus/prompb" "github.com/toolkits/pkg/concurrent/semaphore" "github.com/toolkits/pkg/logger" ) @@ -95,8 +93,6 @@ func (e *Consumer) consumeOne(event *models.AlertCurEvent) { event.RuleNote = fmt.Sprintf("failed to parse rule note: %v", err) } - e.relabel(event) - e.persist(event) if event.IsRecovered && event.NotifyRecovered == 0 { @@ -190,52 +186,6 @@ func (e *Consumer) queryRecoveryVal(event *models.AlertCurEvent) { } } -func (e *Consumer) relabel(event *models.AlertCurEvent) { - rule := e.dispatch.alertRuleCache.Get(event.RuleId) - if rule == nil { - return - } - - // need to keep the original label - event.OriginalTags = event.Tags - event.OriginalTagsJSON = make([]string, len(event.TagsJSON)) - - labels := make([]prompb.Label, len(event.TagsJSON)) - for i, tag := range event.TagsJSON { - label := strings.Split(tag, "=") - if len(label) != 2 { - logger.Errorf("event%+v relabel: the label length is not 2:%v", event, label) - continue - } - event.OriginalTagsJSON[i] = tag - labels[i] = prompb.Label{Name: label[0], Value: label[1]} - } - - for i := 0; i < len(rule.EventRelabelConfig); i++ { - if rule.EventRelabelConfig[i].Replacement == "" { - rule.EventRelabelConfig[i].Replacement = "$1" - } - - if rule.EventRelabelConfig[i].Separator == "" { - rule.EventRelabelConfig[i].Separator = ";" - } - - if rule.EventRelabelConfig[i].Regex == "" { - rule.EventRelabelConfig[i].Regex = "(.*)" - } - } - - // relabel process - relabels := writer.Process(labels, rule.EventRelabelConfig...) - event.TagsJSON = make([]string, len(relabels)) - event.TagsMap = make(map[string]string, len(relabels)) - for i, label := range relabels { - event.TagsJSON[i] = fmt.Sprintf("%s=%s", label.Name, label.Value) - event.TagsMap[label.Name] = label.Value - } - event.Tags = strings.Join(event.TagsJSON, ",,") -} - func getKey(event *models.AlertCurEvent) string { return common.RuleKey(event.DatasourceId, event.RuleId) } diff --git a/alert/process/process.go b/alert/process/process.go index afb70826f..b88150d73 100644 --- a/alert/process/process.go +++ b/alert/process/process.go @@ -18,6 +18,9 @@ import ( "github.com/ccfos/nightingale/v6/models" "github.com/ccfos/nightingale/v6/pkg/ctx" "github.com/ccfos/nightingale/v6/pkg/tplx" + "github.com/ccfos/nightingale/v6/pushgw/writer" + + "github.com/prometheus/prometheus/prompb" "github.com/toolkits/pkg/logger" "github.com/toolkits/pkg/str" ) @@ -218,9 +221,57 @@ func (p *Processor) BuildEvent(anomalyPoint common.AnomalyPoint, from string, no } else { event.LastEvalTime = event.TriggerTime } + + // 生成事件之后,立马进程 relabel 处理 + Relabel(p.rule, event) return event } +func Relabel(rule *models.AlertRule, event *models.AlertCurEvent) { + if rule == nil { + return + } + + // need to keep the original label + event.OriginalTags = event.Tags + event.OriginalTagsJSON = make([]string, len(event.TagsJSON)) + + labels := make([]prompb.Label, len(event.TagsJSON)) + for i, tag := range event.TagsJSON { + label := strings.Split(tag, "=") + if len(label) != 2 { + logger.Errorf("event%+v relabel: the label length is not 2:%v", event, label) + continue + } + event.OriginalTagsJSON[i] = tag + labels[i] = prompb.Label{Name: label[0], Value: label[1]} + } + + for i := 0; i < len(rule.EventRelabelConfig); i++ { + if rule.EventRelabelConfig[i].Replacement == "" { + rule.EventRelabelConfig[i].Replacement = "$1" + } + + if rule.EventRelabelConfig[i].Separator == "" { + rule.EventRelabelConfig[i].Separator = ";" + } + + if rule.EventRelabelConfig[i].Regex == "" { + rule.EventRelabelConfig[i].Regex = "(.*)" + } + } + + // relabel process + relabels := writer.Process(labels, rule.EventRelabelConfig...) + event.TagsJSON = make([]string, len(relabels)) + event.TagsMap = make(map[string]string, len(relabels)) + for i, label := range relabels { + event.TagsJSON[i] = fmt.Sprintf("%s=%s", label.Name, label.Value) + event.TagsMap[label.Name] = label.Value + } + event.Tags = strings.Join(event.TagsJSON, ",,") +} + func (p *Processor) HandleRecover(alertingKeys map[string]struct{}, now int64, inhibit bool) { for _, hash := range p.pendings.Keys() { if _, has := alertingKeys[hash]; has {