0

We are using hystrix in our spring boot application. We wants to use MDC for appending specific attributes like Request Id and Request URI and Loggedin User to every log statement. This mechanism is not working wherever hystrix is implemented.

@HystrixCommand(fallbackMethod = "callFallback", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000")}, ignoreExceptions = Exception.class)
public GenericResponse callUser(User user) {
  //Implementation
   log.info("Creating user called");
}

Its working perfectly fine for non hystrix annotated methods. I do understand that MDC is thread specific and hystrix does executes on seperate thread. Please suggest the work around as i can't comment the hystrix.

Sumanth Varada
  • 1,122
  • 1
  • 16
  • 17

1 Answers1

2

You can use HystrixCommandExecutionHook to ensure flow of ThreadLocal or MDC variables across all hystrix threads.

You can find complete documentation by netflix here.

For more help on how to use hook to solve your purpose, you can refer to this blog

In the blog, you can use MDC.get("key") & MDC.set("key", value) instead of getThreadLocals() & setThreadLocals() to solve your purpose.

FlapPy
  • 160
  • 11
  • Thanks @FlapPy I can see that it is ThreadLocal. Can i assume that the data in ThreadLocalUtil.java is thead safe ? – Sumanth Varada Dec 20 '19 at 15:29
  • `ThreadLocalUtil.java` is nothing but a wrapper with functions get/set data from/to a `ThreadLocal` variable. And since `ThreadLocal` is local to a single thread, it is definitely ThreadSafe, until you use Mutable objects like ArrayList, etc – FlapPy Dec 20 '19 at 15:52