Ver Fonte

[alerter,webapp] 告警阈值触发表达式优化新增自定义equals函数,告警配置页面完善tip

tomsun28 há 4 anos atrás
pai
commit
8ec8740ab2

+ 31 - 1
alerter/src/main/java/com/usthe/alert/AlerterConfiguration.java

@@ -1,9 +1,14 @@
 package com.usthe.alert;
 
 import com.googlecode.aviator.AviatorEvaluator;
+import com.googlecode.aviator.runtime.function.AbstractFunction;
+import com.googlecode.aviator.runtime.type.AviatorBoolean;
+import com.googlecode.aviator.runtime.type.AviatorObject;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
+import java.util.Map;
+
 /**
  * @author tomsun28
  * @date 2021/11/3 12:55
@@ -17,6 +22,31 @@ public class AlerterConfiguration {
     public void configAviatorEvaluator() {
         // 配置AviatorEvaluator使用LRU缓存编译后的表达式
         AviatorEvaluator.getInstance()
-                .useLRUExpressionCache(AVIATOR_LRU_CACHE_SIZE);
+                .useLRUExpressionCache(AVIATOR_LRU_CACHE_SIZE)
+                .addFunction(new StrEqualFunction());
+    }
+
+    /**
+     * 自定义aviator判断字符串是否相等函数
+     */
+    private static class StrEqualFunction extends AbstractFunction {
+        @Override
+        public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
+            if (arg1 == null || arg2 == null) {
+                return AviatorBoolean.valueOf(false);
+            }
+            Object leftTmp = arg1.getValue(env);
+            Object rightTmp = arg2.getValue(env);
+            if (leftTmp == null || rightTmp == null) {
+                return AviatorBoolean.valueOf(false);
+            }
+            String left = String.valueOf(leftTmp);
+            String right = String.valueOf(rightTmp);
+            return AviatorBoolean.valueOf(left.equalsIgnoreCase(right));
+        }
+        @Override
+        public String getName() {
+            return "equals";
+        }
     }
 }

+ 2 - 2
alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java

@@ -2,7 +2,6 @@ package com.usthe.alert.calculate;
 
 import com.googlecode.aviator.AviatorEvaluator;
 import com.googlecode.aviator.Expression;
-import com.usthe.alert.AlerterProperties;
 import com.usthe.alert.AlerterWorkerPool;
 import com.usthe.alert.AlerterDataQueue;
 import com.usthe.alert.entrance.KafkaDataConsume;
