Add id field in Rule entity and record ruleId in block log (#2853)

* Add id field (Long) in Rule
* optimize BlockException log
This commit is contained in:
一个不知名的Java靓仔 2022-08-31 10:32:33 +08:00 committed by GitHub
parent bef6574734
commit 6c74a4cf70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 5 deletions

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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);