Sumanth Varada
Sumanth Varada

Reputation: 1190

How to use MDC in with hystrix in Spring Boot 2?

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.

Upvotes: 0

Views: 1477

Answers (1)

FlapPy
FlapPy

Reputation: 170

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.

Upvotes: 2

Related Questions