Alibaba's open source problem-checking tool won't work? Here comes the best practice!

Alibaba's open source problem-checking tool won't work? Here comes the best practice!

△Hollis, a person with a unique pursuit of Coding△
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!
This is the 291st original sharing
author of
Hollis l Wang Ji's source l Hollis (ID: hollischuang)

Getting started step
installation

https://arthas.gitee.io/install-detail.html The
above command will download the startup script file as.sh to the current directory. The execution method:

curl -L https://alibaba.github.io/arthas/install.sh | sh

as.sh starts:


curl -sk https://arthas.gitee.io/arthas-boot.jar -o ~/.arthas-boot.jar  && echo "alias as.sh='java -jar ~/.arthas-boot.jar --repo-mirror aliyun --use-http'" >> ~/.bashrc && source ~/.bashrc

Online tutorial experience

https://alibaba.github.io/arthas/arthas-tutorials?language=cn of
course, you can also experience it locally~ by downloading an arthas-idea-plugin experience demo directly to get started locally
https://github.com/ WangJi92/arthas-plugin-demo
global command description:

-x 是展示结果属性遍历深度,默认为 1
-n 是执行的次数 ,q 退出

-c classloader 的hash值

退出 q ,关闭 stop

Understand the most commonly used trace and watch functions

Watch and trace are the most commonly used functions in arthas diagnosis for developers to solve online problems! Basic
trace
example:

trace com.wangji92.arthas.plugin.demo.controller.CommonController getRandomInteger -n 5 '1==1'

https://arthas.gitee.io/trace.html

  • Performance optimization~
  • What is the specific process of this method called! It can be seen through the call chain.
  • If there is an exception, you can view the
    advanced functions of the exception stack : The
    trace command will only trace the sub-calls in the matched function, and will not trace multiple levels down. Because trace is more expensive, multi-layer trace may result in a lot of classes and functions to be traced.
    Example:
    trace -E xxxClassA|xxxClassB method1 | method2

    Such as:

    trace -E com.wangji92.arthas.plugin.demo.controller.CommonController|com.wangji92.arthas.plugin.demo.service.ArthasTestService traceE|doTraceE -n 5 '1==1'

    watch
    https://arthas.gitee.io/watch.html
    wathc is literally understood as the information of the observation value, you can view the input parameters, return values, exceptions, you can execute expressions to obtain static variables, and target.xxx calls the target implementation Fields, methods, etc. are all OK~ as long as you want to get nothing you can't do~
    Basic example:

watch com.wangji92.arthas.plugin.demo.controller.CommonController traceE '{params,returnObj,throwExp}' -n 5 -x 3 '1==1'

arthas expression core variables


public class Advice {

    private final ClassLoader loader;

    private final Class<?> clazz;

    private final ArthasMethod method;

    private final Object target;

    private final Object[] params;

    private final Object returnObj;

    private final Throwable throwExp;

    private final boolean isBefore;

    private final boolean isThrow;

    private final boolean isReturn;

    // getter/setter  

}  

From watch and trace, we can see that the following '1==1' executes a conditional expression. When the value is true, an ognl expression is executed, watch observes params, returnObj, throwExp input parameters, return value, whether The exception is also an expression, so what is going on?
Spring el expressions
have not studied ognl. Spring who has used it for many years must know his el expressions. There is also a concept in el expressions called [Context and expression] as shown below, because the simple context can be resolved." The meaning of the "booleanList[0]" script~ This is very familiar and easy to understand, so ognl expressions are not difficult.

class Simple {

    public List<Boolean> booleanList = new ArrayList<Boolean>();

}

Simple simple = new Simple();

simple.booleanList.add(true);

StandardEvaluationContext simpleContext = new StandardEvaluationContext(simple);

// false is passed in here as a string. SpEL and the conversion service will

// correctly recognize that it needs to be a Boolean and convert it

parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");

// b will be false

Boolean b = simple.booleanList.get(0);

The
ognl expression arthas is the same, except that a script called ognl is used. The core variables are his context, and these fields can be obtained directly. The fields params, returnObj, and throwExp that watch observes are what we call the concept of context, observing information about parameters, return values, and exceptions.
The following is the code for expression evaluation and watch observation in the arthas source code! Advice is a context, and a variable const is added here. Isn’t that simple after knowing this? ?
com.taobao.arthas.core.advisor.ReflectAdviceListenerAdapter#isConditionMet