@@ -116,7 +115,7 @@ public class CalculateAlarm {
         List<CollectRep.Field> fields = metricsData.getFieldsList();
         Map<String, Object> fieldValueMap = new HashMap<>(16);
         fieldValueMap.put("app", app);
-        fieldValueMap.put("metric", metrics);
+        fieldValueMap.put("metrics", metrics);
         for (CollectRep.ValueRow valueRow : metricsData.getValuesList()) {
             if (!valueRow.getColumnsList().isEmpty()) {
                 String instance = valueRow.getInstance();
@@ -128,6 +127,7 @@ public class CalculateAlarm {
                 for (int index = 0; index < valueRow.getColumnsList().size(); index++) {
                     String valueStr = valueRow.getColumns(index);
                     CollectRep.Field field = fields.get(index);
+                    fieldValueMap.put("metric", field.getName());
                     if (field.getType() == CommonConstants.TYPE_NUMBER) {
                         Double doubleValue = CommonUtil.parseDoubleStr(valueStr);
                         if (doubleValue != null) {

+ 58 - 19
web-app/src/app/routes/alert/alert-setting/alert-setting.component.html

@@ -128,7 +128,7 @@
           <nz-collapse>
             <nz-collapse-panel
               [nzActive] = "isManageModalAdd"
-              nzHeader="支持的阈值触发表达式环境变量"
+              nzHeader="支持的阈值触发表达式环境变量与操作符"
             >
               <nz-list nzSize="small" nzSplit="false">
                 <nz-list-item *ngIf="cascadeValues.length == 3">
@@ -137,25 +137,30 @@
                 <nz-list-item>
                   <code>instance : 所属行实例值</code>
                 </nz-list-item>
+                <nz-list-item>
+                  <code>支持操作符函数 : equals(str1,str2), ==, <, <=, >, >=, !=, ( ), +, -, &&, ||</code>
+                </nz-list-item>
               </nz-list>
             </nz-collapse-panel>
           </nz-collapse>
         </nz-col>
       </nz-form-item>
       <nz-form-item>
-        <nz-form-label [nzSpan]="7" nzFor= 'expr' nzRequired="true">阈值触发表达式</nz-form-label>
+        <nz-form-label [nzSpan]="7" nzFor= 'expr' nzRequired="true" nzTooltipTitle="根据此表达式来计算判断是否触发阈值,表达式环境变量和操作符见上方">
+          阈值触发表达式
+        </nz-form-label>
         <nz-form-control [nzSpan]="8">
           <nz-textarea-count [nzMaxCharacterCount]="100">
             <textarea [(ngModel)]="define.expr" rows="3" nz-input
-                      name="expr" id="expr"  placeholder="请输入阈值判断表达式,示例: instance.equals(cpu1)&&usage>40">
+                      name="expr" id="expr"  placeholder="根据此表达式计算判断是否触发阈值.&#10;示例: equals&#40;instance,&quot;cpu1&quot;&#41; &amp;&amp; usage&gt;40">
             </textarea>
           </nz-textarea-count>
         </nz-form-control>
       </nz-form-item >
       <nz-form-item>
-        <nz-form-label nzSpan="7"
-                       nzRequired="true"
-                       nzFor= "priority">告警级别
+        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "priority"
+                       nzTooltipTitle="触发阈值的告警级别,从低到高依次为:警告-warning,严重-critical,紧急-emergency">
+          告警级别
         </nz-form-label>
         <nz-form-control nzSpan="8">
           <nz-select
@@ -171,31 +176,67 @@
         </nz-form-control>
       </nz-form-item>
       <nz-form-item>
-        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "duration">触发次数</nz-form-label>
+        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "duration" nzTooltipTitle="设置触发阈值多少次之后才会发送告警">
+          触发次数
+        </nz-form-label>
         <nz-form-control nzSpan="8">
           <nz-input-number [(ngModel)]="define.times" [nzMin]="1" [nzMax]="10" [nzStep]="1"
-                           name="duration" id="duration" nzPlaceHolder="触发几次后告警">
+                           name="duration" id="duration">
           </nz-input-number>
         </nz-form-control>
       </nz-form-item >
       <nz-form-item>
-        <nz-form-label [nzSpan]="7" nzFor= 'template' nzRequired="true">通知模版</nz-form-label>
+        <nz-col [nzSpan]="8" nzOffset="7">
+          <nz-collapse>
+            <nz-collapse-panel
+              [nzActive] = "isManageModalAdd"
+              nzHeader="支持的通知模版环境变量"
+            >
+              <nz-list nzSize="small" nzSplit="false">
+                <nz-list-item *ngIf="cascadeValues.length == 3">
+                  <code>&#36;&#40;metric&#41; : 选中的指标对象名称</code>
+                </nz-list-item>
+                <nz-list-item *ngIf="cascadeValues.length == 3">
+                  <code>&#36;&#40;{{cascadeValues[2]}}&#41; : 选中的指标对象值</code>
+                </nz-list-item>
+                <nz-list-item>
+                  <code>&#36;&#40;instance&#41; : 所在行实例值</code>
+                </nz-list-item>
+                <nz-list-item>
+                  <code>&#36;&#40;app&#41; : 所属监控类型名称</code>
+                </nz-list-item>
+                <nz-list-item>
+                  <code>&#36;&#40;metrics&#41; : 所属监控指标组名称</code>
+                </nz-list-item>
+              </nz-list>
+            </nz-collapse-panel>
+          </nz-collapse>
+        </nz-col>
+      </nz-form-item>
+      <nz-form-item>
+        <nz-form-label [nzSpan]="7" nzFor= 'template' nzRequired="true" nzTooltipTitle="告警触发后发送的通知信息模版,模版环境变量见上方">
+          通知模版
+        </nz-form-label>
         <nz-form-control [nzSpan]="8">
-          <nz-textarea-count [nzMaxCharacterCount]="100">
+          <nz-textarea-count [nzMaxCharacterCount]="200">
             <textarea [(ngModel)]="define.template" rows="3" nz-input
-                      name="template" id="template"  placeholder="请输入告警的通知模版">
+                      name="template" id="template"  placeholder="请输入告警的通知模版.&#10;示例: ${app}.${metrics}.${metric}'s value is too high">
             </textarea>
           </nz-textarea-count>
         </nz-form-control>
       </nz-form-item>
       <nz-form-item>
-        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "preset">全局默认</nz-form-label>
+        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "preset" nzTooltipTitle="此告警阈值配置是否应用于全局所有此类型监控">
+          全局默认
+        </nz-form-label>
         <nz-form-control nzSpan="8">
           <nz-switch [(ngModel)]="define.preset" name="preset" id="preset"></nz-switch>
         </nz-form-control>
       </nz-form-item >
       <nz-form-item>
-        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "enable">启用告警</nz-form-label>
+        <nz-form-label nzSpan="7" nzRequired="true" nzFor= "enable" nzTooltipTitle="此告警阈值配置开启生效或关闭">
+          启用告警
+        </nz-form-label>
         <nz-form-control nzSpan="8">
           <nz-switch [(ngModel)]="define.enable" name="enable" id="enable"></nz-switch>
         </nz-form-control>
@@ -234,12 +275,10 @@
       <nz-table #t [nzData]="$asTransferItems(items)" nzSize="small">
         <thead>
         <tr>
-          <th
-            [nzChecked]="stat.checkAll"
-            [nzIndeterminate]="stat.checkHalf"
-            (nzCheckedChange)="onItemSelectAll($event)"
-          ></th>
-          <th>监控</th>
+          <th [nzChecked]="stat.checkAll" [nzIndeterminate]="stat.checkHalf"
+            (nzCheckedChange)="onItemSelectAll($event)"></th>
+          <th *ngIf="direction=='left'">未关联监控</th>
+          <th *ngIf="direction=='right'">已关联监控</th>
         </tr>
         </thead>
         <tbody>