Skip to content

Commit

Permalink
add filters when customize rule update (#1409) (#1419)
Browse files Browse the repository at this point in the history
* add filters when customize_rule is update

* change metric_scope_id is org.name

Co-authored-by: panjiayao <[email protected]>
  • Loading branch information
erda-bot and Counterflowwind authored Aug 12, 2021
1 parent 2ffddbc commit f7e7c0f
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions modules/core/monitor/alert/alert-apis/alert.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f7e7c0f

Please sign in to comment.