问题描述
我将 Sleuth 与 CompletableFuture.handle
一起使用。示例:
log.info("first");
apnsClient.sendNotification(...).handle((response,cause) -> {
log.info("second");
});
我希望 second
日志与 first
日志具有相同的跟踪 ID。然而,事实并非如此。所以我想知道该怎么办?谢谢!
附言我无法控制 apnsClient.sendNotification
如何管理线程(因为它来自 Pushy),因此无法使用 LazyTraceExecutor 之类的东西。
解决方法
您可以做的是在tracer.currentSpan()
之前检索跨度(例如通过log.info("first")
),使用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");
}
});