Arthas-方法执行监测

Arthas
https://github.com/alibaba/arthas/blob/master/README_CN.md

curl -O https://arthas.aliyun.com/arthas-boot.jar
java -jar arthas-boot.jar

方法执行监测-watch

方便观察指定方法的调用情况。能观察到的范围为:返回值、抛出异常、入参。

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

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

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

观察的维度也比较多,主要体现在参数 advice 的数据结构上。Advice 参数最主要是封装了通知节点的所有信息。

请参考表达式核心变量中关于该节点的描述。

特殊用法请参考:http://gitlab.alibaba-inc.com/middleware-container/arthas/issues/263
OGNL表达式官网:https://commons.apache.org/proper/commons-ognl/language-guide.html

观察方法出参和返回值

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

    @Integer[4],
]

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

$ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" -x 3 

-x表示遍历深度,可以调整来打印具体的参数和结果内容。

条件表达式的例子

$ watch com.alibaba.sample.petstore.biz.impl.UserManagerImpl testAdd "{params, returnObj}" "params[0].equals('aaa')" -x 2 
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 29 ms.  
@ArrayList[
    @Object[][
        @String[aaa],
        @String[bbb],
    ],

    @Integer[6],
]

只有满足条件的调用,才会有响应。

观察异常信息

$ watch com.alibaba.sample.petstore.biz.impl.UserManagerImpl testAdd "{params, throwExp}"  -e -x 2 
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 29 ms.  
@ArrayList[
    @Object[][
        @String[aaa],
        @String[bbb],
    ],

    java.lang.NullPointerException
	...

按照耗时进行过滤

$ watch com.alibaba.sample.petstore.web.store.module.screen.ItemList add "{params,returnObj}" #cost>200 -x 3 
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 59 ms.  
@ArrayList[
    @Object[][
        @ArrayList[
            @String[a],

            @String[b],

            @String[c],

            @String[d],
        ],
        @ArrayList[
            @String[c],

            @String[d],
        ],
    ],

    @Integer[4],
]

表示只有当耗时大于200ms时才会输出,过滤掉执行时间小于200ms的调用

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

如果想查看方法运行前后,当前对象中的全局属性,可以使用target关键字,代表当前对象 ;
然后使用target.field_name访问当前对象的某个全局属性 ;

$ watch com.taobao.container.web.arthas.rest.MyAppsController myFavoriteApps 'target'
$ watch com.taobao.container.web.arthas.rest.MyAppsController myFavoriteApps 'target.myFavAppsMapper'
posted @ 2023-07-13 14:56  北feng  阅读(901)  评论(0)    收藏  举报