add Troubleshooting method invoke exception case scenario #847 (#1335)

This commit is contained in:
Hollow Man 2020-08-13 14:52:09 +08:00 committed by GitHub
parent 1179fe16be
commit f125099387
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 472 additions and 0 deletions

View File

@ -0,0 +1,16 @@
在新的`Terminal 2`里,下载`arthas-boot.jar`,再用`java -jar`命令启动:
`wget https://arthas.aliyun.com/doc/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot`是`Arthas`的启动程序它启动后会列出所有的Java进程用户可以选择需要诊断的目标进程。
选择第一个进程,输入 `1`{{execute T2}} ,再`Enter/回车`
Attach成功之后会打印Arthas LOGO。输入 `help`{{execute T2}} 可以获取到更多的帮助信息。
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

View File

@ -0,0 +1,168 @@
### 现象
目前,访问 http://localhost/user/0 会返回500异常
`curl http://localhost/user/0`{{execute T3}}
```
{"timestamp":1550223186170,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"id < 1","path":"/user/0"}
```
但请求的具体参数,异常栈是什么呢?
### 查看UserController的 参数/异常
在Arthas里执行
`watch com.example.demo.arthas.user.UserController * '{params, throwExp}'`{{execute T2}}
1. 第一个参数是类名,支持通配
2. 第二个参数是函数名,支持通配
访问 `curl http://localhost/user/0`{{execute T3}} ,`watch`命令会打印调用的参数和异常
```bash
$ watch com.example.demo.arthas.user.UserController * '{params, throwExp}'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 53 ms.
ts=2019-02-15 01:35:25; [cost=0.996655ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
@IllegalArgumentException[java.lang.IllegalArgumentException: id < 1],
]
```
可以看到实际抛出的异常是`IllegalArgumentException`。
可以输入 `Q`{{execute T2}} 或者 `Ctrl+C` 退出watch命令。
如果想把获取到的结果展开,可以用`-x`参数:
`watch com.example.demo.arthas.user.UserController * '{params, throwExp}' -x 2`{{execute T2}}
```bash
$ watch com.example.demo.arthas.user.UserController * '{params, throwExp}' -x 2
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 2) cost in 190 ms, listenerId: 1
ts=2020-08-13 05:22:45; [cost=4.805432ms] result=@ArrayList[
@Object[][
@Integer[0],
],
java.lang.IllegalArgumentException: id < 1
at com.example.demo.arthas.user.UserController.findUserById(UserController.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
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)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
,
]
```
### 返回值表达式
在上面的例子里,第三个参数是`返回值表达式`,它实际上是一个`ognl`表达式,它支持一些内置对象:
* loader
* clazz
* method
* target
* params
* returnObj
* throwExp
* isBefore
* isThrow
* isReturn
你可以利用这些内置对象来组成不同的表达式。比如返回一个数组:
`watch com.example.demo.arthas.user.UserController * '{params[0], target, returnObj}'`{{execute T2}}
更多参考: https://arthas.aliyun.com/doc/advice-class.html
### 条件表达式
`watch`命令支持在第4个参数里写条件表达式比如
`watch com.example.demo.arthas.user.UserController * returnObj 'params[0] > 100'`{{execute T2}}
当访问 https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1 时,`watch`命令没有输出
当访问 https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/101 时,`watch`会打印出结果。
```bash
$ watch com.example.demo.arthas.user.UserController * returnObj 'params[0] > 100'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 47 ms.
ts=2019-02-13 19:42:12; [cost=0.821443ms] result=@User[
id=@Integer[101],
name=@String[name101],
]
```
### 当异常时捕获
`watch`命令支持`-e`选项,表示只捕获抛出异常时的请求:
`watch com.example.demo.arthas.user.UserController * "{params[0],throwExp}" -e`{{execute T2}}
### 按照耗时进行过滤
watch命令支持按请求耗时进行过滤比如
`watch com.example.demo.arthas.user.UserController * '{params, returnObj}' '#cost>200'`{{execute T2}}

View File

