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

Sleuth traceId and spanId are not propagated to parallelStream worker threads

我已将 Sleuth 集成到我的 Spring Boot 项目中,以便具有更好的可追溯性。它完美地记录了 traceId 和 spanId。但是,这些字段不会添加到使用 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

但是它说 parallelStream 不能“开箱即用”。那么,是否有使用 parallelStream 的解决方法?

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

最接近的是调用一个可完成的未来

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