watch

方法执行数据观测

让你能方便的观察到指定方法的调用情况。能观察到的范围为:返回值抛出异常入参,通过编写 OGNL 表达式进行对应变量的查看。

参数说明

watch 的参数比较多,主要是因为它能在 4 个不同的场景观察对象

参数名称 参数说明
class-pattern 类名表达式匹配
method-pattern 方法名表达式匹配
express 观察表达式
condition-express 条件表达式
[b] 方法调用之前观察
[e] 方法异常之后观察
[s] 方法返回之后观察
[f] 方法结束之后(正常返回和异常返回)观察
[E] 开启正则表达式匹配,默认为通配符匹配
[x:] 指定输出结果的属性遍历深度,默认为 1

这里重点要说明的是观察表达式,观察表达式的构成主要由 ognl 表达式组成,所以你可以这样写"{params,returnObj}",只要是一个合法的 ognl 表达式,都能被正常支持。

观察的维度也比较多,主要体现在参数 advice 的数据结构上。Advice 参数最主要是封装了通知节点的所有信息。请参考表达式核心变量中关于该节点的描述。

  • 特殊用法请参考:https://github.com/alibaba/arthas/issues/71
  • OGNL表达式官网:https://commons.apache.org/proper/commons-ognl/language-guide.html
    特别说明

  • watch 命令定义了4个观察事件点,即 -b 方法调用前,-e 方法异常后,-s 方法返回后和 -f 方法结束后

  • 4个观察事件点 -b-e-s 默认关闭,-f 默认打开,当指定观察点被打开后,在相应事件点会对观察表达式进行求值并输出
  • 这里要注意方法入参方法出参的区别,有可能在中间被修改导致前后不一致,除了 -b 事件点 params 代表方法入参外,其余事件都代表方法出参
  • 当使用 -b 时,由于观察事件点是在方法调用前,此时返回值或异常均不存在

使用参考

代码示例:

  1. public void execute() {
  2. List<String> list = new ArrayList<String>();
  3. list.add("a");
  4. list.add("b");
  5.  
  6. List<String> list2 = new ArrayList<String>();
  7. list2.add("c");
  8. list2.add("d");
  9.  
  10. int len = add(list, list2);
  11. }
  12.  
  13. private static int add(List<String> list, List<String> list2) {
  14. list.addAll(list2);
  15. return list.size();
  16. }

观察方法出参和返回值

  1. $ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" -x 2
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 44 ms.
  4. @ArrayList[
  5. @Object[][
  6. @ArrayList[isEmpty=false;size=4],
  7. @ArrayList[isEmpty=false;size=2],
  8. ],
  9.  
  10. @Integer[4],
  11. ]

观察方法入参

  1. $ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" -x 2 -b
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 48 ms.
  4. @ArrayList[
  5. @Object[][
  6. @ArrayList[isEmpty=false;size=2],
  7. @ArrayList[isEmpty=false;size=2],
  8. ],
  9.  
  10. null,
  11. ]
对比前一个例子,params[0] 其size为2(入参),返回值为空(事件点为方法执行前,因此获取不到返回值)

同时观察方法调用前和方法返回后

  1. $ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" -x 2 -b -s
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 59 ms.
  4. @ArrayList[
  5. @Object[][
  6. @ArrayList[isEmpty=false;size=2],
  7. @ArrayList[isEmpty=false;size=2],
  8. ],
  9.  
  10. null,
  11. ]
  12. @ArrayList[
  13. @Object[][
  14. @ArrayList[isEmpty=false;size=4],
  15. @ArrayList[isEmpty=false;size=2],
  16. ],
  17.  
  18. @Integer[4],
  19. ]
这里输出结果中,第一次输出的是方法调用前的观察表达式的结果,第二次输出的是方法返回后的表达式的结果
结果的顺序和命令中 -s -b 的顺序没有关系,只与事件本身的先后顺序有关

调整-x的值,观察具体的方法参数值

  1. $ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" -x 3
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 59 ms.
  4. @ArrayList[
  5. @Object[][
  6. @ArrayList[
  7. @String[a],
  8.  
  9. @String[b],
  10.  
  11. @String[c],
  12.  
  13. @String[d],
  14. ],
  15. @ArrayList[
  16. @String[c],
  17.  
  18. @String[d],
  19. ],
  20. ],
  21.  
  22. @Integer[4],
  23. ]
-x表示遍历深度,可以调整来打印具体的参数和结果内容。

条件表达式的例子

  1. $ watch com.alibaba.sample.petstore.biz.impl.UserManagerImpl testAdd "{params, returnObj}" "params[0].equals('aaa')" -x 2
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 29 ms.
  4. @ArrayList[
  5. @Object[][
  6. @String[aaa],
  7. @String[bbb],
  8. ],
  9.  
  10. @Integer[6],
  11. ]
只有满足条件的调用,才会有响应。

观察异常信息的例子

  1. $ watch com.alibaba.sample.petstore.biz.impl.UserManagerImpl testAdd "{params, throwExp}" -e -x 2
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 29 ms.
  4. @ArrayList[
  5. @Object[][
  6. @String[aaa],
  7. @String[bbb],
  8. ],
  9.  
  10. java.lang.NullPointerException
  11. at com.alibaba.sample.petstore.biz.impl.UserManagerImpl.testAdd(UserManagerImpl.java:75)
  12. at com.alibaba.sample.petstore.biz.impl.UserManagerImpl.register(UserManagerImpl.java:60)
  13. at com.alibaba.sample.petstore.web.user.module.action.RegisterAction.doRegister(RegisterAction.java:45)
  14. at com.alibaba.sample.petstore.web.user.module.action.RegisterAction$$FastClassByCGLIB$$ad5428f1.invoke(<generated>)
  15. at net.sf.cglib.reflect.FastMethod.invoke(FastMethod.java:53)
  16. at com.alibaba.citrus.service.moduleloader.impl.adapter.MethodInvoker.invoke(MethodInvoker.java:70)
  17. at com.alibaba.citrus.service.moduleloader.impl.adapter.AbstractModuleEventAdapter.executeAndReturn(AbstractModuleEventAdapter.java:100)
  18. at com.alibaba.citrus.service.moduleloader.impl.adapter.AbstractModuleEventAdapter.execute(AbstractModuleEventAdapter.java:58)
  19. at com.alibaba.citrus.turbine.pipeline.valve.PerformActionValve.invoke(PerformActionValve.java:63)
  20. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  21. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invoke(PipelineImpl.java:210)
  22. at com.alibaba.citrus.service.pipeline.impl.valve.ChooseValve.invoke(ChooseValve.java:98)
  23. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  24. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invoke(PipelineImpl.java:210)
  25. at com.alibaba.citrus.service.pipeline.impl.valve.LoopValve.invokeBody(LoopValve.java:105)
  26. at com.alibaba.citrus.service.pipeline.impl.valve.LoopValve.invoke(LoopValve.java:83)
  27. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  28. at com.alibaba.citrus.turbine.pipeline.valve.PageAuthorizationValve.invoke(PageAuthorizationValve.java:105)
  29. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  30. at com.alibaba.citrus.turbine.pipeline.valve.CheckCsrfTokenValve.invoke(CheckCsrfTokenValve.java:123)
  31. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  32. at com.alibaba.citrus.turbine.pipeline.valve.AnalyzeURLValve.invoke(AnalyzeURLValve.java:126)
  33. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  34. at com.alibaba.citrus.turbine.pipeline.valve.SetLoggingContextValve.invoke(SetLoggingContextValve.java:66)
  35. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  36. at com.alibaba.citrus.turbine.pipeline.valve.PrepareForTurbineValve.invoke(PrepareForTurbineValve.java:52)
  37. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invokeNext(PipelineImpl.java:157)
  38. at com.alibaba.citrus.service.pipeline.impl.PipelineImpl$PipelineContextImpl.invoke(PipelineImpl.java:210)
  39. at com.alibaba.citrus.webx.impl.WebxControllerImpl.service(WebxControllerImpl.java:43)
  40. at com.alibaba.citrus.webx.impl.WebxRootControllerImpl.handleRequest(WebxRootControllerImpl.java:53)
  41. at com.alibaba.citrus.webx.support.AbstractWebxRootController.service(AbstractWebxRootController.java:165)
  42. at com.alibaba.citrus.webx.servlet.WebxFrameworkFilter.doFilter(WebxFrameworkFilter.java:152)
  43. at com.alibaba.citrus.webx.servlet.FilterBean.doFilter(FilterBean.java:148)
  44. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  45. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  46. at com.alibaba.citrus.webx.servlet.SetLoggingContextFilter.doFilter(SetLoggingContextFilter.java:61)
  47. at com.alibaba.citrus.webx.servlet.FilterBean.doFilter(FilterBean.java:148)
  48. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
  49. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
  50. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
  51. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
  52. at com.taobao.tomcat.valves.ContextLoadFilterValve.invoke(ContextLoadFilterValve.java:152)
  53. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
  54. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
  55. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
  56. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:429)
  57. at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1085)
  58. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)
  59. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1760)
  60. at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1719)
  61. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  62. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  63. at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
  64. at java.lang.Thread.run(Thread.java:745),
  65. ]
