Bladeren bron

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

master
beston123 Eric Zhao 5 jaren geleden
bovenliggende
commit
6e1dfb374c
2 gewijzigde bestanden met toevoegingen van 29 en 7 verwijderingen
  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 Bestand weergeven

@@ -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 Bestand weergeven

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

Laden…
Annuleren
Opslaan