Log errors from hystrix commands
The @HystrixCommand annotation can be configured with a backup method to be executed in case of method failure.
public Link defaultDogeLink(Account account) {
return null;
}
@HystrixCommand(fallbackMethod = "defaultDogeLink")
public Link buildDogeLink(Account account) {
// some code that may throw Runtime Exceptions
}
What should I do in order to log (in the central class) exceptions at runtime in all methods annotated with @HystrixCommand?
I am using spring-cloud-netflix and not vanilla hystrix-javanica. I'm looking for something similar to the org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler class (for Spring @Async) that I need to implement in my application.
In hystrix-core, the HystrixCommand class has a getFailedExecutionException () method that can be used in the backup method to log the exception. Can someone please point me to how to get this exception when working with hystrix-javanica?
source to share
Within hystrix-javanica unit tests, I found a way to get the last executed hystrix command:
public Link defaultDogeLink(Account account) {
LOG.warn("exception occured while building doge link for account " + account, getCommand().getFailedExecutionException());
return null;
}
@HystrixCommand(fallbackMethod = "defaultDogeLink")
public Link buildDogeLink(Account account) {
// some code that may throw Runtime Exceptions
}
private com.netflix.hystrix.HystrixInvokableInfo<?> getCommand() {
return HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
}
This is a little more verbose than expected, but fulfills my requirements.
source to share