Browse Source

Improve SentinelResourceAspect and update test cases / document of annotation support

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
master
Eric Zhao 5 years ago
parent
commit
61c525076f
6 changed files with 45 additions and 26 deletions
  1. +2
    -1
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java
  2. +1
    -0
      sentinel-extension/sentinel-annotation-aspectj/README.md
  3. +26
    -0
      sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java
  4. +1
    -23
      sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java
  5. +9
    -1
      sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java
  6. +6
    -1
      sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/FooService.java

+ 2
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/annotation/SentinelResource.java View File

@@ -61,7 +61,8 @@ public @interface SentinelResource {
String fallback() default "";

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

+ 1
- 0
sentinel-extension/sentinel-annotation-aspectj/README.md View File

@@ -11,6 +11,7 @@ The `@SentinelResource` annotation indicates a resource definition, including:
- `entryType`: Resource entry type (inbound or outbound), `EntryType.OUT` by default
- `fallback`: Fallback method when degraded (optional). The fallback method should be located in the same class with original method. The signature of the fallback method should match the original method (parameter types and return type).
- `blockHandler`: Handler method that handles `BlockException` when blocked. The signature should match original method, with the last additional parameter type `BlockException`. The block handler method should be located in the same class with original method by default. If you want to use method in other classes, you can set the `blockHandlerClass` with corresponding `Class` (Note the method in other classes must be *static*).
- `exceptionsToTrace`: List of business exception classes to trace and record (since 1.5.1).

For example:



+ 26
- 0
sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/AbstractSentinelAspectSupport.java View File

@@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.annotation.aspectj;

import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
@@ -76,6 +77,31 @@ public abstract class AbstractSentinelAspectSupport {
throw ex;
}

protected void traceException(Throwable ex, SentinelResource annotation) {
if (isTracedException(ex, annotation.exceptionsToTrace())) {
Tracer.trace(ex);
}
}

/**
* Check whether the exception is in tracked list of exception classes.
*
* @param ex provided throwable
* @param exceptionsToTrace list of exceptions to trace
* @return true if it should be traced, otherwise false
*/
private boolean isTracedException(Throwable ex, Class<? extends Throwable>[] exceptionsToTrace) {
if (exceptionsToTrace == null) {
return false;
}
for (Class<? extends Throwable> exceptionToTrace : exceptionsToTrace) {
if (exceptionToTrace.isAssignableFrom(ex.getClass())) {
return true;
}
}
return false;
}

private boolean isDegradeFailure(/*@NonNull*/ BlockException ex) {
return ex instanceof DegradeException;
}


+ 1
- 23
sentinel-extension/sentinel-annotation-aspectj/src/main/java/com/alibaba/csp/sentinel/annotation/aspectj/SentinelResourceAspect.java View File

@@ -18,7 +18,6 @@ package com.alibaba.csp.sentinel.annotation.aspectj;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -59,9 +58,7 @@ public class SentinelResourceAspect extends AbstractSentinelAspectSupport {
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
if (isTrackedException(ex, annotation.exceptionsToTrace())) {
Tracer.trace(ex);
}
traceException(ex, annotation);
throw ex;
} finally {
if (entry != null) {
@@ -69,23 +66,4 @@ 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;
}
}

+ 9
- 1
sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/SentinelAnnotationIntegrationTest.java View File

@@ -98,7 +98,15 @@ public class SentinelAnnotationIntegrationTest extends AbstractJUnit4SpringConte
try {
fooService.foo(5758);
fail("should not reach here");
} catch (IllegalAccessException ex) {
} catch (Exception ex) {
// Should not be traced.
assertThat(cn.exceptionQps()).isZero();
}

try {
fooService.foo(5763);
fail("should not reach here");
} catch (Exception ex) {
assertThat(cn.exceptionQps()).isPositive();
}



+ 6
- 1
sentinel-extension/sentinel-annotation-aspectj/src/test/java/com/alibaba/csp/sentinel/annotation/aspectj/integration/service/FooService.java View File

@@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.annotation.aspectj.integration.service;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;

import org.springframework.stereotype.Service;

import java.util.concurrent.ThreadLocalRandom;
@@ -28,7 +29,8 @@ import java.util.concurrent.ThreadLocalRandom;
@Service
public class FooService {

@SentinelResource(value = "apiFoo", blockHandler = "fooBlockHandler", fallback = "fooFallbackFunc")
@SentinelResource(value = "apiFoo", blockHandler = "fooBlockHandler", fallback = "fooFallbackFunc",
exceptionsToTrace = {IllegalArgumentException.class})
public String foo(int i) throws Exception {
if (i == 9527) {
throw new DegradeException("ggg");
@@ -36,6 +38,9 @@ public class FooService {
if (i == 5758) {
throw new IllegalAccessException();
}
if (i == 5763) {
throw new IllegalArgumentException();
}
return "Hello for " + i;
}



Loading…
Cancel
Save