简体   繁体   English

Spring Cloud Sleuth v2.2.3 如何将 TraceId 传播到另一个线程

[英]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:其中一个线程正在监听 SQS 队列:

@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: sqsMessage 传播到另一个调度程序:

@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:这个想法是跟踪从接收来自 SQS 的消息到处理逻辑结束的所有过程,但所有线程都有不同的跟踪 ID,当 MonitoringWorker 开始处理消息时,它需要另一个具有不同 traceid 的线程:

在此处输入图像描述

How can I keep the same traceid, spanid could be different in Spring Cloud Sleuth 2.2.3我怎样才能保持相同的 traceid,spanid 在 Spring Cloud Sleuth 2.2.3 中可能会有所不同

Use LazyTraceExecutor .使用LazyTraceExecutor This class propagates traceIds to new threads and create new spanIds in the process.这个 class 将 traceIds 传播到新线程并在进程中创建新的 spanIds。

For more details see有关更多详细信息,请参阅

Ok, can somebody tell how to get Trace context in Spring Cloud Sleuth 2.2.3.好的,有人可以告诉如何在 Spring Cloud Sleuth 2.2.3 中获取跟踪上下文。 It is not possible just to @Autowire Trace object as it is described in google examples正如谷歌示例中所述,仅@Autowire Trace object 是不可能的

you can use LazyTraceThreadPoolTaskExecutor .您可以使用LazyTraceThreadPoolTaskExecutor

@Configuration
public class BeanConfigurations {

    private final BeanFactory beanFactory;

    @Autowired
    public BeanConfigurations(BeanFactory beanFactory){
        this.beanFactory=beanFactory;
    }

    @Bean("asyncExecutor")
    public TaskExecutor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor= new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(20);
        executor.setMaxPoolSize(30);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setThreadNamePrefix("Async-");
        return new LazyTraceThreadPoolTaskExecutor(beanFactory,executor);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM