Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Sleuth v2.2.3 how to propagate TraceId to another thread

I am trying to setup tracing of multithread application. I have set up ThreadPool:

ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("XXXXXX-");
        executor.initialize();

so there are several threads. One of the threads is listening to the SQS queue:

@Scheduled(fixedRateString = "${processing.queue.period:60000}")
public void process() {
..........................................................................
    for (SQSMessage sqsMessage : sqsMessages) {
        String messageReceiptHandle = null;
        try {
             messageReceiptHandle = sqsMessage.getReceiptHandle();
             processMessage(sqsMessage);
        }                               
    }
}
..........................................................................

    public void processMessage(SQSMessage sqsMessage) throws InterruptedException {

        log.debug("Start Processing request: '{}'.", sqsMessage);
        monitoringWorker.addEntityForMonitoring(sqsMessage, processor);
        processor.processMessage(sqsMessage);
        monitoringWorker.removeEntityForMonitoring(sqsMessage.getStagingPrefix());
    }

In this code snipet

monitoringWorker.addEntityForMonitoring(sqsMessage, processor);

sqsMessage is propagated to another scheduler:

@Component
public class MonitoringWorker {

   private List<EntityToMonitor> entitiesForMonitoring = new ArrayList<>();

   public void addEntityForMonitoring(AssetSQSMessage assetSQSMessage, AssetProcessorService service) {
        entitiesForMonitoring.add(new EntityToMonitor(assetSQSMessage, service));
    }

   @Scheduled(fixedDelay = 1000)
   public void monitor() {
        for (EntityToMonitor entity : entitiesForMonitoring) {
            log.info("testmessage");
        }
   }

the idea is to trace all the process from recieving message from SQS till the end of processing logic but all the thread has different trace ids, And when MonitoringWorker starts to process the message it takes another thread with different traceid:

enter image description here

How can I keep the same traceid, spanid could be different in Spring Cloud Sleuth 2.2.3

like image 224
Dezmond1983 Avatar asked Nov 15 '25 23:11

Dezmond1983


1 Answers

Use LazyTraceExecutor. This class propagates traceIds to new threads and create new spanIds in the process.

For more details see

  • @Async Support
  • @Scheduled Support
like image 181
Sudhir Avatar answered Nov 17 '25 22:11

Sudhir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!