From bfc26b8baf7ec296f88119c483d248477014f54f Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Thu, 12 Aug 2021 17:01:01 +0800 Subject: [PATCH 1/8] =?UTF-8?q?change=20kubernetes=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=AE=9E=E4=BE=8BReady=E7=8A=B6=E6=80=81=E5=BC=82=E5=B8=B8=20r?= =?UTF-8?q?ecover=20is=20true?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .erda/migrations/monitor/20210812-monitor-rules-recover.sql | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .erda/migrations/monitor/20210812-monitor-rules-recover.sql diff --git a/.erda/migrations/monitor/20210812-monitor-rules-recover.sql b/.erda/migrations/monitor/20210812-monitor-rules-recover.sql new file mode 100644 index 00000000000..ea6b91a2b7e --- /dev/null +++ b/.erda/migrations/monitor/20210812-monitor-rules-recover.sql @@ -0,0 +1,5 @@ +SET NAMES utf8mb4; +BEGIN; +UPDATE `sp_alert_rules` SET `enable` = 0 WHERE `alert_index`= 'kubernetes_instance_ready'; +INSERT `sp_alert_rules`(`alert_index`,`alert_scope`,`alert_type`,`attributes`,`name`,`template`) VALUES("kubernetes_instance_ready","org","kubernetes","{\"alert_group\":\"{{cluster_name}}-{{daemonset_name}}-{{statefulset_name}}-{{deployment_name}}\",\"level\":\"FATAL\",\"recover\":\"true\",\"tickets_metric_key\":\"{{daemonset_name}}-{{statefulset_name}}-{{deployment_name}}\"}","kubernetes组件实例Ready状态异常","{\"filters\":[{\"operator\":\"in\",\"tag\":\"cluster_name\",\"value\":[\"$cluster_name\"]},{\"operator\":\"eq\",\"tag\":\"namespace\",\"value\":\"kube-system\"}],\"functions\":[{\"aggregator\":\"max\",\"alias\":\"not_ready_value\",\"field\":\"not_ready\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_unavailable}else{if(tags.statefulset_name){return fields.replicas-fields.replicas_ready}else{if(tags.deployment_name){return fields.replicas_unavailable}}}return 0}\",\"operator\":\"gt\",\"value\":0},{\"aggregator\":\"max\",\"alias\":\"ready_value\",\"field\":\"ready_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_available}else{if(tags.statefulset_name){return fields.replicas_ready}else{if(tags.deployment_name){return fields.replicas_available}}}return 0}\"},{\"aggregator\":\"max\",\"alias\":\"total_value\",\"field\":\"total_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_unavailable+fields.number_available}else{if(tags.statefulset_name){return fields.replicas}else{if(tags.deployment_name){return fields.replicas}}}return 0}\"},{\"aggregator\":\"value\",\"alias\":\"resource_name_value\",\"field\":\"resource_name_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return tags.daemonset_name}else{if(tags.statefulset_name){return tags.statefulset_name}else{if(tags.deployment_name){return tags.deployment_name}}}return null};\"}],\"group\":[\"cluster_name\",\"namespace\",\"daemonset_name\",\"statefulset_name\",\"deployment_name\"],\"metrics\":[\"kubernetes_daemonset\",\"kubernetes_statefulset\",\"kubernetes_deployment\"],\"outputs\":[\"alert\"],\"select\":{\"_meta\":\"#_meta\",\"_metric_scope\":\"#_metric_scope\",\"_metric_scope_id\":\"#_metric_scope_id\",\"cluster_name\":\"#cluster_name\",\"component_name\":\"#resource_name_value\",\"daemonset_name\":\"#daemonset_name\",\"deployment_name\":\"#deployment_name\",\"namespace\":\"#namespace\",\"org_name\":\"#org_name\",\"statefulset_name\":\"#statefulset_name\"},\"window\":1}"); +COMMIT; From 3908069b545b306af2be8f3548d6b0dab10af681 Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Fri, 13 Aug 2021 11:04:44 +0800 Subject: [PATCH 2/8] modify cluster_name and application_id value in expression filters --- .../monitor/alert/alert-apis/adapt/alert_convert.go | 13 +++++++++++++ .../monitor/alert/alert-apis/adapt/expression.go | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go index a7859c18d6b..97eb887e75f 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go @@ -14,6 +14,7 @@ package adapt import ( + "fmt" "reflect" "strconv" "strings" @@ -465,6 +466,18 @@ func ToDBAlertExpressionModel(e *pb.AlertExpression, orgName string, alert *pb.A if !ok { continue } + //if tag == "cluster_name" && value != "$cluster_name" { + // continue + //} + if tag == ClusterName || tag == ApplicationId { + v, ok := value.(string) + if !ok { + return nil, fmt.Errorf("assert cluster_name or application_id is failed") + } + if !strings.HasPrefix(v, "$") { + continue + } + } if attr, ok := attributes[tag]; ok { val, err := formatOperatorValue(opType, utils.StringType, attr) if err != nil { diff --git a/modules/core/monitor/alert/alert-apis/adapt/expression.go b/modules/core/monitor/alert/alert-apis/adapt/expression.go index c9ff12f1739..79e3816a2c4 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/expression.go +++ b/modules/core/monitor/alert/alert-apis/adapt/expression.go @@ -70,6 +70,11 @@ const ( Hours = "hours" ) +const ( + ClusterName = "cluster_name" + ApplicationId = "application_id" +) + type ( // DisplayKey . DisplayKey struct { From 625a77d75b9578ddf6b77c2faa816157f3d3e186 Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Fri, 13 Aug 2021 11:05:14 +0800 Subject: [PATCH 3/8] modify sql delete commit --- .erda/migrations/monitor/20210812-monitor-rules-recover.sql | 3 --- 1 file changed, 3 deletions(-) diff --git a/.erda/migrations/monitor/20210812-monitor-rules-recover.sql b/.erda/migrations/monitor/20210812-monitor-rules-recover.sql index ea6b91a2b7e..7ab74b36f89 100644 --- a/.erda/migrations/monitor/20210812-monitor-rules-recover.sql +++ b/.erda/migrations/monitor/20210812-monitor-rules-recover.sql @@ -1,5 +1,2 @@ -SET NAMES utf8mb4; -BEGIN; UPDATE `sp_alert_rules` SET `enable` = 0 WHERE `alert_index`= 'kubernetes_instance_ready'; INSERT `sp_alert_rules`(`alert_index`,`alert_scope`,`alert_type`,`attributes`,`name`,`template`) VALUES("kubernetes_instance_ready","org","kubernetes","{\"alert_group\":\"{{cluster_name}}-{{daemonset_name}}-{{statefulset_name}}-{{deployment_name}}\",\"level\":\"FATAL\",\"recover\":\"true\",\"tickets_metric_key\":\"{{daemonset_name}}-{{statefulset_name}}-{{deployment_name}}\"}","kubernetes组件实例Ready状态异常","{\"filters\":[{\"operator\":\"in\",\"tag\":\"cluster_name\",\"value\":[\"$cluster_name\"]},{\"operator\":\"eq\",\"tag\":\"namespace\",\"value\":\"kube-system\"}],\"functions\":[{\"aggregator\":\"max\",\"alias\":\"not_ready_value\",\"field\":\"not_ready\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_unavailable}else{if(tags.statefulset_name){return fields.replicas-fields.replicas_ready}else{if(tags.deployment_name){return fields.replicas_unavailable}}}return 0}\",\"operator\":\"gt\",\"value\":0},{\"aggregator\":\"max\",\"alias\":\"ready_value\",\"field\":\"ready_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_available}else{if(tags.statefulset_name){return fields.replicas_ready}else{if(tags.deployment_name){return fields.replicas_available}}}return 0}\"},{\"aggregator\":\"max\",\"alias\":\"total_value\",\"field\":\"total_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return fields.number_unavailable+fields.number_available}else{if(tags.statefulset_name){return fields.replicas}else{if(tags.deployment_name){return fields.replicas}}}return 0}\"},{\"aggregator\":\"value\",\"alias\":\"resource_name_value\",\"field\":\"resource_name_value\",\"field_script\":\"function invoke(fields,tags){if(tags.daemonset_name){return tags.daemonset_name}else{if(tags.statefulset_name){return tags.statefulset_name}else{if(tags.deployment_name){return tags.deployment_name}}}return null};\"}],\"group\":[\"cluster_name\",\"namespace\",\"daemonset_name\",\"statefulset_name\",\"deployment_name\"],\"metrics\":[\"kubernetes_daemonset\",\"kubernetes_statefulset\",\"kubernetes_deployment\"],\"outputs\":[\"alert\"],\"select\":{\"_meta\":\"#_meta\",\"_metric_scope\":\"#_metric_scope\",\"_metric_scope_id\":\"#_metric_scope_id\",\"cluster_name\":\"#cluster_name\",\"component_name\":\"#resource_name_value\",\"daemonset_name\":\"#daemonset_name\",\"deployment_name\":\"#deployment_name\",\"namespace\":\"#namespace\",\"org_name\":\"#org_name\",\"statefulset_name\":\"#statefulset_name\"},\"window\":1}"); -COMMIT; From e844ab55997cf709a1f41d6e8c514f7c128649ca Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Fri, 13 Aug 2021 11:16:50 +0800 Subject: [PATCH 4/8] delete annotation --- modules/core/monitor/alert/alert-apis/adapt/alert_convert.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go index 97eb887e75f..fa0a924d521 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert.go @@ -466,9 +466,6 @@ func ToDBAlertExpressionModel(e *pb.AlertExpression, orgName string, alert *pb.A if !ok { continue } - //if tag == "cluster_name" && value != "$cluster_name" { - // continue - //} if tag == ClusterName || tag == ApplicationId { v, ok := value.(string) if !ok { From c4051432736a95b81fca8fa4528d8e2b53749bbe Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Mon, 16 Aug 2021 11:29:29 +0800 Subject: [PATCH 5/8] add ToDBAlertExpressionModel test function --- .../alert-apis/adapt/alert_convert_test.go | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go index b67b08bf72e..4644240afd6 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go @@ -17,7 +17,10 @@ import ( "testing" "time" + "google.golang.org/protobuf/types/known/structpb" + "github.com/erda-project/erda-infra/providers/i18n" + "github.com/erda-project/erda-proto-go/core/monitor/alert/pb" "github.com/erda-project/erda/apistructs" "github.com/erda-project/erda/modules/core/monitor/alert/alert-apis/db" "github.com/erda-project/erda/pkg/encoding/jsonmap" @@ -194,3 +197,195 @@ func TestAlertNotify_ToModel(t *testing.T) { }) } } + +func TestToDBAlertExpressionModel(t *testing.T) { + type args struct { + e *pb.AlertExpression + orgName string + alert *pb.Alert + rule *pb.AlertRule + } + clusterName, _ := structpb.NewList([]interface{}{ + "terminus-test", + "fdp-test", + }) + templateCluster, _ := structpb.NewList([]interface{}{ + "cluster_name", + }) + templateOutput, _ := structpb.NewList([]interface{}{ + "alert", + }) + ruleAttribute, _ := structpb.NewList([]interface{}{ + "machine_mem", + "machine", + }) + templateFilters, _ := structpb.NewList([]interface{}{ + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("cluster_name"), + "operator": structpb.NewStringValue("neq"), + "value": structpb.NewStringValue("xxxxxxx"), + "dataType": structpb.NewStringValue(""), + }, + }) + templateFunctions, _ := structpb.NewList([]interface{}{ + map[string]*structpb.Value{ + "unit": structpb.NewStringValue(""), + "field": structpb.NewStringValue("mem_used"), + "alias": structpb.NewStringValue("sdf"), + "aggregator": structpb.NewStringValue("sum"), + "operator": structpb.NewStringValue("neq"), + "value": structpb.NewNumberValue(float64(1)), + "dataType": structpb.NewStringValue(""), + }, + }) + templateSelect, _ := structpb.NewList([]interface{}{ + map[string]*structpb.Value{ + "os": structpb.NewStringValue("#os"), + "hostname": structpb.NewStringValue("#hostname"), + }, + }) + + tests := []struct { + name string + args args + want *db.AlertExpression + wantErr bool + }{ + { + name: "TestToDBAlertExpressionModel", + args: args{ + e: &pb.AlertExpression{ + Id: 44, + RuleId: 0, + AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Window: 1, + Functions: []*pb.AlertExpressionFunction{ + { + Field: "mem_used", + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(1), + }, + }, + IsRecover: false, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + orgName: "terminus", + alert: &pb.Alert{ + Id: 56, + Name: "pjycccc", + AlertScope: "org", + AlertScopeId: "1", + Enable: true, + Rules: []*pb.AlertExpression{ + { + Id: 44, + RuleId: 0, + AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Window: 1, + Functions: []*pb.AlertExpressionFunction{ + { + Field: "mem_used", + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(1), + }, + }, + IsRecover: false, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + }, + Notifies: []*pb.AlertNotify{ + { + Id: 0, + Type: "notify_group", + GroupId: 21, + GroupType: "mbox", + NotifyGroup: nil, + DingdingUrl: "", + Silence: &pb.AlertNotifySilence{ + Value: 5, + Unit: "minutes", + Policy: "fixed", + }, + CreateTime: 0, + UpdateTime: 0, + }, + }, + Filters: nil, + Attributes: map[string]*structpb.Value{ + "alert_domain": structpb.NewStringValue("https://erda.test.terminus.io"), + "alert_dashboard_path": structpb.NewStringValue("/dataCenter/customDashboard"), + "alert_record_path": structpb.NewStringValue("/dataCenter/alarm/record"), + "dice_org_id": structpb.NewStringValue("1"), + "cluster_name": structpb.NewListValue(clusterName), + }, + ClusterNames: []string{"terminus-test", "fdp-test"}, + Domain: "https://erda.test.terminus.io", + CreateTime: 0, + UpdateTime: 0, + }, + rule: &pb.AlertRule{ + Id: 44, + Name: "erda_test", + AlertScope: "org", + AlertType: "org_customize", + AlertIndex: &pb.DisplayKey{ + Key: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Display: "erda_test", + }, + Template: map[string]*structpb.Value{ + "filters": structpb.NewListValue(templateFilters), + "window": structpb.NewNumberValue(float64(1)), + "functions": structpb.NewListValue(templateFunctions), + "group": structpb.NewListValue(templateCluster), + "metric": structpb.NewStringValue("host_summary"), + "outputs": structpb.NewListValue(templateOutput), + "select": structpb.NewListValue(templateSelect), + }, + Window: 1, + Functions: []*pb.AlertRuleFunction{ + { + Field: &pb.DisplayKey{ + Key: "mem_used", + Display: "mem_used", + }, + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(float64(1)), + DataType: "number", + Unit: "", + }, + }, + IsRecover: false, + Attributes: map[string]*structpb.Value{ + "active_metric_groups": structpb.NewListValue(ruleAttribute), + "alert_dashboard_id": structpb.NewStringValue("e1730156951c47ec90ea79552b596a39"), + "alert_group": structpb.NewStringValue("{{cluster_name}}-{{cluster_name}}"), + "level": structpb.NewStringValue("WARNING"), + "recover": structpb.NewStringValue("false"), + "tickets_metric_key": structpb.NewStringValue("{{cluster_name}}"), + }, + Version: "", + Enable: true, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ToDBAlertExpressionModel(tt.args.e, tt.args.orgName, tt.args.alert, tt.args.rule) + if (err != nil) != tt.wantErr { + t.Errorf("ToDBAlertExpressionModel() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got == nil { + t.Errorf("ToDBAlertExpressionModel() got = %v, want %v", got, tt.want) + } + }) + } +} From 6d1f8d57d21ce6402c47cd65b4df0b5438483222 Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Mon, 16 Aug 2021 12:51:09 +0800 Subject: [PATCH 6/8] add filters in test --- .../alert-apis/adapt/alert_convert_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go index 4644240afd6..6ca4be26340 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go @@ -226,6 +226,24 @@ func TestToDBAlertExpressionModel(t *testing.T) { "value": structpb.NewStringValue("xxxxxxx"), "dataType": structpb.NewStringValue(""), }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("cluster_name"), + "operator": structpb.NewStringValue("in"), + "value": structpb.NewStringValue("$cluster_name"), + "dataType": structpb.NewStringValue(""), + }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("_metric_scope"), + "operator": structpb.NewStringValue("eq"), + "value": structpb.NewStringValue("org"), + "dataType": structpb.NewStringValue(""), + }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("_metric_scope_id"), + "operator": structpb.NewStringValue("eq"), + "value": structpb.NewStringValue("terminus"), + "dataType": structpb.NewStringValue(""), + }, }) templateFunctions, _ := structpb.NewList([]interface{}{ map[string]*structpb.Value{ From 674fdad1f91bcea496f26a4e34ea4df5e31ecf30 Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Mon, 16 Aug 2021 13:04:47 +0800 Subject: [PATCH 7/8] add filters in test --- .../alert-apis/adapt/alert_convert_test.go | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go index 6ca4be26340..1b388bee2a2 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go @@ -245,6 +245,32 @@ func TestToDBAlertExpressionModel(t *testing.T) { "dataType": structpb.NewStringValue(""), }, }) + templateFilters2, _ := structpb.NewList([]interface{}{ + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("cluster_name"), + "operator": structpb.NewStringValue("neq"), + "value": structpb.NewNumberValue(float64(3)), + "dataType": structpb.NewStringValue(""), + }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("cluster_name"), + "operator": structpb.NewStringValue("in"), + "value": structpb.NewStringValue("$cluster_name"), + "dataType": structpb.NewStringValue(""), + }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("_metric_scope"), + "operator": structpb.NewStringValue("eq"), + "value": structpb.NewStringValue("org"), + "dataType": structpb.NewStringValue(""), + }, + map[string]*structpb.Value{ + "tag": structpb.NewStringValue("_metric_scope_id"), + "operator": structpb.NewStringValue("eq"), + "value": structpb.NewStringValue("terminus"), + "dataType": structpb.NewStringValue(""), + }, + }) templateFunctions, _ := structpb.NewList([]interface{}{ map[string]*structpb.Value{ "unit": structpb.NewStringValue(""), @@ -393,6 +419,130 @@ func TestToDBAlertExpressionModel(t *testing.T) { }, }, }, + { + name: "TestToDBAlertExpressionModel", + args: args{ + e: &pb.AlertExpression{ + Id: 44, + RuleId: 0, + AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Window: 1, + Functions: []*pb.AlertExpressionFunction{ + { + Field: "mem_used", + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(1), + }, + }, + IsRecover: false, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + orgName: "terminus", + alert: &pb.Alert{ + Id: 56, + Name: "pjycccc", + AlertScope: "org", + AlertScopeId: "1", + Enable: true, + Rules: []*pb.AlertExpression{ + { + Id: 44, + RuleId: 0, + AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Window: 1, + Functions: []*pb.AlertExpressionFunction{ + { + Field: "mem_used", + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(1), + }, + }, + IsRecover: false, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + }, + Notifies: []*pb.AlertNotify{ + { + Id: 0, + Type: "notify_group", + GroupId: 21, + GroupType: "mbox", + NotifyGroup: nil, + DingdingUrl: "", + Silence: &pb.AlertNotifySilence{ + Value: 5, + Unit: "minutes", + Policy: "fixed", + }, + CreateTime: 0, + UpdateTime: 0, + }, + }, + Filters: nil, + Attributes: map[string]*structpb.Value{ + "alert_domain": structpb.NewStringValue("https://erda.test.terminus.io"), + "alert_dashboard_path": structpb.NewStringValue("/dataCenter/customDashboard"), + "alert_record_path": structpb.NewStringValue("/dataCenter/alarm/record"), + "dice_org_id": structpb.NewStringValue("1"), + "cluster_name": structpb.NewListValue(clusterName), + }, + ClusterNames: []string{"terminus-test", "fdp-test"}, + Domain: "https://erda.test.terminus.io", + CreateTime: 0, + UpdateTime: 0, + }, + rule: &pb.AlertRule{ + Id: 44, + Name: "erda_test", + AlertScope: "org", + AlertType: "org_customize", + AlertIndex: &pb.DisplayKey{ + Key: "96683ebf-a1de-4756-9f55-e2794e00a19e", + Display: "erda_test", + }, + Template: map[string]*structpb.Value{ + "filters": structpb.NewListValue(templateFilters2), + "window": structpb.NewNumberValue(float64(1)), + "functions": structpb.NewListValue(templateFunctions), + "group": structpb.NewListValue(templateCluster), + "metric": structpb.NewStringValue("host_summary"), + "outputs": structpb.NewListValue(templateOutput), + "select": structpb.NewListValue(templateSelect), + }, + Window: 1, + Functions: []*pb.AlertRuleFunction{ + { + Field: &pb.DisplayKey{ + Key: "mem_used", + Display: "mem_used", + }, + Aggregator: "sum", + Operator: "neq", + Value: structpb.NewNumberValue(float64(1)), + DataType: "number", + Unit: "", + }, + }, + IsRecover: false, + Attributes: map[string]*structpb.Value{ + "active_metric_groups": structpb.NewListValue(ruleAttribute), + "alert_dashboard_id": structpb.NewStringValue("e1730156951c47ec90ea79552b596a39"), + "alert_group": structpb.NewStringValue("{{cluster_name}}-{{cluster_name}}"), + "level": structpb.NewStringValue("WARNING"), + "recover": structpb.NewStringValue("false"), + "tickets_metric_key": structpb.NewStringValue("{{cluster_name}}"), + }, + Version: "", + Enable: true, + CreateTime: 1628821079000, + UpdateTime: 1628821079000, + }, + }, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { From 7830c9a6e8a3bf2b27f100da18594bcd156e171b Mon Sep 17 00:00:00 2001 From: pjy <1736989646@qq.com> Date: Mon, 16 Aug 2021 16:55:07 +0800 Subject: [PATCH 8/8] modify filters in template --- .../alert-apis/adapt/alert_convert_test.go | 202 ++---------------- 1 file changed, 16 insertions(+), 186 deletions(-) diff --git a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go index 1b388bee2a2..49630cff600 100644 --- a/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go +++ b/modules/core/monitor/alert/alert-apis/adapt/alert_convert_test.go @@ -219,68 +219,22 @@ func TestToDBAlertExpressionModel(t *testing.T) { "machine_mem", "machine", }) - templateFilters, _ := structpb.NewList([]interface{}{ - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("cluster_name"), - "operator": structpb.NewStringValue("neq"), - "value": structpb.NewStringValue("xxxxxxx"), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("cluster_name"), - "operator": structpb.NewStringValue("in"), - "value": structpb.NewStringValue("$cluster_name"), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("_metric_scope"), - "operator": structpb.NewStringValue("eq"), - "value": structpb.NewStringValue("org"), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("_metric_scope_id"), - "operator": structpb.NewStringValue("eq"), - "value": structpb.NewStringValue("terminus"), - "dataType": structpb.NewStringValue(""), - }, - }) - templateFilters2, _ := structpb.NewList([]interface{}{ - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("cluster_name"), - "operator": structpb.NewStringValue("neq"), - "value": structpb.NewNumberValue(float64(3)), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("cluster_name"), - "operator": structpb.NewStringValue("in"), - "value": structpb.NewStringValue("$cluster_name"), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("_metric_scope"), - "operator": structpb.NewStringValue("eq"), - "value": structpb.NewStringValue("org"), - "dataType": structpb.NewStringValue(""), - }, - map[string]*structpb.Value{ - "tag": structpb.NewStringValue("_metric_scope_id"), - "operator": structpb.NewStringValue("eq"), - "value": structpb.NewStringValue("terminus"), - "dataType": structpb.NewStringValue(""), + templateFilters0, _ := structpb.NewList([]interface{}{ + map[string]interface{}{ + "tag": "cluster_name", + "operator": "neq", + "value": "xxxxxxx", + "dataType": "", }, }) - templateFunctions, _ := structpb.NewList([]interface{}{ - map[string]*structpb.Value{ - "unit": structpb.NewStringValue(""), - "field": structpb.NewStringValue("mem_used"), - "alias": structpb.NewStringValue("sdf"), - "aggregator": structpb.NewStringValue("sum"), - "operator": structpb.NewStringValue("neq"), - "value": structpb.NewNumberValue(float64(1)), - "dataType": structpb.NewStringValue(""), - }, + templateFunctions, _ := structpb.NewStruct(map[string]interface{}{ + "unit": "", + "field": "mem_used", + "alias": "sdf", + "aggregator": "sum", + "operator": "neq", + "value": 1, + "dataType": "", }) templateSelect, _ := structpb.NewList([]interface{}{ map[string]*structpb.Value{ @@ -381,133 +335,9 @@ func TestToDBAlertExpressionModel(t *testing.T) { Display: "erda_test", }, Template: map[string]*structpb.Value{ - "filters": structpb.NewListValue(templateFilters), - "window": structpb.NewNumberValue(float64(1)), - "functions": structpb.NewListValue(templateFunctions), - "group": structpb.NewListValue(templateCluster), - "metric": structpb.NewStringValue("host_summary"), - "outputs": structpb.NewListValue(templateOutput), - "select": structpb.NewListValue(templateSelect), - }, - Window: 1, - Functions: []*pb.AlertRuleFunction{ - { - Field: &pb.DisplayKey{ - Key: "mem_used", - Display: "mem_used", - }, - Aggregator: "sum", - Operator: "neq", - Value: structpb.NewNumberValue(float64(1)), - DataType: "number", - Unit: "", - }, - }, - IsRecover: false, - Attributes: map[string]*structpb.Value{ - "active_metric_groups": structpb.NewListValue(ruleAttribute), - "alert_dashboard_id": structpb.NewStringValue("e1730156951c47ec90ea79552b596a39"), - "alert_group": structpb.NewStringValue("{{cluster_name}}-{{cluster_name}}"), - "level": structpb.NewStringValue("WARNING"), - "recover": structpb.NewStringValue("false"), - "tickets_metric_key": structpb.NewStringValue("{{cluster_name}}"), - }, - Version: "", - Enable: true, - CreateTime: 1628821079000, - UpdateTime: 1628821079000, - }, - }, - }, - { - name: "TestToDBAlertExpressionModel", - args: args{ - e: &pb.AlertExpression{ - Id: 44, - RuleId: 0, - AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", - Window: 1, - Functions: []*pb.AlertExpressionFunction{ - { - Field: "mem_used", - Aggregator: "sum", - Operator: "neq", - Value: structpb.NewNumberValue(1), - }, - }, - IsRecover: false, - CreateTime: 1628821079000, - UpdateTime: 1628821079000, - }, - orgName: "terminus", - alert: &pb.Alert{ - Id: 56, - Name: "pjycccc", - AlertScope: "org", - AlertScopeId: "1", - Enable: true, - Rules: []*pb.AlertExpression{ - { - Id: 44, - RuleId: 0, - AlertIndex: "96683ebf-a1de-4756-9f55-e2794e00a19e", - Window: 1, - Functions: []*pb.AlertExpressionFunction{ - { - Field: "mem_used", - Aggregator: "sum", - Operator: "neq", - Value: structpb.NewNumberValue(1), - }, - }, - IsRecover: false, - CreateTime: 1628821079000, - UpdateTime: 1628821079000, - }, - }, - Notifies: []*pb.AlertNotify{ - { - Id: 0, - Type: "notify_group", - GroupId: 21, - GroupType: "mbox", - NotifyGroup: nil, - DingdingUrl: "", - Silence: &pb.AlertNotifySilence{ - Value: 5, - Unit: "minutes", - Policy: "fixed", - }, - CreateTime: 0, - UpdateTime: 0, - }, - }, - Filters: nil, - Attributes: map[string]*structpb.Value{ - "alert_domain": structpb.NewStringValue("https://erda.test.terminus.io"), - "alert_dashboard_path": structpb.NewStringValue("/dataCenter/customDashboard"), - "alert_record_path": structpb.NewStringValue("/dataCenter/alarm/record"), - "dice_org_id": structpb.NewStringValue("1"), - "cluster_name": structpb.NewListValue(clusterName), - }, - ClusterNames: []string{"terminus-test", "fdp-test"}, - Domain: "https://erda.test.terminus.io", - CreateTime: 0, - UpdateTime: 0, - }, - rule: &pb.AlertRule{ - Id: 44, - Name: "erda_test", - AlertScope: "org", - AlertType: "org_customize", - AlertIndex: &pb.DisplayKey{ - Key: "96683ebf-a1de-4756-9f55-e2794e00a19e", - Display: "erda_test", - }, - Template: map[string]*structpb.Value{ - "filters": structpb.NewListValue(templateFilters2), + "filters": structpb.NewListValue(templateFilters0), "window": structpb.NewNumberValue(float64(1)), - "functions": structpb.NewListValue(templateFunctions), + "functions": structpb.NewStructValue(templateFunctions), "group": structpb.NewListValue(templateCluster), "metric": structpb.NewStringValue("host_summary"), "outputs": structpb.NewListValue(templateOutput),