首页  

arthas watch 使用     所属分类 arthas 浏览量 2682
https://alibaba.github.io/arthas/watch  

                                                                                   


watch -h

 USAGE:                                                                                                                             
   watch [-b] [-e] [-x $value] [-f] [-h] [-n $value] [-E] [-M $value] [-s] class-pattern method-pattern express [condition-expre 
 ss]                                                                                                                                
                                                                                                                                    
 SUMMARY:                                                                                                                           
   Display the input/output parameter, return object, and thrown exception of specified method invocation                           
   The express may be one of the following expression (evaluated dynamically):                                                      
           target : the object                                                                                                      
            clazz : the object's class                                                                                              
           method : the constructor or method                                                                                       
           params : the parameters array of method                                                                                  
     params[0..n] : the element of parameters array                                                                                 
        returnObj : the returned object of method                                                                                   
         throwExp : the throw exception of method                                                                                   
         isReturn : the method ended by return                                                                                      
          isThrow : the method ended by throwing exception                                                                          
            #cost : the execution time in ms of method invocation                                                                   
 Examples:                                                                                                                          
   watch -b org.apache.commons.lang.StringUtils isBlank params                                                                      
   watch -f org.apache.commons.lang.StringUtils isBlank returnObj                                                                   
   watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2                                             
   watch -bf *StringUtils isBlank params                                                                                            
   watch *StringUtils isBlank params[0]                                                                                             
   watch *StringUtils isBlank params[0] params[0].length==1                                                                         
   watch *StringUtils isBlank params '#cost>100'                                                                                    
   watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0]                                                            
                                                                                                                                    
 WIKI:                                                                                                                              
   https://alibaba.github.io/arthas/watch                                                                                           
                                                                                                                                    
 OPTIONS:                                                                                                                           
 -b, --before                                Watch before invocation                                                                
 -e, --exception                             Watch after throw exception                                                            
 -x, --expand                          Expand level of object (1 by default)                                                  
 -f, --finish                                Watch after invocation, enable by default                                              
 -h, --help                                  this help                                                                              
 -n, --limits                          Threshold of execution times   最大执行次数
                                                         
 -E, --regex                                 Enable regular expression to match (wildcard matching by default)                      
 -M, --sizeLimit                     Upper size limit in bytes for the result (10 * 1024 * 1024 by default)                 
 -s, --success                               Watch after successful invocation                                                      
  class-pattern                             The full qualified class name you want to watch                                        
  method-pattern                            The method name you want to watch                                                      
  express                                 the content you want to watch, written by ognl.                                        
                                             Examples:                                                                              
                                               params                                                                               
                                               params[0]                                                                            
                                               'params[0]+params[1]'                                                                
                                               '{params[0], target, returnObj}'                                                     
                                               returnObj                                                                            
                                               throwExp                                                                             
                                               target                                                                               
                                               clazz                                                                                
                                               method                                                                               
                                                                                                                                    
 condition-express                         Conditional expression in ognl style, for example:                                     
                                               TRUE  : 1==1                                                                         
                                               TRUE  : true                                                                         
                                               FALSE : false                                                                        
                                               TRUE  : 'params.length>=0'                                                           
                                               FALSE : 1==2                                                                         
                                                              
                                                              
                                                              
OGNL  Object Graph Navigation Language


方法执行数据观测
观察指定方法的调用情况。 返回值、抛出异常、入参,通过编写 OGNL 表达式进行相应变量的查看。
 
watch 命令定义了4个观察事件点,即 -b 方法调用前,-e 方法异常后,-s 方法返回后,-f 方法结束后
4个观察事件点 -b、-e、-s 默认关闭,-f 默认打开,当指定观察点被打开后,在相应事件点会对观察表达式进行求值并输出

 
watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2

-e 抛出异常时才触发

watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2


#cost>200(单位ms) 耗时大于200ms时才会输出 

条件表达式
'params[0]>10'

watch a.b.c hello '{params[0],params[0].getDimKey(params[1],false), returnObj}' 'params[2]!=null && params[2].toString().equals("-1")' -x 3


watch dyyx.util.WebUtil run '{params[1],throwExp}'  'throwExp.getMessage().equals("tiger")' -e -x 7

http://127.0.0.1:8080/api.jsp?error=tiger&msg=hello


watch demo.MathGame primeFactors 'target'

target 当前对象 查看当前对象中的属性 

watch demo.MathGame primeFactors 'target.illegalArgumentCount'


-x 参数说明
-x, --expand                          Expand level of object (1 by default)  

