I’m using Axon 4.7.4 with Spring Boot 3.0.6.
I’ve noticed that JDBC inserts and updates are not reported as part of traces generated for event processors. I think this is because Axon creates its spans via handler interceptors, which is too late to pick up other things happening in the unit of work.
For example, the constructor of PooledStreamingEventProcessor
registers a handler interceptor responsible for creating a linked handler span. However, as hibernate postpones inserts and updates until the transaction commits, those are not included in the processor span. If we, instead, create a span near the creation of UoW, inserts and updates should be included.
I’ve experimented with the idea and modified the PooledStreamingEventProcessor
in two ways. First, I’ve changed the registration of the handler interceptor in the constructor in the following way:
registerHandlerInterceptor((unitOfWork, interceptorChain) -> spanFactory
.createInternalSpan(
() -> "PooledStreamingEventProcessor[" + builder.name() + "] ",
unitOfWork.getMessage())
.runCallable(interceptorChain::proceed));
Second, to limit the change to a PooledStreamingEventProcessor
only, I added the processInUnitOfWork()
override in a pooled processor like this:
@Override
protected void processInUnitOfWork(
List<? extends EventMessage<?>> eventMessages,
UnitOfWork<? extends EventMessage<?>> unitOfWork,
Collection<Segment> processingSegments) throws Exception
{
Span unitOfWorkSpan = spanFactory.createLinkedHandlerSpan(() -> "processInUnitOfWork ", unitOfWork.getMessage());
unitOfWorkSpan.start();
try (SpanScope unused = unitOfWorkSpan.makeCurrent()) {
super.processInUnitOfWork(eventMessages, unitOfWork, processingSegments);
}
finally {
unitOfWorkSpan.end();
}
}
After those changes, my traces now include SQL updates and inserts, as seen in the following pictures. The first corresponds to the original Axon behavior, while the second is after my changes.
What do you think?