express中,表示异常信息的变量是throwExp

按照耗时进行过滤

  1. $ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" #cost>200 -x 3
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 59 ms.
  4. @ArrayList[
  5. @Object[][
  6. @ArrayList[
  7. @String[a],
  8.  
  9. @String[b],
  10.  
  11. @String[c],
  12.  
  13. @String[d],
  14. ],
  15. @ArrayList[
  16. @String[c],
  17.  
  18. @String[d],
  19. ],
  20. ],
  21.  
  22. @Integer[4],
  23. ]
#cost>200(单位是ms)表示只有当耗时大于200ms时才会输出,过滤掉执行时间小于200ms的调用

观察当前对象中的全局属性

如果想查看方法运行前后,当前对象中的全局属性,可以使用target关键字,代表当前对象

  1. $ watch com.taobao.container.web.arthas.rest.MyAppsController myFavoriteApps 'target'
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 128 ms.
  4. ts=2017-10-31 18:45:55;result=@MyAppsController[
  5. myFavAppsMapper=@$Proxy131[org.apache.ibatis.binding.MapperProxy@563e97f3],
  6. getAppNameAndIdByEmpId=@$Proxy135[HardCodedTarget(type=GetAppNameAndIdByEmpId, url=http://hello.com)],
  7. enableWebConsoleAppsMapper=@$Proxy132[org.apache.ibatis.binding.MapperProxy@7d51e4a8],
  8. ]

然后使用target.field_name访问当前对象的某个全局属性

  1. $ watch com.taobao.container.web.arthas.rest.MyAppsController myFavoriteApps 'target.myFavAppsMapper'
  2. Press Ctrl+C to abort.
  3. Affect(class-cnt:1 , method-cnt:1) cost in 126 ms.
  4. ts=2017-10-31 18:46:17;result=@$Proxy131[
  5. m1=@Method[public boolean java.lang.Object.equals(java.lang.Object)],
  6. m2=@Method[public java.lang.String java.lang.Object.toString()],
  7. m5=@Method[public abstract java.util.List com.taobao.container.dal.arthas.mapper.MyFavAppsMapper.listFavApps(java.util.Map)],
  8. m3=@Method[public abstract int com.taobao.container.dal.arthas.mapper.MyFavAppsMapper.delete(java.lang.String,java.lang.String,java.lang.String)],
  9. m4=@Method[public abstract long com.taobao.container.dal.arthas.mapper.MyFavAppsMapper.insert(com.taobao.container.dal.arthas.domain.MyFavAppsDO)],
  10. m0=@Method[public native int java.lang.Object.hashCode()],
  11. ]

原文: https://alibaba.github.io/arthas/watch.html