Add exceptionsToIgnore configuration support in @SentinelResource annotation (#683)
This commit is contained in:
parent
cb9335126c
commit
08611fae0f
|
|
@ -65,4 +65,14 @@ public @interface SentinelResource {
|
||||||
* @since 1.5.1
|
* @since 1.5.1
|
||||||
*/
|
*/
|
||||||
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
|
Class<? extends Throwable>[] exceptionsToTrace() default {Throwable.class};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates the exceptions to be ignored. Note that {@code exceptionsToTrace} should
|
||||||
|
* not appear with {@code exceptionsToIgnore} at the same time, or {@code exceptionsToIgnore}
|
||||||
|
* will be of higher precedence.
|
||||||
|
*
|
||||||
|
* @return the list of exception classes to ignore, empty by default
|
||||||
|
* @since 1.6.0
|
||||||
|
*/
|
||||||
|
Class<? extends Throwable>[] exceptionsToIgnore() default {};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,11 @@ public abstract class AbstractSentinelAspectSupport {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void traceException(Throwable ex, SentinelResource annotation) {
|
protected void traceException(Throwable ex, SentinelResource annotation) {
|
||||||
|
Class<? extends Throwable>[] exceptionsToIgnore = annotation.exceptionsToIgnore();
|
||||||
|
// The ignore list will be checked first.
|
||||||
|
if (exceptionsToIgnore.length > 0 && isIgnoredException(ex, exceptionsToIgnore)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (isTracedException(ex, annotation.exceptionsToTrace())) {
|
if (isTracedException(ex, annotation.exceptionsToTrace())) {
|
||||||
Tracer.trace(ex);
|
Tracer.trace(ex);
|
||||||
}
|
}
|
||||||
|
|
@ -86,8 +91,10 @@ public abstract class AbstractSentinelAspectSupport {
|
||||||
/**
|
/**
|
||||||
* Check whether the exception is in tracked list of exception classes.
|
* Check whether the exception is in tracked list of exception classes.
|
||||||
*
|
*
|
||||||
* @param ex provided throwable
|
* @param ex
|
||||||
* @param exceptionsToTrace list of exceptions to trace
|
* provided throwable
|
||||||
|
* @param exceptionsToTrace
|
||||||
|
* list of exceptions to trace
|
||||||
* @return true if it should be traced, otherwise false
|
* @return true if it should be traced, otherwise false
|
||||||
*/
|
*/
|
||||||
private boolean isTracedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) {
|
private boolean isTracedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) {
|
||||||
|
|
@ -102,6 +109,18 @@ public abstract class AbstractSentinelAspectSupport {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isIgnoredException(Throwable ex, Class<? extends Throwable>[] exceptionsToIgnore) {
|
||||||
|
if (exceptionsToIgnore == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (Class<? extends Throwable> exceptionToIgnore : exceptionsToIgnore) {
|
||||||
|
if (exceptionToIgnore.isAssignableFrom(ex.getClass())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isDegradeFailure(/*@NonNull*/ BlockException ex) {
|
private boolean isDegradeFailure(/*@NonNull*/ BlockException ex) {
|
||||||
return ex instanceof DegradeException;
|
return ex instanceof DegradeException;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,22 @@ public class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringConte
|
||||||
fooService.baz("Sentinel");
|
fooService.baz("Sentinel");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAnnotationExceptionsToIgnore() {
|
||||||
|
assertThat(fooService.baz("Sentinel")).isEqualTo("cheers, Sentinel");
|
||||||
|
String resourceName = "apiBaz";
|
||||||
|
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
|
||||||
|
assertThat(cn).isNotNull();
|
||||||
|
assertThat(cn.passQps()).isPositive();
|
||||||
|
|
||||||
|
try {
|
||||||
|
fooService.baz("fail");
|
||||||
|
fail("should not reach here");
|
||||||
|
} catch (IllegalMonitorStateException ex) {
|
||||||
|
assertThat(cn.exceptionQps()).isZero();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNormalBlockHandlerAndFallback() throws Exception {
|
public void testNormalBlockHandlerAndFallback() throws Exception {
|
||||||
assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
|
assertThat(fooService.foo(1)).isEqualTo("Hello for 1");
|
||||||
|
|
|
||||||
|
|
@ -49,8 +49,12 @@ public class FooService {
|
||||||
return ThreadLocalRandom.current().nextInt(0, 30000);
|
return ThreadLocalRandom.current().nextInt(0, 30000);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler")
|
@SentinelResource(value = "apiBaz", blockHandler = "bazBlockHandler",
|
||||||
|
exceptionsToIgnore = {IllegalMonitorStateException.class})
|
||||||
public String baz(String name) {
|
public String baz(String name) {
|
||||||
|
if (name.equals("fail")) {
|
||||||
|
throw new IllegalMonitorStateException("boom!");
|
||||||
|
}
|
||||||
return "cheers, " + name;
|
return "cheers, " + name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue