Skip to content

Commit

Permalink
feat: add ~ for method-pattern to take value from class-pattern
Browse files Browse the repository at this point in the history
此功能的用意:使用特殊值 ~ 表示其值从class-pattern获取
目的从stack或trace的输出复制出来不用修改就可直接使用
**增加方便性,以提升效率**
  • Loading branch information
qxo committed Oct 27, 2023
1 parent e638b97 commit 44adc44
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.taobao.arthas.core.command.monitor200;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.middleware.cli.annotations.Argument;
import com.taobao.middleware.cli.annotations.Description;

/**
* 基于方法命令抽像类.
*/
public abstract class AbstractMethodBasedCommand extends EnhancerCommand {


protected String classPattern;
protected String methodPattern;

@Argument(argName = "class-pattern", index = 0)
@Description("Path and classname of Pattern Matching")
public void setClassPattern(final String classPattern) {
this.classPattern = classPattern;
}

@Argument(argName = "method-pattern", index = 1)
@Description("Method of Pattern Matching, ~ means take from class-pattern com.a.C:method or com.a.C.method")
public void setMethodPattern(final String methodPattern) {
this.methodPattern = methodPattern;
}

@Override
protected void enhance(final CommandProcess process) {
if ("~".equals(methodPattern)) { //smart way
final Pattern smartPattern = Pattern.compile("([.][A-Z][^.]*)([.:])([a-z][^. ]*)$");
final Matcher matcher = smartPattern.matcher(classPattern);
if (matcher.find()) {
methodPattern = matcher.group(3);
classPattern = matcher.replaceAll("$1");
process.echoTips("smart way input ==> " + classPattern + " " + methodPattern + "\n");
}
}
super.enhance(process);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,21 @@
@Summary("Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. ")
@Description("\nExamples:\n" +
" monitor org.apache.commons.lang.StringUtils isBlank\n" +
" monitor org.apache.commons.lang.StringUtils.isBlank ~\n" +
" monitor org.apache.commons.lang.StringUtils:isBlank ~\n" +
" monitor org.apache.commons.lang.StringUtils isBlank -c 5\n" +
" monitor org.apache.commons.lang.StringUtils isBlank params[0]!=null\n" +
" monitor -b org.apache.commons.lang.StringUtils isBlank params[0]!=null\n" +
" monitor -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank\n" +
Constants.WIKI + Constants.WIKI_HOME + "monitor")
public class MonitorCommand extends EnhancerCommand {
public class MonitorCommand extends AbstractMethodBasedCommand {

private String classPattern;
private String methodPattern;
private String conditionExpress;
private int cycle = 60;
private boolean isRegEx = false;
private int numberOfLimit = 100;
private boolean isBefore = false;

@Argument(argName = "class-pattern", index = 0)
@Description("Path and classname of Pattern Matching")
public void setClassPattern(String classPattern) {
this.classPattern = classPattern;
}

@Argument(argName = "method-pattern", index = 1)
@Description("Method of Pattern Matching")
public void setMethodPattern(String methodPattern) {
this.methodPattern = methodPattern;
}

@Argument(argName = "condition-express", index = 2, required = false)
@Description(Constants.CONDITION_EXPRESS)
public void setConditionExpress(String conditionExpress) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
@Summary("Display the stack trace for the specified class and method")
@Description(Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE +
" stack org.apache.commons.lang.StringUtils isBlank\n" +
" stack org.apache.commons.lang.StringUtils.isBlank ~\n" +
" stack org.apache.commons.lang.StringUtils:isBlank ~\n" +
" stack *StringUtils isBlank\n" +
" stack *StringUtils isBlank params[0].length==1\n" +
" stack *StringUtils isBlank '#cost>100'\n" +
" stack -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank\n" +
Constants.WIKI + Constants.WIKI_HOME + "stack")
public class StackCommand extends EnhancerCommand {
private String classPattern;
private String methodPattern;
public class StackCommand extends AbstractMethodBasedCommand {

private String conditionExpress;
private boolean isRegEx = false;
private int numberOfLimit = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
@Summary("Time Tunnel")
@Description(Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE +
" tt -t *StringUtils isEmpty\n" +
" tt -t *StringUtils.isEmpty ~\n" +
" tt -t *StringUtils:isEmpty ~\n" +
" tt -t *StringUtils isEmpty params[0].length==1\n" +
" tt -l\n" +
" tt -i 1000\n" +
Expand All @@ -49,16 +51,14 @@
" tt -s '{params[0] > 1}' -w '{params}' \n" +
" tt --delete-all\n" +
Constants.WIKI + Constants.WIKI_HOME + "tt")
public class TimeTunnelCommand extends EnhancerCommand {
public class TimeTunnelCommand extends AbstractMethodBasedCommand {
// 时间隧道(时间碎片的集合)
// TODO 并非线程安全?
private static final Map<Integer, TimeFragment> timeFragmentMap = new LinkedHashMap<Integer, TimeFragment>();
// 时间碎片序列生成器
private static final AtomicInteger sequence = new AtomicInteger(1000);
// TimeTunnel the method call
private boolean isTimeTunnel = false;
private String classPattern;
private String methodPattern;
private String conditionExpress;
// list the TimeTunnel
private boolean isList = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@Summary("Trace the execution time of specified method invocation.")
@Description(value = Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE +
" trace org.apache.commons.lang.StringUtils isBlank\n" +
" trace org.apache.commons.lang.StringUtils.isBlank ~\n" +
" trace org.apache.commons.lang.StringUtils:isBlank ~\n" +
" trace *StringUtils isBlank\n" +
" trace *StringUtils isBlank params[0].length==1\n" +
" trace *StringUtils isBlank '#cost>100'\n" +
Expand All @@ -42,10 +44,8 @@
" trace OuterClass$InnerClass *\n" +
Constants.WIKI + Constants.WIKI_HOME + "trace")
//@formatter:on
public class TraceCommand extends EnhancerCommand {
public class TraceCommand extends AbstractMethodBasedCommand {

private String classPattern;
private String methodPattern;
private String conditionExpress;
private boolean isRegEx = false;
private int numberOfLimit = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@Summary("Display the input/output parameter, return object, and thrown exception of specified method invocation")
@Description(Constants.EXPRESS_DESCRIPTION + "\nExamples:\n" +
" watch org.apache.commons.lang.StringUtils isBlank\n" +
" watch org.apache.commons.lang.StringUtils.isBlank ~\n" +
" watch org.apache.commons.lang.StringUtils:isBlank ~\n" +
" watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj, throwExp}' -x 2\n" +
" watch *StringUtils isBlank params[0] params[0].length==1\n" +
" watch *StringUtils isBlank params '#cost>100'\n" +
Expand All @@ -31,10 +33,8 @@
" watch javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter\n" +
" watch OuterClass$InnerClass\n" +
Constants.WIKI + Constants.WIKI_HOME + "watch")
public class WatchCommand extends EnhancerCommand {
public class WatchCommand extends AbstractMethodBasedCommand {

private String classPattern;
private String methodPattern;
private String express;
private String conditionExpress;
private boolean isBefore = false;
Expand Down

0 comments on commit 44adc44

Please sign in to comment.