From 5357324a18d51dca53da075c13327c4a7660111c Mon Sep 17 00:00:00 2001 From: beiwei30 Date: Sun, 28 Oct 2018 22:41:20 +0800 Subject: [PATCH] enhance translation --- site/src/site/sphinx/en/groovy.md | 170 ++++++++++++++++++++++++++++++ site/src/site/sphinx/groovy.md | 4 +- 2 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 site/src/site/sphinx/en/groovy.md diff --git a/site/src/site/sphinx/en/groovy.md b/site/src/site/sphinx/en/groovy.md new file mode 100644 index 000000000..991adff8c --- /dev/null +++ b/site/src/site/sphinx/en/groovy.md @@ -0,0 +1,170 @@ +groovy +=== + +> Arthas support groovy scripting to allow user to use script like BTrace. It is possible to use if/for/switch/while in groovy scripting, but has more limitations compared to BTrace. + +### Limitations + +1. Prohibit from alternating the original logic. Like `watch` command, The major purpose of scripting is monitoring and observing. +1. Only allow to monitor at the stages of before/success/exception/finish on one method. + +### Parameters + +|Parameter|Explanation| +|---:|:---| +|*class-pattern*|class name pattern| +|*method-pattern*|method name pattern| +|*script-filepath*|the absolute path of the groovy script| +|[S]|match all sub classes| +|[E]|enable regex match, the default is wildcard match| + +Note: the third parameter `script-filepath` must be the absolute path of the groovy script, for example `/tmp/test.groovy`. It is not recommended to use relative path, e.g. `./test.groovy`. + +### Explanation on the important callbacks + +```java +/** + * Listeners for script to enhance the class + */ +interface ScriptListener { + + /** + * When the script is created + * + * @param output Output + */ + void create(Output output); + + /** + * When the script is destroyed + * + * @param output Output + */ + void destroy(Output output); + + /** + * Before the method executes + * + * @param output Output + * @param advice Advice + */ + void before(Output output, Advice advice); + + /** + * After the method returns + * + * @param output Output + * @param advice Advice + */ + void afterReturning(Output output, Advice advice); + + /** + * After the method throws exceptions + * + * @param output Output + * @param advice Advice + */ + void afterThrowing(Output output, Advice advice); + +} +``` + +### `Advice` parameter + +`Advice` contains all information necessary for notification. Refer to [expression core parameters](advice-class.md) for more details. + +### `Output` parameter + +There are three methods in `Output`, used for outputting the corresponding text. + +```java +/** + * Output + */ +interface Output { + + /** + * Output text without line break + * + * @param string Text to output + * @return this + */ + Output print(String string); + + /** + * Output text with line break + * + * @param string Text to output + * @return this + */ + Output println(String string); + + /** + * Finish outputting from the script + * + * @return this + */ + Output finish(); + +} +``` + +### A groovy sample script to output logs + +```groovy +import com.taobao.arthas.core.command.ScriptSupportCommand +import com.taobao.arthas.core.util.Advice + +import static java.lang.String.format + +/** + * Output method logs + */ +public class Logger implements ScriptSupportCommand.ScriptListener { + + @Override + void create(ScriptSupportCommand.Output output) { + output.println("script create."); + } + + @Override + void destroy(ScriptSupportCommand.Output output) { + output.println("script destroy."); + } + + @Override + void before(ScriptSupportCommand.Output output, Advice advice) { + output.println(format("before:class=%s;method=%s;paramslen=%d;%s;", + advice.getClazz().getSimpleName(), + advice.getMethod().getName(), + advice.getParams().length, advice.getParams())) + } + + @Override + void afterReturning(ScriptSupportCommand.Output output, Advice advice) { + output.println(format("returning:class=%s;method=%s;", + advice.getClazz().getSimpleName(), + advice.getMethod().getName())) + } + + @Override + void afterThrowing(ScriptSupportCommand.Output output, Advice advice) { + output.println(format("throwing:class=%s;method=%s;", + advice.getClazz().getSimpleName(), + advice.getMethod().getName())) + } +} +``` + +Run the script like this: + +```bash +$ groovy com.alibaba.sample.petstore.dal.dao.ProductDao getProductById /Users/zhuyong/middleware/arthas/scripts/Logger.groovy -S +script create. +Press Ctrl+C to abort. +Affect(class-cnt:1 , method-cnt:1) cost in 102 ms. +before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@45df64fc; +returning:class=IbatisProductDao;method=getProductById; +before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@5b0e2d00; +returning:class=IbatisProductDao;method=getProductById; +``` diff --git a/site/src/site/sphinx/groovy.md b/site/src/site/sphinx/groovy.md index 3ab816b70..b5f5cff36 100644 --- a/site/src/site/sphinx/groovy.md +++ b/site/src/site/sphinx/groovy.md @@ -18,7 +18,7 @@ groovy |[S]|匹配所有的子类| |[E]|开启正则表达式匹配,默认为通配符匹配| -* 需要说明的是,第三个输入参数是脚本的绝对路径,比如 `/tmp/test.js`,不建议输入相对路径,比如 `./test.js` +需要说明的是,第三个输入参数是脚本的绝对路径,比如 `/tmp/test.groovy`,不建议输入相对路径,比如 `./test.groovy` ### 五个关键函数声明 @@ -167,4 +167,4 @@ before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Obje returning:class=IbatisProductDao;method=getProductById; before:class=IbatisProductDao;method=getProductById;paramslen=1;[Ljava.lang.Object;@5b0e2d00; returning:class=IbatisProductDao;method=getProductById; -``` \ No newline at end of file +```