Skip to content

Arthas线上诊断

简介

Arthas(阿尔萨斯) 是一款线上监控诊断产品,通过全局视角实时查看应用 load、内存、gc、线程的状态信息,并能在不修改应用代码的情况下,对业务问题进行诊断,包括查看方法调用的出入参、异常,监测方法执行耗时,类加载信息等,大大提升线上问题排查效率。

官方文档 https://arthas.aliyun.com/

安装

bash
curl -L https://arthas.aliyun.com/install.sh | sh
./as.sh
bash
curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

提示

如果提示telnet is not installed.没有安装,请使用java -jar arthas-boot.jar这种方式

使用

选择JVM进程

bash
root@abb1a712c8cd:/app# java -jar arthas-boot.jar
[INFO] JAVA_HOME: /usr/lib/jvm/java-8-openjdk-amd64/jre
[INFO] arthas-boot version: 3.6.7
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 7 gponline-web.jar
1
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.6.7?mirror=aliyun
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.6.7/arthas
[INFO] Try to attach process 7
Picked up JAVA_TOOL_OPTIONS: 
[INFO] Attach process 7 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                           
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                          
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                          
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                         
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                          

wiki       https://arthas.aliyun.com/doc                                        
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html                  
version    3.6.7                                                                
main_class                                                                      
pid        7                                                                    
time       2023-04-07 20:32:49                                                  

[arthas@7]$

查看帮助

bash
[arthas@7]$ help
 NAME         DESCRIPTION                                                                                                                                                                                                                                                                                                                                        
 help         Display Arthas Help                                                                                                                                                                                                                                                                                                                                
 auth         Authenticates the current session                                                                                                                                                                                                                                                                                                                  
 keymap       Display all the available keymap for the specified connection.                                                                                                                                                                                                                                                                                     
 sc           Search all the classes loaded by JVM                                                                                                                                                                                                                                                                                                               
 sm           Search the method of classes loaded by JVM                                                                                                                                                                                                                                                                                                         
 classloader  Show classloader info                                                                                                                                                                                                                                                                                                                              
 jad          Decompile class                                                                                                                                                                                                                                                                                                                                    
 getstatic    Show the static field of a class                                                                                                                                                                                                                                                                                                                   
 monitor      Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc.                                                                                                                                                                                                                                                 
 stack        Display the stack trace for the specified class and method                                                                                                                                                                                                                                                                                         
 thread       Display thread info, thread stack                                                                                                                                                                                                                                                                                                                  
 trace        Trace the execution time of specified method invocation.                                                                                                                                                                                                                                                                                           
 watch        Display the input/output parameter, return object, and thrown exception of specified method invocation                                                                                                                                                                                                                                             
 tt           Time Tunnel                                                                                                                                                                                                                                                                                                                                        
 jvm          Display the target JVM information                                                                                                                                                                                                                                                                                                                 
 memory       Display jvm memory info.                                                                                                                                                                                                                                                                                                                           
 perfcounter  Display the perf counter information.                                                                                                                                                                                                                                                                                                              
 ognl         Execute ognl expression.                                                                                                                                                                                                                                                                                                                           
 mc           Memory compiler, compiles java files into bytecode and class files in memory.                                                                                                                                                                                                                                                                      
 redefine     Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)                                                                                                                                                                                                                                                                         
 retransform  Retransform classes. @see Instrumentation#retransformClasses(Class...)                                                                                                                                                                                                                                                                             
 dashboard    Overview of target jvm's thread, memory, gc, vm, tomcat info.                                                                                                                                                                                                                                                                                      
 dump         Dump class byte array from JVM                                                                                                                                                                                                                                                                                                                     
 heapdump     Heap dump                                                                                                                                                                                                                                                                                                                                          
 options      View and change various Arthas options                                                                                                                                                                                                                                                                                                             
 cls          Clear the screen                                                                                                                                                                                                                                                                                                                                   
 reset        Reset all the enhanced classes                                                                                                                                                                                                                                                                                                                     
 version      Display Arthas version                                                                                                                                                                                                                                                                                                                             
 session      Display current session information                                                                                                                                                                                                                                                                                                                
 sysprop      Display and change the system properties.                                                                                                                                                                                                                                                                                                          
 sysenv       Display the system env.                                                                                                                                                                                                                                                                                                                            
 vmoption     Display, and update the vm diagnostic options.                                                                                                                                                                                                                                                                                                     
 logger       Print logger info, and update the logger level                                                                                                                                                                                                                                                                                                     
 history      Display command history                                                                                                                                                                                                                                                                                                                            
 cat          Concatenate and print files                                                                                                                                                                                                                                                                                                                        
 base64       Encode and decode using Base64 representation                                                                                                                                                                                                                                                                                                      
 echo         write arguments to the standard output                                                                                                                                                                                                                                                                                                             
 pwd          Return working directory name                                                                                                                                                                                                                                                                                                                      
 mbean        Display the mbean information                                                                                                                                                                                                                                                                                                                      
 grep         grep command for pipes.                                                                                                                                                                                                                                                                                                                            
 tee          tee command for pipes.                                                                                                                                                                                                                                                                                                                             
 profiler     Async Profiler. https://github.com/jvm-profiling-tools/async-profiler                                                                                                                                                                                                                                                                              
 vmtool       jvm tool                                                                                                                                                                                                                                                                                                                                           
 stop         Stop/Shutdown Arthas server and exit the console.                                                                                                                                                                                                                                                                                                  
