Repository: alibaba/Sentinel (java)

Issue:
feature: record rule ID in Sentinel | 希望能够在客户端保存规则的Id
服务端存储了rule的Id, 但是客户端并没有保存., 希望客户端也能够保存一下, 并且在sentinel-block.log中能够打印ruleId, 这样可以在规则比较多时候知道具体是触发的哪个规则的阈值

Pull request description:
optimize BlockException log
### Describe what this PR does / why we need it
优化BlockException日志，方便了解触发阻断异常的规则

### Does this pull request fix one issue?
Fixes #2851 

### Describe how you did it
给`AbstractRule`添加id属性即可

### Describe how to verify it
配置并触发规则后即可看到id

### Special notes for reviews
<img width="478" alt="image" src="https://user-images.githubusercontent.com/42876375/187246295-7fa51615-1ae1-4de4-8b02-5169cdbf8cb0.png">


Patch:
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java
index f6f10a76..befec3bc 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/block/AbstractRule.java
@@ -23,6 +23,11 @@ package com.alibaba.csp.sentinel.slots.block;
  */
 public abstract class AbstractRule implements Rule {
 
+    /**
+     * rule id.
+     */
+    private Long id;
+
     /**
      * Resource name.
      */
@@ -39,6 +44,15 @@ public abstract class AbstractRule implements Rule {
      */
     private String limitApp;
 
+    public Long getId() {
+        return id;
+    }
+
+    public AbstractRule setId(Long id) {
+        this.id = id;
+        return this;
+    }
+
     @Override
     public String getResource() {
         return resource;
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java
index c599bb8f..7c3ad4d2 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtil.java
@@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.slots.logger;
 import com.alibaba.csp.sentinel.eagleeye.EagleEye;
 import com.alibaba.csp.sentinel.eagleeye.StatLogger;
 import com.alibaba.csp.sentinel.log.LogBase;
+import com.alibaba.csp.sentinel.util.StringUtil;
 
 public class EagleEyeLogUtil {
 
@@ -40,7 +41,11 @@ public class EagleEyeLogUtil {
             .buildSingleton();
     }
 
-    public static void log(String resource, String exceptionName, String ruleLimitApp, String origin, int count) {
-        statLogger.stat(resource, exceptionName, ruleLimitApp, origin).count(count);
+    public static void log(String resource, String exceptionName, String ruleLimitApp, String origin, Long ruleId, int count) {
+        String ruleIdString = StringUtil.EMPTY;
+        if (ruleId != null) {
+            ruleIdString = String.valueOf(ruleId);
+        }
+        statLogger.stat(resource, exceptionName, ruleLimitApp, origin, ruleIdString).count(count);
     }
 }
diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java
index 948b9101..e70457f5 100755
--- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java
+++ b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/slots/logger/LogSlot.java
@@ -38,7 +38,7 @@ public class LogSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
             fireEntry(context, resourceWrapper, obj, count, prioritized, args);
         } catch (BlockException e) {
             EagleEyeLogUtil.log(resourceWrapper.getName(), e.getClass().getSimpleName(), e.getRuleLimitApp(),
-                context.getOrigin(), count);
+                context.getOrigin(), e.getRule().getId(), count);
             throw e;
         } catch (Throwable e) {
             RecordLog.warn("Unexpected entry exception", e);


Test patch:
diff --git a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
index 8a48bc7c..236780b8 100644
--- a/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
+++ b/sentinel-core/src/test/java/com/alibaba/csp/sentinel/slots/logger/EagleEyeLogUtilTest.java
@@ -19,7 +19,7 @@ public class EagleEyeLogUtilTest {
 
     @Test
     public void testWriteLog() throws Exception {
-        EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1);
+        EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1L,1);
 
         final File file = new File(LogBase.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME);
         await().timeout(2, TimeUnit.SECONDS)
@@ -39,7 +39,7 @@ public class EagleEyeLogUtilTest {
         String newLogBase = userHome + File.separator + "tmpLogDir" + System.currentTimeMillis();
         System.setProperty(LogBase.LOG_DIR, newLogBase);
 
-        EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 1);
+        EagleEyeLogUtil.log("resourceName", "BlockException", "app1", "origin", 2L,1);
 
 
         final File file = new File(LogBase.getLogBaseDir() + EagleEyeLogUtil.FILE_NAME);