I have Spring MVC Application where I have implemented a class which extends HandlerInterceptorAdapter for me to perform some operations inside preHandle, postHandle and afterCompletion before any controller gets executed. below is what I am observing.
Log Entry for preHandle logging AppTime: 00:30:01.230 | Thread: [http-nio-8080-exec-1] | Level: INFO | Class: hello.extractor | X-Span-Export=false, X-B3-SpanId=3aad3f52e26eb59d, X-B3-TraceId=3aad3f52e26eb59d | Msg: Inside preHandle
Log Entry for postHandle logging AppTime: 00:30:01.394 | Thread: [http-nio-8080-exec-1] | Level: INFO | Class: hello.extractor | X-Span-Export=false, X-B3-SpanId=3aad3f52e26eb59d, X-B3-TraceId=3aad3f52e26eb59d | Msg: Inside postHandle
Log Entry for afterCompletionlogging AppTime: 00:30:01.395 | Thread: [http-nio-8080-exec-1] | Level: INFO | Class: hello.extractor | | Msg: Inside afterCompletion.
if you see the third entry is missing trace ID and Span ID. Any idea on how to resolve this?
public class Extractor extends HandlerInterceptorAdapter {
private static Logger logger = LoggerFactory.getLogger(HttpHeaderExtractor.class);
@Autowired
public Extractor() {
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
logger.info("Inside preHandle");
return true;
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
logger.info("Inside postHandle");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
logger.info("Inside afterCompletion");
}
}
I found that we had added another HandlerIntercepter which was doing MDC Logging and inside in it we were manually clearing which is causing not to populate trace id in my another HandlerIntercepter aftercompletion method. Also if you are using multiple handler interceptors and the sequence you register matters. In latest spring, you will have declare them in reverse order for them execute one by one.