浏览代码

Add exceptionsToTrace configuration support in @SentinelResource annotation (#543)

master
beston123 Eric Zhao 5 年前
父节点
当前提交
6e1dfb374c
共有 2 个文件被更改,包括 29 次插入7 次删除
  1. +7
    -6
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java
  2. +22
    -1
      sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java

+ 7
- 6
sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java 查看文件

@@ -15,14 +15,10 @@
*/
package com.alibaba.csp.sentinel.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.alibaba.csp.sentinel.EntryType;

import java.lang.annotation.*;

/**
* The annotation indicates a definition of Sentinel resource.
*
@@ -63,4 +59,9 @@ public @interface SentinelResource {
* @return name of the fallback function, empty by default
*/
String fallback() default "";

/**
* @return the exception classes to trace, Throwable.class by default
*/
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
}

+ 22
- 1
sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java 查看文件

@@ -59,7 +59,9 @@ public class SentinelResourceAspect extends AbstractSentinelAspectSupport {
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Tracer.trace(ex);
if (isTrackedException(ex, annotation.exceptionsToTrace())) {
Tracer.trace(ex);
}
throw ex;
} finally {
if (entry != null) {
@@ -67,4 +69,23 @@ public class SentinelResourceAspect extends AbstractSentinelAspectSupport {
}
}
}

/**
* whether the exception is in tracked list of exception classes
*
* @param ex
* @param exceptionsToTrace
* @return
*/
private boolean isTrackedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) {
if (exceptionsToTrace == null) {
return false;
}
for (Class exceptionToTrace : exceptionsToTrace) {
if (exceptionToTrace.isAssignableFrom(ex.getClass())) {
return true;
}
}
return false;
}
}

正在加载...
取消
保存