motan
motan copied to clipboard
filter-opentracing 集成 zipkin,zipkin-ui 无法体现依赖关系
目标
监控 motan rpc 的调用链路
我的思路
由于 motan 提供了 com.weibo:filter-opentracing 模块,只需要提供一个满足 opentracing 规范的 io.opentracing.Tracer 实现即可。于是尝试集成 zipkin,使用 opentracing 提供的 brave.opentracing.BraveTracer (本质是 zipkin 的 java 客户端 brave 向 opentracing 规范做的一个桥接,github 地址:brave-opentracing)
项目配置
启动两个服务:
student-service,teacher-service 有后者向前者发起 RPC 请求,在 zipkin 中记录链路信息。
依赖
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-api</artifactId>
<version>0.31.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.opentracing.brave/brave-opentracing -->
<dependency>
<groupId>io.opentracing.brave</groupId>
<artifactId>brave-opentracing</artifactId>
<version>0.25.0</version>
</dependency>
<dependency>
<groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.zipkin.brave/brave -->
<dependency>
<groupId>io.zipkin.brave</groupId>
<artifactId>brave</artifactId>
<version>4.13.3</version>
</dependency>
motan 配置:
@Bean
OpenTracingContext openTracingContext() {
OpenTracingContext openTracingContext = new OpenTracingContext();
openTracingContext.setTracerFactory(() -> braveTracer());
return openTracingContext;
}
BraveTracer braveTracer() {
Sender sender = OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
AsyncReporter spanReporter = AsyncReporter.create(sender);
Tracing braveTracing = Tracing.newBuilder()
.localServiceName("teacher-service")//student-service 同样配置
.spanReporter(spanReporter)
.sampler(Sampler.ALWAYS_SAMPLE)
.build();
BraveTracer braveTracer = BraveTracer.create(braveTracing);
return braveTracer;
}
设置全局过滤器
BasicServiceConfigBean::setFilter("opentracing");
实际结果
但是在 zipkin-ui 中无法体现依赖关系。预期出现 teacher-service ->student-service 实际没有任何数据
这问题是否是和 OpenTracingFilter 的实现相关,还是我的配置有问题?
希望能得到解答。
OpenTracingFilter的逻辑是:
在client端发起调用时把当前span(如果没有span时新建) inject到motan的attachment中。
在server端处理请求时,把attachment中的trace信息extract出来。这时会建立新的span。
所以一次远程调用会新建2个span。
你看span的name,teacherapi和studentapi各两个span这个没什么问题。
如果想了解rpc中span的传递流程,可以debug OpenTracingFilter的filter方法
teacherapi和studentapi各两个span?但是我的图里面,不应该是3个teacherapi的span,1个studentapi的span吗? @rayzhang0603
spanname是有group+interface+method组成的, saveteacher和savestudent这两个方法不是各有两个span吗?
表述有误,抱歉... 我是好奇,第三个 span(saveStudent) 为什么属于 teacher-service?
因为调用student的行为是在teacher service发起的,所以这个span是在teacher service侧创建的。 可以debug断点一下BraveSpan的构造方法和finish方法,这样会比较容易看清span的创建和结束过程。
@rayzhang0603 方便了解下新浪现在是用的哪种方式做的链路追踪么。
@hepyu 基于zipkin实现的
@rayzhang0603 brave5.0版本跟踪数据json结构有变化 后续motan是否考虑作对应的升级 例如在OpenTracingFilter-filter中添加对应的kind等数据属性 我们使用brave和zipkin收集数据后 如果链路比较深 也就是span层级比较多 是比较难区分client和server的 client发起一次远程server调用就会记录两个span
@aqiangsoft 后续版本会对OT的api进行升级,并支持kind等属性