The author | Agentd

Arthas Watch command usage guide

Arthas is one of my favorite development and debugging tools for the Java space.

Every time I had a problem with my testing, when someone was resending code to add a log, I would happily pull out my Arthas and say, boy, you don’t have to worry about resending code to add a log. Arthas, you deserve it.

This time, I would like to introduce a function that I use most: Watch. Arthas has many features, but this one is my favorite. With Watch, I no longer have to log in order to observe function calls.

Arthas: what is

Arthas says about itself on its website:

Arthas is an open source Java diagnostic tool for Alibaba that developers love. Arthas can help you when you are stuck with a problem like the following:

  1. From which JAR is this class loaded? Why are all kinds of class-related exceptions reported?
  2. Why didn’t the code I changed execute? Did I not commit? Got the branch wrong?
  3. If you encounter a problem, you cannot debug it online. Can you only re-publish it by logging?
  4. There is a problem with a user’s data processing online, but it cannot be debugged online, and it cannot be reproduced offline!
  5. Is there a global view of the health of the system?
  6. Is there any way to monitor the real-time health of the JVM?
  7. How to quickly locate application hot spots, generate flame map?

One click to install and start Arthas

  • Method 1: Implement Arthas one-click remote diagnosis using Cloud Toolkit

Cloud Toolkit is a free local IDE plug-in released by AliYun to help developers develop, test, diagnose and deploy applications more efficiently. Plugins enable one-click deployment of native applications to any server, even the cloud (ECS, EDAS, ACK, ACR, applets, etc.); There are also built-in Arthas diagnostics, Dubbo tools, Terminal terminals, file uploads, function calculations, and MySQL executators. Not only the mainstream version of IntelliJ IDEA, but also Eclipse, Pycharm, Maven and others.

It is recommended to download Cloud Toolkit using the IDEA plugin to use Arthas: t.tb.cn/2A5CbHWveOX…

  • Method 2: Download directly

Address: github.com/alibaba/art… .

Curl - O https://alibaba.github.io/arthas/arthas-boot.jar && Java - Dfile. Encoding = utf-8 - jar arthas - boot. Jar duplicate codeCopy the code

Explain the above shell command a bit. The command is divided into two parts, the preceding part is to download Arthas and the following part is to start Arthas.

Curl curl curl curl curl curl curl curl curl curl curl This is because some servers do not have WGET installed, but most of them have CURL installed. You can change ‘curl’ to wget if you have wGET installed on your server.

With wget the command can be changed to:

# wget version of the commandWget https://alibaba.github.io/arthas/arthas-boot.jar && Java - Dfile. Encoding = utf-8 - jar arthas - boot. Jar duplicate codeCopy the code

Another point that needs to be explained is -dfile.encoding =UTF-8. This Java setting is intended to make Arthas output Chinese without garble. See the Java default encoding thinking triggered by Arthas Chinese garble in my previous article.

Arthas watch command

