Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify cluster_name and application_id in expression filters #1425

Merged
3 changes: 0 additions & 3 deletions .erda/migrations/monitor/20210812-monitor-rules-recover.sql
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions modules/core/monitor/alert/alert-apis/adapt/alert_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package adapt

import (
"fmt"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -465,6 +466,15 @@ func ToDBAlertExpressionModel(e *pb.AlertExpression, orgName string, alert *pb.A
if !ok {
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 {
Expand Down
5 changes: 5 additions & 0 deletions modules/core/monitor/alert/alert-apis/adapt/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ const (
Hours = "hours"
)

const (
ClusterName = "cluster_name"
ApplicationId = "application_id"
)

type (
// DisplayKey .
DisplayKey struct {
Expand Down