Arthas实践--获取分布式追踪的traceId

Arthas实践--获取分布式追踪的traceId_第1张图片

Arthas

Arthas是Alibaba开源的Java诊断工具,深受开发者喜爱。

  • Github:https://github.com/alibaba/arthas

  • 文档:https://alibaba.github.io/arthas

现在越来越多公司使用了分布式跟踪系统,这些系统通常会为每一个请求分配一个 traceid,一直透传到后端的各个组件里。

在使用Arthas时,为了准确抓到调用,有时需要准确判断 traceId;或者想把traceId打印出来,到系统里查询。

那么在Arthas里,怎么获取traceId呢?下面以阿里巴巴内部的分布式跟踪系统EagleEye 为例来展示,其它的分布式跟踪系统系统也是类似的。

EagleEye 获取traceId的API

其实很简单,比如应用本身在代码里获取traceId的话,是调用 EagleEye.getTraceId()

那么在watch时,使用ognl表达式来获取即可。

watch

可以直接调用static函数来获取traceId,比如:

@com.taobao.eagleeye.EagleEye@getTraceId()

[arthas@39996]$ watch com.test.pandora.eagleeye.HomeController home '@com.taobao.eagleeye.EagleEye@getTraceId()'
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 47 ms, listenerId: 4
ts=2020-06-05 17:05:36; [cost=0.752721ms] result=@String[1ee1149c15913479360831011d9c3c]

或者:

[arthas@39996]$ watch com.test.pandora.eagleeye.HomeController home '{params, @com.taobao.eagleeye.EagleEye@getTraceId()}'
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 49 ms, listenerId: 5
ts=2020-06-05 17:08:30; [cost=1.058038ms] result=@ArrayList[
    @Object[][isEmpty=true;size=0],
    @String[1ee1149c15913481104721013d9c3c],
]

trace

trace 命令会自动打印 eagleeye的traceId,比如:

[arthas@39996]$ trace com.test.pandora.eagleeye.HomeController home
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 70 ms, listenerId: 2
`---ts=2020-06-05 17:03:40;thread_name=http-nio-7001-exec-7;id=77;is_daemon=true;priority=5;TCCL=org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedWebappClassLoader@5929021;trace_id=1ee1149c15913478208291007d9c3c;rpc_id=0
    `---[1.215536ms] com.test.pandora.eagleeye.HomeController:home()
        `---[0.882264ms] org.slf4j.Logger:info() #20

总结

  • 其它的分布式跟踪软件的traceId,或者ThreadLocal里的值也可以用类似的方式获取到

  • ognl表达式的一些技巧参考:https://github.com/alibaba/arthas/issues/71

你可能感兴趣的:(Arthas实践--获取分布式追踪的traceId)