dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Observability tasks: trace delivery across threads(跨线程时的trace数据传递)

Open songxiaosheng opened this issue 2 years ago • 5 comments

  • [ ] I have searched the issues of this repository and believe that this is not a duplicate.

Describe the proposal

En:Generally, in Java, traceid and spanid are passed through the thread context, but in the case of multi-threading, there will be multiple thread contexts. At this time, the chain will be lost when the same request reaches the child thread. At this time, we need to copy the link data from one thread to the context of another thread. If you don’t know how to copy, you can consider how to share data between threads, such as using shared variables such as member variables.

中文:一般在Java中traceid和spanid我们都是通过线程上下文来传递的但是出现了多线程的情况下就会有多个线程上下文的情况,这个时候就会导致同一个请求中到了子线程时候丢失了链路追踪数据,这个时候就需要我们将链路数据从一个线程拷贝到另外一个线程的上下文中,如果不知道如何拷贝可以考虑下线程间是如何共享数据的比如使用成员变量这种共享变量

songxiaosheng avatar Dec 20 '22 06:12 songxiaosheng

如何参与贡献?

ShenFeng312 avatar Dec 20 '22 08:12 ShenFeng312

如何参与贡献?

可以直接认领任务,后续我们一起沟通,会把任务指派给你

songxiaosheng avatar Dec 20 '22 08:12 songxiaosheng

如何参与贡献?

可以直接认领任务,后续我们一起沟通,会把任务指派给你

我希望参与和trace 这块相关的内容。如果可以的话 希望能分配给我 thanks

ShenFeng312 avatar Dec 20 '22 08:12 ShenFeng312

如何参与贡献?

可以留下钉钉号吗,方便我们联系

conghuhu avatar Dec 22 '22 12:12 conghuhu

如何参与贡献?

可以留下钉钉号吗,方便我们联系

shenfeng970312

ShenFeng312 avatar Dec 30 '22 02:12 ShenFeng312

dubbo 异步调用采用org.apache.dubbo.rpc.protocol.dubbo.FutureAdapter 包装了CompletableFuture 该类在dubbo-rpc-api 模块下,且相关代码都在该模块下。如果要封装异步trace信息会强制依赖到相关组件,或者加SPI。这样做成本大收益小。所以我认为不应该默认实现,把是否要携带trace的选择交给用户。可以让用户用已下方式实现:

public class ConsumerAsync {

    @DubboReference
    private DemoService demoService;

    @Resource
    private ObservationRegistry observationRegistry;

    public String doSayHello(String name) throws ExecutionException, InterruptedException {
        CompletableFuture<String> stringCompletableFuture = demoService.sayHelloAsync(name);
        Observation currentObservation = observationRegistry.getCurrentObservation();
        stringCompletableFuture.whenComplete((s, throwable) -> Observation.tryScoped(currentObservation,
            ()-> System.out.println(s)));
        return stringCompletableFuture.get();
    }
}

ShenFeng312 avatar Feb 15 '23 08:02 ShenFeng312

可以的 到时候我们可以把这一部分增加到官方文档提供一种解决方案

songxiaosheng avatar Feb 15 '23 23:02 songxiaosheng