public static void run(HttpServletRequest request, Map< String, Object> context) throws Exception {
http://127.0.0.1:8080/api.jsp?error=&msg=hello


watch dyyx.util.WebUtil run '{params[1],throwExp}' 

ts=2020-01-03 12:05:08; [cost=1.150441ms] result=@ArrayList[
    @TreeMap[isEmpty=false;size=2],
    null,
]



-x 2  可看到map里具体的值

watch dyyx.util.WebUtil run '{params[1],throwExp}' -x 2

ts=2020-01-03 12:05:16; [cost=0.66692ms] result=@ArrayList[
    @TreeMap[
        @String[result]:@String[hello],
        @String[type]:@String[echo],
    ],
    null,
]


异常观察
http://127.0.0.1:8080/api.jsp?error=error&msg=hello
watch dyyx.util.WebUtil run '{params[1],throwExp}' -x 2

ts=2020-01-03 12:06:31; [cost=0.201315ms] result=@ArrayList[
    @TreeMap[
        @String[type]:@String[echo],
    ],
    java.lang.RuntimeException: error
	at dyyx.util.WebUtil.run_echo(WebUtil.java:343)
	at dyyx.util.WebUtil.run(WebUtil.java:297)
	at org.apache.jsp.api_jsp._jspService(api_jsp.java:51)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:684)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:428)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
	at org.eclipse.jetty.server.Server.handle(Server.java:370)
	at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494)
	at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:971)
	at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:1033)
	at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:644)
	at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
	at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:667)
	at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
	at java.lang.Thread.run(Thread.java:745)
,
]



watch  dyyx.HelloBean   sayHello '{target,params,throwExp}'  -e

watch  dyyx.HelloBean   sayHello '{target,target.toString(),params,throwExp}'  -e -x 2

watch  dyyx.HelloBean   sayHello '{target,target.toString(),params,throwExp}'  -e -x 2 -n 9

http://127.0.0.1:8080/api.jsp?type=hello2&name=tiger
watch  dyyx.HelloBean   sayHello2 '{target,params,throwExp}' -x 3

ts=2020-06-23 09:16:11; [cost=2.185813ms] result=@ArrayList[
    @HelloBean[
        name=@String[tiger],
        seq=@AtomicLong[
            serialVersionUID=@Long[1927816293512124184],
            unsafe=@Unsafe[sun.misc.Unsafe@13929bda],
            valueOffset=@Long[16],
            VM_SUPPORTS_LONG_CAS=@Boolean[true],
            value=@Long[8],
        ],
        VERSION=@String[20200623 0909],
        now=@Date[2020-06-23 09:16:11,714],
        nowtimestr=@String[2020-06-23T09:16:11.714],
        nowtime=@Long[1592874971715],
        list=@ArrayList[
            @String[a],
            @String[b],
        ],
        map=@HashMap[
            @String[a]:@String[1],
            @String[b]:@Long[2],
            @String[c]:@Double[3.3],
            @String[d]:@Date[2020-06-23 09:16:11,715],
        ],
    ],
    @Object[][
        @HashMap[
            @String[nowstr]:@String[2020-06-23T09:16:11.714],
            @String[now]:@Date[2020-06-23 09:16:11,714],
            @String[nowtime]:@Long[1592874971714],
        ],
    ],
    null,
]



私有的 静态的变量 都会输出


public class HelloBean {	
	private final String name;	
	private static final AtomicLong seq = new AtomicLong(0);
	private static final String VERSION = "20200623 0909";
		
	private Date now = new Date();

	private String nowtimestr = LocalDateTime.now().toString();
	
	private long nowtime = System.currentTimeMillis();
	
	private List<String> list = new ArrayList<String>();
	private Map<String,Object> map = new HashMap<String,Object>();
	
	


now=@Date[2020-06-23 09:16:11,714],

变量名=@类型[变量值]

watch  dyyx.HelloBean   sayHello2 '{target.VERSION,target.now,params,throwExp}' -x 3
watch failed, condition is: null, express is: {target.VERSION,target.now,params,throwExp}, ognl.OgnlException: VERSION [java.lang.NoSuchFieldException: VERSION], visit /Users/dugang/logs/arthas/arthas.log for more details.

watch  dyyx.HelloBean   sayHello2 '{target.now,params,throwExp}' -x 3

target.VERSION 会报错  静态变量
target.now  可以


Arthas的一些特殊用法文档说明 https://github.com/alibaba/arthas/issues/71 params[0].size() params[0].{ #this.name } 只获取列表对象里的name字段 按条件过滤 params[0].{? #this.name == null } 过滤后统计 params[0].{? #this.age > 10 }.size()

上一篇     下一篇
zookeeper使用场景

arthas使用

编程心法

arthas tt 使用

arthas OGNL 使用

zookeeper内部原理