/**

 * 判断条件是否满足,满足的情况下需要输出结果

 * @param conditionExpress 条件表达式

 * @param advice 当前的advice对象

 * @param cost 本次执行的耗时

 * @return true 如果条件表达式满足

 */

protected boolean isConditionMet(String conditionExpress, Advice advice, double cost) throws ExpressException {

    return StringUtils.isEmpty(conditionExpress) ||

            ExpressFactory.threadLocalExpress(advice).bind(Constants.COST_VARIABLE, cost).is(conditionExpress);

}

protected Object getExpressionResult(String express, Advice advice, double cost) throws ExpressException {

    return ExpressFactory.threadLocalExpress(advice)

            .bind(Constants.COST_VARIABLE, cost).get(express);

}

Expression practice
arthas group often asks how to judge overloaded methods, nothing more than evaluation conditions? The number of parameters and what is the first parameter? The type of return value, etc. can be used as your evaluation conditions. In the following watch, the first section is the observed value, and the latter section is the expression evaluation, which is executed only when the conditions are met.
The input parameter length is greater than 0:

watch com.wangji92.arthas.plugin.demo.controller.CommonController traceE '{params,returnObj,throwExp}' -n 5 -x 3 'params.length >0'

The return value is String and the length is greater than 5:

watch com.wangji92.arthas.plugin.demo.controller.CommonController traceE '{params,returnObj,throwExp}' -n 5 -x 3 'returnObj instanceof java.lang.String && returnObj.length>5'

Conditional expression + asynchronous tasks
only have bugs in specific scenarios. How to troubleshoot bugs?
How to solve the problem that only appears once or twice a day?
Conditional expressions are mainly used for filtering. For example, certain scenarios only appear in specific parameters, and it will take a lot of time to wait. At this time, you can use conditional expression filtering + asynchronous tasks.

ognl expression

https://arthas.gitee.io/ognl.html
From the above, ognl is omnipotent in watch and trace. In fact, tt also uses ognl expressions to execute logic. @xxxClas@xxxStaticField is the syntax of static variables Sugar ognl, take a good look at the official documentation.
For special usage of OGNL, please refer to: https://github.com/alibaba/arthas/issues/71
Obtaining static variables
Static variables may be loaded by multiple classloaders in a jvm, and the jvm is recognized as an instance is loaded by a classloader, so it needs Know the hash value of the current static class (sc -d com.wangji92.arthas.plugin.demo.controller.StaticTest) can be obtained through this command.

ognl  -x  3 '@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_DOUBLE' -c e374b99

Call spring method?
watch executes the ognl syntax to obtain the spring context and then call the bean method

watch -x 3 -n 1 
org.springframework.web.servlet.DispatcherServlet doDispatch '@org.springframework.web.context.support.WebApplicationContextUtils@getWebApplicationContext(params[0].getServletContext()).getBean("commonController").getRandomInteger()'

ognl executes a static spring context and then calls bean methods

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getBean("commonController").getRandomInteger()' -c e374b99

Does it feel like taking off, omnipotent! The premise is that you have to master some simple grammar of ognl!

complete

For online troubleshooting, I feel that these commands are enough for you. There are some other decompilation, flame graphs, .. time tunnel, logger level modification, jvm environment information, etc. I feel that there are no frequencies above. High, after all, JVM information is specially monitored~ Even without arthas, you can find better tools to analyze the stack and JVM failures.
Some special user cases are worth learning and thinking: https://github.com/alibaba/arthas/issues?q=label%3Auser-case is
over?
what? I can’t remember so many commands, and some advanced ognl syntax is cool... What if I let you get all the spring environment variables? I haven't experienced enough of these two commands, trace and watch? How can I be more advanced! Okay, please see below.

Advanced
premise

