簡體   English   中英

在 Spring Cloud Sleuth 中跨 CompletableFuture 保持跟蹤/跨度

[英]Keep trace/span across CompletableFuture in Spring Cloud Sleuth

我將 Sleuth 與CompletableFuture.handle一起使用。 例子:

log.info("first");
apnsClient.sendNotification(...).handle((response, cause) -> {
     log.info("second");
});

我希望second日志與first日志具有相同的跟蹤 ID。 但是,事實並非如此。 因此,我想知道該怎么辦? 謝謝!

PS我無法控制apnsClient.sendNotification如何管理線程(因為它來自Pushy ),所以沒有辦法使用像LazyTraceExecutor這樣的東西。

您可以做的是在log.info("first") ) 之前檢索跨度(例如通過tracer.currentSpan() ),使用log.info("second")將跨度傳遞給 lambda 並通過跟蹤器手動繼續跟蹤tracer.withSpanInScope(span) 它會是這樣的:

Span span = tracer.currentSpan();
log.info("first");
apnsClient.sendNotification(...).handle((response, cause) -> {
     try (SpanInScope ws = tracer.withSpanInScope(span)) {
         log.info("second");
     }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM