Hystrix icon indicating copy to clipboard operation
Hystrix copied to clipboard

How to get the reason why hystrix execute fallbackMethod

Open chinaerserver opened this issue 6 years ago • 7 comments

I want to get the real reason why hystrix execute fallbackMethod ,like timeout , thread pool reject etc.. How can I get this fallback reason for each different method. e.g. @HystrixCommand(fallbackMethod=fb) public String foo(){return "bo";} public String fb(){ log.warn("why fall back , the reason is timeout"); return "fo"; }

chinaerserver avatar Apr 11 '18 02:04 chinaerserver

You can use HystrixMetrics to find what your progarm happend. HystrixMetrics can tell you what kind of event happend in the past.

zhou-jered avatar Apr 18 '18 06:04 zhou-jered

What's more, you can registe an executionhook to receive excution event; The following code is an example with Spring: `@Configuration public class HystrixConfig {

@Autowired
public void configHystrixPlugin() {
    System.err.println("Configing Hystrix Pkugins");
    HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixCommandExecutionHook() {
        @Override
        public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) {
            if(commandInstance instanceof HystrixInvokableInfo) {
                HystrixInvokableInfo invokableInfo = (HystrixInvokableInfo)commandInstance;
                //do something with specfied exception here
            }
            return e;
        }
    });
}

}`

zhou-jered avatar Apr 18 '18 07:04 zhou-jered

Maybe this is an useful example for you,ex:

@HystrixCommand(fallbackMethod = "fb")
    public String foo() {
        return "bo";
    }

    public String fb(Throwable exception) {
        logger.warn("real exception : {}", exception.getMessage());
        return "fo";
    }

SoulSong avatar May 08 '18 05:05 SoulSong

@SoulSong I found this code style in hystrix-javanica demo ,thanks!

Javanica provides an ability to get execution exception (exception thrown that caused the failure of a command) within a fallback is being executed. A fallback method signature can be extended with an additional parameter in order to get an exception thrown by a command. Javanica exposes execution exception through additional parameter of fallback method. Execution exception is derived by calling method getExecutionException() as in vanilla hystrix.

chinaerserver avatar May 09 '18 02:05 chinaerserver

Maybe this is an useful example for you,ex:

@HystrixCommand(fallbackMethod = "fb")
    public String foo() {
        return "bo";
    }

    public String fb(Throwable exception) {
        logger.warn("real exception : {}", exception.getMessage());
        return "fo";
    }

In my case it doesn't work as expected. Throwable exception will present only if code inside HystrixCommand would throw real Exception. In cases of timeout, thread pool overflow, and short circuited state exception wil be equals to null. I'm using Hystrix Javanica 1.4.21.

KirillLogin avatar Jan 24 '19 08:01 KirillLogin

@KirillLogin did you find a way to accomplish this?

riser101 avatar Apr 01 '20 07:04 riser101

@riser101 You could use the getFailedExecutionException() method to figure out the cause, message et al. for the underlying exception. Something like below:

@Override
    public String getFallback() {
        return getFailedExecutionException().getMessage();
    }

darkdefender27 avatar Apr 13 '20 13:04 darkdefender27