Skip to content

Commit

Permalink
Add id field in Rule entity and record ruleId in block log (alibaba#2853
Browse files Browse the repository at this point in the history
)

* Add id field (Long) in Rule
* optimize BlockException log
  • Loading branch information
icodening authored Aug 31, 2022
1 parent bef6574 commit 6c74a4c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
*/
public abstract class AbstractRule implements Rule {

/**
* rule id.
*/
private Long id;

/**
* Resource name.
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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 {

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void entry(Context context, ResourceWrapper resourceWrapper, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -39,7 +39,7 @@ public void testChangeLogBase() throws Exception {
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);
Expand Down

0 comments on commit 6c74a4c

Please sign in to comment.