Java诊断工具Arthas

1. Arthas简介

Arthas是阿里开源的一个线上java诊断工具,发现阿里还是挺喜欢开源一些技术的,造福人类。昨天试用了一下,发现真是强大,解决了我工作两年的很多困扰,有点相见恨晚的感觉。

根据官网的说明,它能解决下面的问题:

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

  1. 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?

  2. 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?

  3. 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?

  4. 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!

  5. 是否有一个全局视角来查看系统的运行状况?

  6. 有什么办法可以监控到JVM的实时运行状态?

Arthas采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

2. Arthas安装

在Linux下直接执行curl -L https://alibaba.github.io/arthas/install.sh | sh,然后执行./as.sh就可以进入到Arthas的命令行界面。

3. Arthas试用

安装完后试用了它的监控相关的功能,以后调试就不那么麻烦了。

# 进入命令行界面,刚进入的时候会显示所有的java进程,选择对应的数字然后回车就可以进入到对应的进程中
./as.sh
Arthas script version: 3.0.4.1
Found existing java process, please choose one and hit RETURN.
  [1]: 27073 org.apache.rocketmq.namesrv.NamesrvStartup
* [2]: 2379 org.apache.catalina.startup.Bootstrap
2
Calculating attach execution time...
Attaching to 2379 using version 3.0.5...

real    0m0.249s
user    0m0.208s
sys    0m0.025s
Attach success.
telnet connecting to arthas server... current timestamp is 1545738424
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          


wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 2379
timestamp: 1545738424462

$

3.1 monitor命令

monitor可以对某个类的某个方法进行周期性监控,来输出方法的平均响应时间,成功失败次数,调用次数等。

# 每3秒统计queryCatalog方法的调用次数,成功失败次数和平均响应时间等。
$ monitor -c 3 com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 82 ms.


 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:41  com.timeline.services.CatalogService  queryCatalog  3      3        0     2.46        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:44  com.timeline.services.CatalogService  queryCatalog  0      0        0     0.00        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:47  com.timeline.services.CatalogService  queryCatalog  0      0        0     0.00        0.00%                                                                          

 timestamp            class                                 method        total  success  fail  avg-rt(ms)  fail-rate                                                                      
----------------------------------------------------------------------------------------------------------------------                                                                     
 2018-12-25 19:57:50  com.timeline.services.CatalogService  queryCatalog  2      2        0     2.08        0.00%

具体使用请见:https://alibaba.github.io/arthas/monitor.html。

3.2 watch命令

watch可以监控某个方法的出入参,异常信息等。

# 观察方法的出入参
# -x表示遍历深度,可以调整来打印具体的参数和结果内容,默认值是1。
$watch com.timeline.services.CatalogService queryCatalog "{params,returnObj}" -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 73 ms.
ts=2018-12-25 19:49:55;result=@ArrayList[
    @Object[][isEmpty=true;size=0],
    @ArrayList[
        @CatalogVO[CatalogVO(id=59, name=wangjun, location=beijing, time=1537256454000, description=desccc)],
    ],
]
# 可以看到入参是空的,出参是一个list

具体使用请见:https://alibaba.github.io/arthas/watch.html。

3.3 trace命令

trace可以输出方法内部调用关系,并打印方法内部每个调用的耗时。

# 打印queryCatalog方法内每个调用的耗时
$ trace com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 101 ms.
`---ts=2018-12-25 20:00:25;thread_name=http-nio-8080-exec-8;id=1b;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.ParallelWebappClassLoader@4d1110a3
    `---[2.896979ms] com.timeline.services.CatalogService:queryCatalog()
        +---[2.605774ms] com.timeline.dao.CatalogDao:queryCatalog()
        +---[0.0048ms] java.util.ArrayList:<init>()
        +---[min=0.001154ms,max=0.00634ms,total=0.007494ms,count=2] java.util.List:size()
        +---[0.004428ms] java.util.List:get()
        +---[0.005115ms] com.timeline.entity.Catalog:getId()
        +---[0.003107ms] com.timeline.entity.Catalog:getName()
        +---[0.0026ms] com.timeline.entity.Catalog:getLocation()
        +---[0.002573ms] com.timeline.entity.Catalog:getTime()
        +---[0.005901ms] com.timeline.common.Utils:timestamp2Long()
        +---[0.002543ms] com.timeline.entity.Catalog:getDescription()
        +---[0.005133ms] com.timeline.vo.CatalogVO:<init>()
        `---[0.004174ms] java.util.List:add()

具体使用请见:https://alibaba.github.io/arthas/trace.html。

3.4 stack命令

stack可以输出方法的整个调用路径。

# 打印queryCatalog方法的整个调用路径(不抛出堆栈也能看到调用链啦!)
$ stack com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 82 ms.
ts=2018-12-25 20:02:56;thread_name=http-nio-8080-exec-9;id=1c;is_daemon=true;priority=5;TCCL=org.apache.catalina.loader.ParallelWebappClassLoader@4d1110a3
    @com.timeline.controllers.CalalogController.queryCatalog()
        at sun.reflect.GeneratedMethodAccessor46.invoke(null:-1)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901)

具体使用请见:https://alibaba.github.io/arthas/stack.html。

3.5 tt命令

记录方法每次的调用出入参和异常信息,并且完整保留调用现场,可以再次模拟调用。

# 记录每次调用queryCatalog的情况,可以根据其他命令打印出入餐和复现现场
$ tt -t com.timeline.services.CatalogService queryCatalog
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 128 ms.


 INDEX       TIMESTAMP                    COST(ms)      IS-RET      IS-EXP     OBJECT                CLASS                                       METHOD                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1008        2018-12-25 20:04:46          4.747987      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1009        2018-12-25 20:04:47          2.270835      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1010        2018-12-25 20:04:47          1.434482      true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1011        2018-12-25 20:04:48          1.82891       true        false      0x2577a8a4            CatalogService                              queryCatalog                              
 1012        2018-12-25 20:04:49          2.27361       true        false      0x2577a8a4            CatalogService                              queryCatalog

具体使用请见:https://alibaba.github.io/arthas/tt.html。

当然除此之外还有很丰富的其他功能,比如JVM,ClassLoader相关的命令。

Last updated