/
CallStackTimeConsumeModule.java
46 lines (42 loc) · 1.9 KB
/
CallStackTimeConsumeModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.sandbox.modules;
import com.alibaba.jvm.sandbox.api.Information;
import com.alibaba.jvm.sandbox.api.Module;
import com.alibaba.jvm.sandbox.api.annotation.Command;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatchBuilder;
import com.alibaba.jvm.sandbox.api.listener.ext.EventWatcher;
import com.alibaba.jvm.sandbox.api.resource.ModuleEventWatcher;
import com.sandbox.advicelistener.CallStackTimeConsumeAdviceListener;
import org.kohsuke.MetaInfServices;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource;
import java.util.Map;
/**
* 调用栈中每个方法调用的耗时
*/
@MetaInfServices(Module.class)
@Information(id = "call-stack-time-consume", author = "summer", version = "0.0.1")
public class CallStackTimeConsumeModule implements Module {
private static final Logger log = LoggerFactory.getLogger(CallStackTimeConsumeModule.class);
@Resource
private ModuleEventWatcher moduleEventWatcher;
@Command("print")
public void print(Map<String, String> req) {
try {
String classPattern = req.get("class");
String behaviorPattern = req.get("method");
if (classPattern == null || classPattern.trim().isEmpty() || behaviorPattern == null || behaviorPattern.trim().isEmpty()) {
log.warn("类匹配模式和方法匹配模式都不能为空");
return;
}
log.info("print call-stack-time-consume msg:classPattern:" + classPattern + ", behaviorPattern:" + behaviorPattern);
EventWatcher watcher = new EventWatchBuilder(moduleEventWatcher)
.onClass(classPattern)
.onBehavior(behaviorPattern)
.onWatching().withCall().withLine()
.onWatch(new CallStackTimeConsumeAdviceListener());
} catch (Exception e) {
log.error("发生异常", e);
}
}
}