I'm struggling with a problem regarding Spring State Machine and Spring Cloud Sleuth. After emitting event to change the state of my state machine, I'm not able to see the traceId anymore. The traceId just get lost.
As example, I have a simple State Machine with only three states:
@Configuration @EnableStateMachine
public class StateMachineConfiguration extends EnumStateMachineConfigurerAdapter<State, Event> {
@Override
public void configure(StateMachineStateConfigurer<State, Event> states) throws Exception {
states
.withStates()
.initial(State.STARTED)
.end(State.FINISHED)
.states(EnumSet.allOf(State.class));
}
@Override
public void configure(StateMachineTransitionConfigurer<State, Event> transitions) throws Exception {
transitions
.withExternal()
.source(State.STARTED).target(State.PROCESSING)
.event(Event.ANALYZE)
.and()
.withExternal()
.source(State.PROCESSING).target(State.FINISHED)
.event(Event.ANALYZE)
;
}
}
And a controller method with logs to see the states:
@RestController
public class StateMachineController {
@Autowired
private StateMachine<State, Event> stateMachine;
@GetMapping
public void stateMachineTest() {
log.info("Invoking state machine.");
stateMachine.start();
log.info(" --- {} / traceId = {}", stateMachine.getUuid(), MDC.get("X-B3-TraceId"));
stateMachine.sendEvent(Event.ANALYZE);
log.info(" --- {} / traceId = {}", stateMachine.getUuid(), MDC.get("X-B3-TraceId"));
stateMachine.sendEvent(Event.ANALYZE);
log.info(" --- {} / traceId = {}", stateMachine.getUuid(), MDC.get("X-B3-TraceId"));
}
}
The result of accessing this method is:
2020-06-24 17:54:49.212 INFO [spring-sm,c98b3d6a7a0eb04f,c98b3d6a7a0eb04f,false] 4156 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : Invoking state machine.
2020-06-24 17:54:49.238 INFO [spring-sm,c98b3d6a7a0eb04f,c98b3d6a7a0eb04f,false] 4156 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 41ac020d-5c25-4acd-b757-262c4491fcb7 / traceId = c98b3d6a7a0eb04f
2020-06-24 17:54:49.260 INFO [spring-sm,,,] 4156 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 41ac020d-5c25-4acd-b757-262c4491fcb7 / traceId = null
2020-06-24 17:54:49.262 INFO [spring-sm,,,] 4156 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 41ac020d-5c25-4acd-b757-262c4491fcb7 / traceId = null
We can see the traceId was lost after the first event to change the state and I have no idea what is going on. I'm currently using the following versions:
- Spring Boot 1.5.21.RELEASE
- Spring State Machine 2.2.0.RELEASE
- Spring Cloud Dependencies Edgware.RELEASE
Any ideas/suggestions on that?
I've noticed some minutes ago that this behaviour does not happen with Spring Boot 2.2.2.RELEASE and Spring Cloud Dependencies Hoxton.RELEASE. The same example above results:
2020-06-24 09:38:52.781 INFO [spring-sm,e96c38b34cd805bd,e96c38b34cd805bd,false] 7008 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : Invoking state machine.
2020-06-24 09:38:52.803 INFO [spring-sm,e96c38b34cd805bd,e96c38b34cd805bd,false] 7008 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 69de87f9-738e-429a-8e07-8e465260301c started
2020-06-24 09:38:52.815 INFO [spring-sm,e96c38b34cd805bd,e96c38b34cd805bd,false] 7008 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 69de87f9-738e-429a-8e07-8e465260301c in processing
2020-06-24 09:38:52.818 INFO [spring-sm,e96c38b34cd805bd,e96c38b34cd805bd,false] 7008 --- [nio-8082-exec-1] b.c.l.pf.spring.sm.SpringSmApplication : --- 69de87f9-738e-429a-8e07-8e465260301c finished
The big problem here is that I cannot update my "real project" to Spring Boot 2 right now.
Any kind of idea or suggestion will be really appreciated. Thanks in advance.