1

I successfully configured spring-cloud (via spring-cloud-starter-hystrix) to wrap a call to a service.

This all works fine and looks like the following:

@Component
public class MyService {

  @HystrixCommand(fallbackMethod = "fallback")
  public void longRunning() {
     // this could fail
  }

  public void fallback() {
    // fallback code
  }
}

My question now is, I would like to log some statistics about the execution error in longRunning()

Trying to access HystrixRequestLog.getCurrentRequest() within the fallback method throws

java.lang.IllegalStateException: HystrixRequestContext.initializeContext() must be called at the beginning of each request before RequestVariable functionality can be used.

I am looking for a simple way to log the exception of longRunning if the fallback is called.

testing with v1.0.0.RC2

Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Joey
  • 1,349
  • 14
  • 26

2 Answers2

1

To see a stack trace you can just enable DEBUG logging in com.netflix.hystrix.

As far as I can tell, to use the HystrixRequestContext the caller of MyService has to call HystrixRequestContext.initializeContext() before using the service. That sucks, so if anyone has a better idea, I'm interested.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • Sounds a little cumbersome to me too, would be interesting if there are nicer ways to log errors to make it easier to find out what went wrong in production... – Patrick Cornelissen May 30 '15 at 08:23
1

Starting from Javanica v1.4.21, it allows fallback method to have an argument of Throwable type for accessing the command execution exception like so:

public void fallback(Throwable e) {
   // fallback code
   LOGGER.error(e.getMessage());
}

To get this feature, your build config needs to override the older version of Javanica pulled in by Spring Cloud.

Tommy Situ
  • 156
  • 2
  • 5