The premise is that you have a general understanding of arthas, basic commands are somewhat conceptual, and the simple syntax of ognl can be understood. Simple conditional expressions will be used. We said before that there are so many commands in arthas, so it is indispensable to remember a small book! I feel uncomfortable and want to cry~ Don't worry, Brother Wang will solve the problem for you.
At present, the official Arthas tools are not simple enough. Some commands need to be remembered, especially some high-level grammars that are particularly extensible, such as ognl to get spring context and do whatever they want, watch and trace are not simple enough, and some command tool information needs to be constructed. You only need a plug-in that can simply handle string information. When dealing with online problems, the fastest and most convenient commands are needed, so the arthas idea plugin still has meaning and value.
The meaning of arthas idea plugin
is not to deal with protocol-level problems, but to solve the problem of command generation. Since the project is managed in the idea, you think about which category you want to watch. This plugin knows it, helping you more convenient and faster The build command. You must understand this when using the arthas idea plugin! Mainly to solve the problem of how you structure the command
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!

solve the problem

Spring environment variable priority issue Get static variables Flame graph integration logger command integration decompilation integration trace -E integration tt integration...... Basically all the functions you can see on arths are integrated into this! Search for arths idea directly in idea to install it.

Common special usage issues

Static variables
can be obtained directly by ognl

ognl  -x  3 '@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_DOUBLE' -c e374b99

Can be obtained by watch (the cursor is placed on the field)

watch com.wangji92.arthas.plugin.demo.controller.StaticTest * '{params,returnObj,throwExp,@com.wangji92.arthas.plugin.demo.controller.StaticTest@INVOKE_STATIC_DOUBLE}' -n 5 -x 3 '1==1'

General variables
can be obtained through spring context.getBean().field (this is to configure a static spring context to see the usage documentation) tt, watch are also possible~ the same principle

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getBean("staticTest").filedValue' -c e374b99

Watch get placed on the field

watch com.wangji92.arthas.plugin.demo.controller.StaticTest * '{params,returnObj,throwExp,target.filedValue}' -n 5 -x 3 'method.initMethod(),method.constructor!=null || [email protected]@isStatic(method.method.getModifiers())'

The value of the selected configuration item springContext.getEnvironment() (this is to configure a static spring context see the usage document

ognl -x 3 '#springContext=@com.wangji92.arthas.plugin.demo.common.ApplicationContextProvider@context,#springContext.getEnvironment().getProperty("custom.name")' -c e374b99

Obtain the values ​​of all configuration items. It is also possible to obtain the spring context tt and static by watch. The same principle


watch -x 3 -n 1  org.springframework.web.servlet.DispatcherServlet doDispatch '#springContext=@org.springframework.web.context.support.WebApplicationContextUtils@getWebApplicationContext(params[0].getServletContext()),#allProperties={},#standardServletEnvironment=#propertySourceIterator=#springContext.getEnvironment(),#propertySourceIterator=#standardServletEnvironment.getPropertySources().iterator(),#propertySourceIterator.{#key=#this.getName(),#allProperties.add("                "),#allProperties.add("------------------------- name:"+#key),#this.getSource() instanceof java.util.Map ?#this.getSource().entrySet().iterator.{#key=#this.key,#allProperties.add(#key+"="+#standardServletEnvironment.getProperty(#key))}:#{}},#allProperties'

Plug-in address: https://plugins.jetbrains.com/plugin/13581-arthas-idea
Arthas is officially holding an essay event, if you have

  • Problems checked with Arthas
  • Source code interpretation of Arthas
  • Suggestions to Arthas
  • Unlimited, other content related to Arthas

Welcome to participate in the essay activity, and there are prizes to get~
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!
Click to read the original text to access the essay activity address.

Welfare time

GIFT TIME

Arthas technical community specially sponsored part of the gifts (1 Bluetooth speaker, 10 decompression brain-shaped balls) of this essay activity to readers of Hollis

This gift does not need to be commented, nor forwarded,
using the fairest lottery method.
No public concern: the programmer interview on-site
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!
public number backstage Re: Essay
to participate in sweepstakes
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!
face of Java 329: Which command to monitor the status of virtual machines running a variety of information?
In-depth Concurrency Issue 013: Expanding synchronized-lock optimization
Alibaba's open source problem-checking tool won't work?  Here comes the best practice!
If you like this article,
please press and hold the QR code and follow Hollis.

Forwarding to the circle of friends is my greatest support.

One point looking
like a feeling of
watching a support
↘↘↘

Guess you like

Origin blog.51cto.com/13626762/2544073