From f7e7c0f442d508d9a07507dd3dbfbc42ec5daf54 Mon Sep 17 00:00:00 2001 From: erda-bot <81558540+erda-bot@users.noreply.github.com> Date: Thu, 12 Aug 2021 19:39:56 +0800 Subject: [PATCH] add filters when customize rule update (#1409) (#1419) * add filters when customize_rule is update * change metric_scope_id is org.name Co-authored-by: panjiayao <44628544+Counterflowwind@users.noreply.github.com> --- .../monitor/alert/alert-apis/alert.service.go | 55 +++++++++++-------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/modules/core/monitor/alert/alert-apis/alert.service.go b/modules/core/monitor/alert/alert-apis/alert.service.go index 8830dd6c56e..ba221c59f71 100644 --- a/modules/core/monitor/alert/alert-apis/alert.service.go +++ b/modules/core/monitor/alert/alert-apis/alert.service.go @@ -418,11 +418,9 @@ func (m *alertService) CreateOrgCustomizeAlert(ctx context.Context, request *pb. if err != nil { return nil, errors.NewInternalServerError(err) } - var isOrg bool if request.AlertScope != MicroService { request.AlertScope = "org" request.AlertScopeId = orgID - isOrg = true } request.Attributes = make(map[string]*structpb.Value) var metricNames []string @@ -448,32 +446,12 @@ func (m *alertService) CreateOrgCustomizeAlert(ctx context.Context, request *pb. ruleMetric := metricMap[rule.Metric] labels := ruleMetric.Labels scope := labels["metric_scope"] - scopeId := labels["metric_scope_id"] + scopeId := org.Name if err := m.checkMetricMeta(rule, metricMap[rule.Metric]); err != nil { return nil, errors.NewInternalServerError(err) } - if isOrg { - if scope != "" { - rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ - Tag: "_metric_scope", - Operator: "eq", - Value: structpb.NewStringValue(scope), - }) - } - if scopeId != "" { - rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ - Tag: "_metric_scope_id", - Operator: "eq", - Value: structpb.NewStringValue(scopeId), - }) - } - rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ - Tag: "cluster_name", - Operator: "in", - Value: structpb.NewStringValue("$" + "cluster_name"), - }) - } + m.addFilter(request.AlertScope, scope, scopeId, rule) } data, err := json.Marshal(request) if err != nil { @@ -500,6 +478,30 @@ func (m *alertService) CreateOrgCustomizeAlert(ctx context.Context, request *pb. return result, nil } +func (m *alertService) addFilter(alertScope, scope, scopeId string, rule *pb.CustomizeAlertRule) { + if alertScope == "org" { + if scope != "" { + rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ + Tag: "_metric_scope", + Operator: "eq", + Value: structpb.NewStringValue(scope), + }) + } + if scopeId != "" { + rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ + Tag: "_metric_scope_id", + Operator: "eq", + Value: structpb.NewStringValue(scopeId), + }) + } + rule.Filters = append(rule.Filters, &pb.CustomizeAlertRuleFilter{ + Tag: "cluster_name", + Operator: "in", + Value: structpb.NewStringValue("$" + "cluster_name"), + }) + } +} + func (m *alertService) checkMetricMeta( rule *pb.CustomizeAlertRule, metric *metricpb.MetricMeta) error { if metric == nil { @@ -699,6 +701,11 @@ func (m *alertService) UpdateOrgCustomizeAlert(ctx context.Context, request *pb. } rule.Attributes["metric_name"] = structpb.NewStringValue(metric.Name.Name) } + ruleMetric := metricMap[rule.Metric] + labels := ruleMetric.Labels + scope := labels["metric_scope"] + scopeId := labels["metric_scope_id"] + m.addFilter(request.AlertScope, scope, scopeId, rule) } data, err := json.Marshal(request) if err != nil {