[arthas@7]$

sm 查找类中的方法

bash
sm *UserController info
bash
[arthas@7]$ sm *UserController info
com.gponline.controller.auth.UserController info(Ljavax/servlet/http/HttpServletRequest;)Lcom/gponline/common/response/Response;
Affect(row-cnt:1) cost in 73 ms.

trace 追踪方法执行

bash
trace com.gponline.controller.auth.UserController info
bash
[arthas@7]$ trace com.gponline.controller.auth.UserController info 
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 332 ms, listenerId: 1
`---ts=2023-04-07 20:37:15;thread_name=http-nio-8080-exec-11;id=16e;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@23b087c3
    `---[5.826707ms] com.gponline.controller.auth.UserController:info()
        +---[0.64% 0.037154ms ] javax.servlet.http.HttpServletRequest:getParameter() #240
        +---[63.54% 3.702035ms ] com.gponline.service.user.interfaces.feign.api.user.UserInfoFeignApi:getActiveUser() #241
        +---[0.49% 0.028364ms ] com.gponline.common.model.DTO.GpUserInfoDTO:getUserUniqueCode() #245
        +---[11.57% 0.674174ms ] org.slf4j.Logger:info() #246
        +---[0.24% 0.013797ms ] org.springframework.data.redis.core.StringRedisTemplate:opsForValue() #247
        +---[6.95% 0.404684ms ] org.springframework.data.redis.core.ValueOperations:get() #247
        +---[0.18% 0.01042ms ] com.gponline.common.utils.StringUtils:isNotEmpty() #250
        +---[6.37% 0.371343ms ] com.alibaba.fastjson.JSONObject:parseObject() #251
        `---[0.23% 0.013392ms ] com.gponline.common.response.Response:buildOkRes() #257

watch 观察参数返回

bash
watch  com.gponline.controller.auth.UserController info
bash
[arthas@7]$ watch  com.gponline.controller.auth.UserController info 
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 161 ms, listenerId: 2
method=com.gponline.controller.auth.UserController.info location=AtExit
ts=2023-04-07 20:40:30; [cost=5.162129ms] result=@ArrayList[
    @Object[][isEmpty=false;size=1],
    @UserController[com.gponline.controller.auth.UserController@40344a4b],
    @Response[Response{code=0, msg='ok', data=UserOutDTO(uniqueId=d7cfa8839d1b4c4b9e9b6fdce1567389, gpId=null, name=, nickName=null, headImg=, phone=null, bindWechat=false, bindQQ=true, hasPwd=false, email=, bindEmail=false, accountDto=UserAccountOutDTO(id=null, userUniqueCode=null, amount=null, totalIncome=null, totalOutcome=null), couponNumber=null, inviteAuth=0, weChatInfo=null, isVip=0)', currentTime=Fri Apr 07 20:40:30 CST 2023}],
]

monitor 观察统计

bash
monitor com.gponline.controller.auth.UserController info
bash
[arthas@7]$ monitor com.gponline.controller.auth.UserController info 
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 146 ms, listenerId: 3
 timestamp                                             class                                                                             method                                                                           total                      success                     fail                       avg-rt(ms)                 fail-rate                 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 2023-04-07 20:53:22                                   com.gponline.controller.auth.UserController                                       info                                                                             23                         23                          0                          3.95                       0.00%

示例

arthas

stack 输出当前方法被调用的调用路径

输出当前方法被调用的调用路径,看看这个方法在哪些地方被调用了

bash
stack org.apache.http.impl.conn.PoolingHttpClientConnectionManager requestConnection
bash
[arthas@7]$ stack org.apache.http.impl.conn.PoolingHttpClientConnectionManager requestConnection
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 252 ms, listenerId: 2
ts=2024-07-10 15:06:53;thread_name=scheduling-1;id=612;is_daemon=false;priority=5;TCCL=org.springframework.boot.loader.LaunchedURLClassLoader@5ea7bc4
    @org.apache.http.impl.conn.PoolingHttpClientConnectionManager.requestConnection()
        at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:176)
        at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
        at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
        at me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor.execute(ApacheSimplePostRequestExecutor.java:42)
        at me.chanjar.weixin.common.util.http.apache.ApacheSimplePostRequestExecutor.execute(ApacheSimplePostRequestExecutor.java:23)
        at me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl.executeInternal(BaseWxCpServiceImpl.java:348)
        at me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl.execute(BaseWxCpServiceImpl.java:295)
        at me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl.post(BaseWxCpServiceImpl.java:258)
        at me.chanjar.weixin.cp.api.impl.WxCpExternalContactServiceImpl.transferResult(WxCpExternalContactServiceImpl.java:284)
        at com.gupaoedu.ke.service.impl.CpSysExternalContactPoolServiceImpl.callTransferResult(CpSysExternalContactPoolServiceImpl.java:183)
        at com.gupaoedu.ke.service.impl.CpSysExternalContactPoolServiceImpl.callTransferResult(CpSysExternalContactPoolServiceImpl.java:213)
        at com.gupaoedu.ke.service.impl.CpSysExternalContactPoolServiceImpl.callTransferResult(CpSysExternalContactPoolServiceImpl.java:213)
        at com.gupaoedu.ke.service.impl.CpSysExternalContactPoolServiceImpl.callTransferResult(CpSysExternalContactPoolServiceImpl.java:213)

示例

image-20240710150732223

人生感悟