Watch lets you easily observe the invocation of a given method. Can observe the range is: return value, throw exception, input parameter (can also observe the function of the object itself, do not know why the official introduction did not say this “, by writing OGNL expression for the corresponding variable view.

# watch -h
# USAGE
watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class-pattern method-pattern express [condition-express] Copies codeCopy the code

1. Observe the result of the methodreturnObj

It looks complicated, but it’s actually very simple. To take the simplest example: Suppose we want to look at the contains method in the string in this code.

public class App {
    public static void main(String[] args) throws IOException {
        String hello = "Hello Arthas";
        while (true) {
            boolean contains = StringUtils.contains(hello, "Arthas"); System.out.println(contains); }}} copy the codeCopy the code

You can use the following statement:

## Observe the result that contains returns
[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains returnObj -n 3
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 68 ms.
# ts=2020-05-02 16:46:04; [cost] = 2.424254 ms result = @ Boolean (true)
# ts=2020-05-02 16:46:05; [cost] = 0.21033 ms result = @ Boolean (true)
# ts=2020-05-02 16:46:06; [cost] = 0.165514 ms result = @ Boolean (true)Copy the codeCopy the code

-n 3 Indicates that the command is executed only for three times. This parameter is often used. Otherwise, the output may flush.

2. Filter unconcerned callscondition-express

Obviously, the real case is not as simple as the example above. There must be more than one place in the actual service code that calls the Contains method of String. We need to filter out extraneous calls.

## Observe the result that contains returns, and filter out irrelevant calls
[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains returnObj 'params[1]=="Arthas"'
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 29 ms.
# ts=2020-05-02 16:48:50; [cost] = 0.331109 ms result = @ Boolean (true)
# ts=2020-05-02 16:48:51; [cost] = 0.175224 ms result = @ Boolean (true)
# ts=2020-05-02 16:48:52; [cost] = 0.138984 ms result = @ Boolean (true)Copy the codeCopy the code

An incoming parameter is an easy way to distinguish one call from another. By params[1]==”Arthas” condition- Express, we can keep only function calls whose second input is Arthas.

3. Observe both the input and the result

[arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains {params,returnObj} 'params[1]=="Arthas"'
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 33 ms.
# ts=2020-05-02 16:51:27; [cost] = 0.507486 ms result = @ ArrayList [
# @Object[][isEmpty=false;size=2],
# @Boolean[true],
#]Copy the codeCopy the code

You can view the fields you want to view at the same time by wrapping them with {}. Notice that params is an array, but it is printed without printing out the contents. Use -x 2 to specify the depth of the property traversal of the printed object.

arthas@11939]$ watch org.apache.commons.lang3.StringUtils contains  {params,returnObj} 'params[1]=="Arthas"' -x 2
# Press Q or Ctrl+C to abort.
# Affect(class-cnt:1 , method-cnt:2) cost in 35 ms.
# ts=2020-05-02 16:51:33; [cost] = 0.391218 ms result = @ ArrayList [
# @Object[][
# @String[Hello Arthas],
# @String[Arthas],
#].
# @Boolean[true],
#]Copy the codeCopy the code

4. Give you some practical examples that I use

During the development of momo’s dynamic recommendation, testing often involves checking whether a user has turned on the corresponding service switch, and it is often necessary to check whether a certain experimental switch is turned on.

Select * from 1234567 if ElasticSearch is enabled
watch com.momo.Experiment enableElasticSearch returnObj 'target.momoId=="1234567"'
# ts=2020-05-02 20:09:46; [cost] = 24.443527 ms result = @ Boolean (true)Copy the codeCopy the code

I also often check the return result or exception based on the tinder user ID of the entry:

The process method of the MorecControlFlow class returns the result
The momoId attribute of the first parameter of the process method is valid only if the value is "123454567"
Neither the classpath nor the tinder is real data
watch com.momo.MorecControlFlow process returnObj 'params[0].momoId=="123454567"'
# ts=2019-03-18 21:09:46; [cost] = 264.434972 ms result = @ Boolean (true)
IMorecShuffler shuffle exception
The momoId attribute of the first parameter of the process method is valid only if the value is "123454567"
watch com.momo.plugins.shuffler.IMorecShuffler shuffle throwExp 'params[0].morecRequest.momoId=="123454567"'
# ts=2019-03-27 20:54:29; [cost] = 46.642339 ms result = Java lang. IndexOutOfBoundsException: Index: 12, Size: 11at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665) at java.util.ArrayList.add(ArrayList.java:477) at Com. Momo. Plugin. Shuffler. RoomShuffler. Shuffle (45) RoomShuffler: duplicate codeCopy the code

A few tips

I’ve just listed the common observation methods and parameters above, but there are many other commands supported by Watch. Check out Arthas’s watch command documentation.

You can also view it using watch -h after starting the arthas command.

While working with Arthas many people find it difficult to get the fully qualified name of a class, this can be done by using the Copy Refrence shortcut of Idea. The self-defined shortcut is ⌥⇧⌘C.

The other thing is that when writing code it is best to break it down and try to wrap small functions into separate functions, you will be back thanking yourself when you need to use Arthas to observe function calls.

Arthas’s essay campaign is in full swing

Arthas is officially holding an essay call if you have:

  • Problems identified using Arthas
  • Source code interpretation of Arthas
  • Advise Arthas
  • No limit, other Arthas related content

Welcome to participate in the essay activity, there are prizes to win oh ~ click to submit

“Alibaba Cloud originator focuses on micro-service, Serverless, container, Service Mesh and other technical fields, focuses on the trend of cloud native popular technology, large-scale implementation of cloud native practice, and becomes the public account that most understands cloud native developers.”