diff --git a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java b/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java index 78274443..37e613b0 100644 --- a/sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java +++ b/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[] exceptionsToTrace() default {Throwable.class}; } diff --git a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java b/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java index 7f2285e5..a4a78ec9 100644 --- a/sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java +++ b/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[] exceptionsToTrace) { + if (exceptionsToTrace == null) { + return false; + } + for (Class exceptionToTrace : exceptionsToTrace) { + if (exceptionToTrace.isAssignableFrom(ex.getClass())) { + return true; + } + } + return false; + } }