Sleuth traceId和spanId不会传播到parallelStream工作线程

时间:2020-06-28 00:03:27

标签: java parallel-processing spring-cloud-sleuth

我已将Sleuth集成到我的Spring Boot项目中,以便具有更好的可跟踪性。它完美记录了traceId和spanId。但是,这些字段不会添加到使用parallelStream执行的某些操作中生成的日志中。

Sleuth doc suggests to use CompletableFuture instead

Sleuth不适用于parallelStream()。如果要使跟踪信息通过流传播,则必须将方法与supplyAsync(...)一起使用,如先前所示

但是它说parallelStream不能“开箱即用”。因此,有没有解决方法可以使用parallelStream?

感谢您对此的任何帮助或评论

1 个答案:

答案 0 :(得分:1)

您能做的最接近的是称呼一个可以实现的未来

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"));