mirror of
https://github.com/alibaba/arthas.git
synced 2024-04-21 10:21:39 +00:00
Add condition-express param to monitor command (#1420)
This commit is contained in:
+40
-3
@@ -1,9 +1,14 @@
|
||||
package com.taobao.arthas.core.command.monitor200;
|
||||
|
||||
import com.alibaba.arthas.deps.org.slf4j.Logger;
|
||||
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
|
||||
import com.taobao.arthas.core.advisor.Advice;
|
||||
import com.taobao.arthas.core.advisor.AdviceListenerAdapter;
|
||||
import com.taobao.arthas.core.command.express.ExpressException;
|
||||
import com.taobao.arthas.core.command.model.MonitorModel;
|
||||
import com.taobao.arthas.core.shell.command.CommandProcess;
|
||||
import com.taobao.arthas.core.advisor.ArthasMethod;
|
||||
import com.taobao.arthas.core.util.StringUtils;
|
||||
import com.taobao.arthas.core.util.ThreadLocalWatch;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -65,9 +70,16 @@ import static com.taobao.arthas.core.util.ArthasCheckUtils.isEquals;
|
||||
class MonitorAdviceListener extends AdviceListenerAdapter {
|
||||
// 输出定时任务
|
||||
private Timer timer;
|
||||
private static final Logger logger = LoggerFactory.getLogger(MonitorAdviceListener.class);
|
||||
// 监控数据
|
||||
private ConcurrentHashMap<Key, AtomicReference<MonitorData>> monitorData = new ConcurrentHashMap<Key, AtomicReference<MonitorData>>();
|
||||
private final ThreadLocalWatch threadLocalWatch = new ThreadLocalWatch();
|
||||
private final ThreadLocal<Boolean> conditionResult = new ThreadLocal<Boolean>() {
|
||||
@Override
|
||||
protected Boolean initialValue() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
private MonitorCommand command;
|
||||
private CommandProcess process;
|
||||
|
||||
@@ -98,22 +110,47 @@ class MonitorAdviceListener extends AdviceListenerAdapter {
|
||||
public void before(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target, Object[] args)
|
||||
throws Throwable {
|
||||
threadLocalWatch.start();
|
||||
if (!StringUtils.isEmpty(this.command.getConditionExpress()) && command.isBefore()) {
|
||||
Advice advice = Advice.newForBefore(loader, clazz, method, target, args);
|
||||
long cost = threadLocalWatch.cost();
|
||||
this.conditionResult.set(isConditionMet(this.command.getConditionExpress(), advice, cost));
|
||||
//重新计算执行方法的耗时(排除执行condition-express耗时)
|
||||
threadLocalWatch.start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterReturning(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target,
|
||||
Object[] args, Object returnObject) throws Throwable {
|
||||
finishing(clazz, method, false);
|
||||
finishing(clazz, method, false, Advice.newForAfterRetuning(loader, clazz, method, target, args, returnObject));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterThrowing(ClassLoader loader, Class<?> clazz, ArthasMethod method, Object target,
|
||||
Object[] args, Throwable throwable) {
|
||||
finishing(clazz, method, true);
|
||||
finishing(clazz, method, true, Advice.newForAfterThrowing(loader, clazz, method, target, args, throwable));
|
||||
}
|
||||
|
||||
private void finishing(Class<?> clazz, ArthasMethod method, boolean isThrowing) {
|
||||
private void finishing(Class<?> clazz, ArthasMethod method, boolean isThrowing, Advice advice) {
|
||||
double cost = threadLocalWatch.costInMillis();
|
||||
|
||||
if (command.isBefore()) {
|
||||
if (!this.conditionResult.get()) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
//不满足condition-express的不纳入统计
|
||||
if (!isConditionMet(this.command.getConditionExpress(), advice, cost)) {
|
||||
return;
|
||||
}
|
||||
} catch (ExpressException e) {
|
||||
//condition-express执行错误的不纳入统计
|
||||
logger.warn("monitor execute condition-express failed.", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
final Key key = new Key(clazz.getName(), method.getName());
|
||||
|
||||
while (true) {
|
||||
|
||||
@@ -23,15 +23,19 @@ import com.taobao.middleware.cli.annotations.Summary;
|
||||
@Description("\nExamples:\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 {
|
||||
|
||||
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")
|
||||
@@ -45,6 +49,12 @@ public class MonitorCommand extends EnhancerCommand {
|
||||
this.methodPattern = methodPattern;
|
||||
}
|
||||
|
||||
@Argument(argName = "condition-express", index = 2, required = false)
|
||||
@Description(Constants.CONDITION_EXPRESS)
|
||||
public void setConditionExpress(String conditionExpress) {
|
||||
this.conditionExpress = conditionExpress;
|
||||
}
|
||||
|
||||
@Option(shortName = "c", longName = "cycle")
|
||||
@Description("The monitor interval (in seconds), 60 seconds by default")
|
||||
public void setCycle(int cycle) {
|
||||
@@ -63,6 +73,12 @@ public class MonitorCommand extends EnhancerCommand {
|
||||
this.numberOfLimit = numberOfLimit;
|
||||
}
|
||||
|
||||
@Option(shortName = "b", longName = "before", flag = true)
|
||||
@Description("Evaluate the condition-express before method invoke")
|
||||
public void setBefore(boolean before) {
|
||||
isBefore = before;
|
||||
}
|
||||
|
||||
public String getClassPattern() {
|
||||
return classPattern;
|
||||
}
|
||||
@@ -71,6 +87,10 @@ public class MonitorCommand extends EnhancerCommand {
|
||||
return methodPattern;
|
||||
}
|
||||
|
||||
public String getConditionExpress() {
|
||||
return conditionExpress;
|
||||
}
|
||||
|
||||
public int getCycle() {
|
||||
return cycle;
|
||||
}
|
||||
@@ -83,6 +103,10 @@ public class MonitorCommand extends EnhancerCommand {
|
||||
return numberOfLimit;
|
||||
}
|
||||
|
||||
public boolean isBefore() {
|
||||
return isBefore;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Matcher getClassNameMatcher() {
|
||||
if (classNameMatcher == null) {
|
||||
|
||||
Reference in New Issue
Block a user