Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exceptionsToTrace configuration support in @SentinelResource annotation #543

Merged
merged 3 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -63,4 +59,9 @@
* @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};
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,33 @@ public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwab
} catch (BlockException ex) {
return handleBlockException(pjp, annotation, ex);
} catch (Throwable ex) {
Tracer.trace(ex);
if (isTrackedException(ex, annotation.exceptionsToTrace())) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can move the logic to AbstractSentinelAspectSupport class to support extension for sub-classes. E.g.:

protected void traceException(Throwable ex, SentinelResource annotation)

Tracer.trace(ex);
}
throw ex;
} finally {
if (entry != null) {
entry.exit();
}
}
}

/**
* 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;
}
}