简体   繁体   中英

Sleuth traceId and spanId are not propagated to parallelStream worker threads

I have integrated Sleuth to my Spring Boot project in order to have a better traceability. It logs perfectly traceId and spanId. However, these fields are not added to logs generated in some operations performed using parallelStream.

Sleuth doc suggests to use CompletableFuture instead :

Sleuth does not work with parallelStream() out of the box. If you want to have the tracing information propagated through the stream, you have to use the approach with supplyAsync(...), as shown earlier

But it says that parallelStream does not work "out of the box". So, is there any workaround to use parallelStream?

Thanks for any help or comment on this

The closest you can do is to call a completable future

CompletableFuture completableFuture = CompletableFuture.supplyAsync(() -> {
                ingredientsCollector.collectIngredients(order, processId).stream()
                        .filter(ingredient -> ingredient != null)
                        .forEach((Ingredient ingredient) -> {
                            log.info("Adding an ingredient [{}] for order [{}] , processId [{}]", ingredient);
                            ingredientWarehouse.addIngredient(ingredient);
                        });
                return null;
            }, new TraceableExecutorService(this.beanFactory, Executors.newFixedThreadPool(5), "fetchIngredients"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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