@ -0,0 +1,10 @@
通过本教程基本掌握了Arthas排查函数调用异常。更多高级特性可以在下面的进阶指南里继续了解。
* [Arthas进阶](https://arthas.aliyun.com/doc/arthas-tutorials?language=cn&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas 文档](https://arthas.aliyun.com/doc/)
欢迎关注公众号获取Arthas项目的信息源码分析案例实践。
![Arthas公众号](/arthas/scenarios/common-resources/assets/qrcode_gongzhonghao.jpg)

View File

@ -0,0 +1,45 @@
{
"title": "Arthas 排查函数调用异常 案例",
"description": "Arthas 排查函数调用异常 案例",
"difficulty": "高级使用者",
"time": "10分钟",
"details": {
"steps": [
{
"title": "Start demo",
"text": "start-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "排查函数调用异常",
"text": "case-watch-method-exception.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
},
"assets": {
"host01": []
}
},
"environment": {
"uilayout": "terminal",
"showdashboard": true,
"dashboards": [
{
"name": "Web Port 80",
"port": 80
}
]
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

View File

@ -0,0 +1,11 @@
![Arthas](https://arthas.aliyun.com/doc/_images/arthas.png)
`Arthas` 是Alibaba开源的Java诊断工具深受开发者喜爱。在线排查问题无需重启动态跟踪Java代码实时监控JVM状态。
`Arthas` 支持JDK 6+支持Linux/Mac/Windows采用命令行交互模式同时提供丰富的 `Tab` 自动补全功能,进一步方便进行问题的定位和诊断。
* Github: https://github.com/alibaba/arthas
* 文档: https://arthas.aliyun.com/doc/

View File

@ -0,0 +1,14 @@
下载`demo-arthas-spring-boot.jar`,再用`java -jar`命令启动:
`wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar`{{execute T1}}
`demo-arthas-spring-boot`是一个很简单的spring boot应用源代码[查看](https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-arthas-spring-boot)
启动之后可以访问80端口 https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com
![Demo Web](/arthas/scenarios/common-resources/assets/demo-web.png)

View File

@ -0,0 +1,16 @@
In the new `Terminal 2`, download `arthas-boot.jar` and start with the `java -jar` command:
`wget https://arthas.aliyun.com/doc/arthas-boot.jar
java -jar arthas-boot.jar`{{execute T2}}
`arthas-boot` is the launcher for `Arthas`. It lists all the Java processes, and the user can select the target process to be diagnosed.
Select the first process, type `1`{{execute T2}} then type `Enter`
After the Attach is successful, Arthas LOGO is printed. Enter `help`{{execute T2}} for more help.
![Arthas Boot](/arthas/scenarios/common-resources/assets/arthas-boot.png)

View File

@ -0,0 +1,116 @@
Currently, visiting http://localhost/user/0 will return a 500 error:
`curl http://localhost/user/0`{{execute T3}}
```
{"timestamp":1550223186170,"status":500,"error":"Internal Server Error","exception":"java.lang.IllegalArgumentException","message":"id < 1","path":"/user/0"}
```
But what are the specific parameters of the request, what is the exception stack?
### View the parameters/exception of UserController
Execute in Arthas:
`watch com.example.demo.arthas.user.UserController * '{params, throwExp}'`{{execute T2}}
1. The first argument is the class name, which supports wildcards.
2. The second argument is the function name, which supports wildcards.
Visit `curl http://localhost/user/0`{{execute T3}} , the `watch` command will print the parameters and exception
```bash
$ watch com.example.demo.arthas.user.UserController * '{params, throwExp}'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 53 ms.
ts=2019-02-15 01:35:25; [cost=0.996655ms] result=@ArrayList[
@Object[][isEmpty=false;size=1],
@IllegalArgumentException[java.lang.IllegalArgumentException: id < 1],
]
```
The user can see that the actual thrown exception is `IllegalArgumentException`.
The user can exit the watch command by typing `Q`{{execute T2}} or `Ctrl+C`.
If the user want to expand the result, can use the `-x` option:
`watch com.example.demo.arthas.user.UserController * '{params, throwExp}' -x 2`{{execute T2}}
```bash
$ watch com.example.demo.arthas.user.UserController * '{params, throwExp}' -x 2
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 2) cost in 190 ms, listenerId: 1
ts=2020-08-13 05:22:45; [cost=4.805432ms] result=@ArrayList[
@Object[][
@Integer[0],
],
java.lang.IllegalArgumentException: id < 1
at com.example.demo.arthas.user.UserController.findUserById(UserController.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...
,
]
```
### The return value expression
In the above example, the third argument is the `return value expression`, which is actually an `ognl` expression that supports some built-in objects:
* loader
* clazz
* method
* target
* params
* returnObj
* throwExp
* isBefore
* isThrow
* isReturn
You can use these built-in objects in the expressions. For example, return an array:
`watch com.example.demo.arthas.user.UserController * '{params[0], target, returnObj}'`{{execute T2}}
More references: https://arthas.aliyun.com/doc/en/advice-class.html
### The conditional expression
The `watch` command supports conditional expressions in the fourth argument, such as:
`watch com.example.demo.arthas.user.UserController * returnObj 'params[0] > 100'`{{execute T2}}
When visit https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/1 , the `watch` command print nothing.
When visit https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com/user/101 , the `watch` command will print:
```bash
$ watch com.example.demo.arthas.user.UserController * returnObj 'params[0] > 100'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:2) cost in 47 ms.
ts=2019-02-13 19:42:12; [cost=0.821443ms] result=@User[
id=@Integer[101],
name=@String[name101],
]
```
### Capture when an exception occurs
The `watch` command supports the `-e` option, which means that only requests that throw an exception are caught:
`watch com.example.demo.arthas.user.UserController * "{params[0],throwExp}" -e`{{execute T2}}
### Filter by cost
The watch command supports filtering by cost, such as:
`watch com.example.demo.arthas.user.UserController * '{params, returnObj}' '#cost>200'`{{execute T2}}

View File

@ -0,0 +1,6 @@
Through this tutorial, you can know how to Troubleshooting method invoke exception. More advanced features can be found in the Advanced Guide below.
* [Arthas Advanced](https://arthas.aliyun.com/doc/arthas-tutorials?language=en&id=arthas-advanced)
* [Arthas Github](https://github.com/alibaba/arthas)
* [Arthas Documentation](https://arthas.aliyun.com/doc/en)

View File

@ -0,0 +1,45 @@
{
"title": "Arthas Troubleshooting method invoke exception",
"description": "Arthas Troubleshooting method invoke exception",
"difficulty": "expert",
"time": "10 minutes",
"details": {
"steps": [
{
"title": "Start demo",
"text": "start-demo.md"
},
{
"title": "Start arthas-boot",
"text": "arthas-boot.md"
},
{
"title": "Troubleshooting method invoke exception",
"text": "case-watch-method-exception.md"
}
],
"intro": {
"text": "intro.md"
},
"finish": {
"text": "finish.md"
},
"assets": {
"host01": []
}
},
"environment": {
"uilayout": "terminal",
"showdashboard": true,
"dashboards": [
{
"name": "Web Port 80",
"port": 80
}
]
},
"backend": {
"imageid": "java",
"environmentsprotocol": "http"
}
}

View File

@ -0,0 +1,11 @@
![Arthas](https://arthas.aliyun.com/doc/_images/arthas.png)
`Arthas` is a Java diagnostic tool open-sourced by Alibaba middleware team. Arthas helps developers in trouble-shooting issues in production environment for Java based applications without modifying code or restarting servers.
`Arthas` supports JDK 6+, supports Linux/Mac/Windows.
* Github: https://github.com/alibaba/arthas
* Documentation: https://arthas.aliyun.com/doc/en

View File

@ -0,0 +1,14 @@
Download `demo-arthas-spring-boot.jar`, and start with `java -jar` command:
`wget https://github.com/hengyunabc/spring-boot-inside/raw/master/demo-arthas-spring-boot/demo-arthas-spring-boot.jar
java -jar demo-arthas-spring-boot.jar`{{execute T1}}
`demo-arthas-spring-boot` is a simple Spring Boot demo, the source code here: [View](https://github.com/hengyunabc/spring-boot-inside/tree/master/demo-arthas-spring-boot)
After booting, access port 80: https://[[HOST_SUBDOMAIN]]-80-[[KATACODA_HOST]].environments.katacoda.com
![Demo Web](/arthas/scenarios/common-resources/assets/demo-web.png)