Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwu51 committed Mar 13, 2024
1 parent 863840f commit f7a8173
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
13 changes: 10 additions & 3 deletions src/main/java/w/core/Swapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,33 @@ public boolean swap(Message message) {

Set<Class<?>> classes = Global.allLoadedClasses.getOrDefault(transformer.getClassName(), new HashSet<>());

boolean exists = false;
for (Class<?> aClass : classes) {
if (aClass.isInterface() || Modifier.isAbstract(aClass.getModifiers())) {
Set<String> candidates = new HashSet<>();
for (Object instances : Global.getInstances(aClass)) {
candidates.add(instances.getClass().getName());
}
Global.error("Should use a simple pojo, but " + aClass.getName() + " is a Interface or Abstract class or something wired, \nmaybe you should use: " + candidates);
Global.error("!Error: Should use a simple pojo, but " + aClass.getName() + " is a Interface or Abstract class or something wired, \nmaybe you should use: " + candidates);
return false;
}
exists = true;
}

if (!exists) {
Global.error("Class not exist" + transformer.getClassName());
return false;
}

Global.addTransformer(transformer);
Global.debug("add transform finish, will retrans class");
Global.debug("add transformer finish, will retrans class");


for (Class<?> aClass : classes) {
try {
Global.addActiveTransformer(aClass, transformer);
} catch (Throwable e) {
Global.error("re transform error:", e);
Global.error("re transformer error:", e);
return false;
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/w/core/model/TraceTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TraceTransformer extends BaseClassTransformer {
String method;

public TraceTransformer(TraceMessage traceMessage) {
this.message = traceMessage;
this.className = traceMessage.getSignature().split("#")[0];
this.method = traceMessage.getSignature().split("#")[1];
this.traceId = traceMessage.getId();
Expand All @@ -30,7 +31,7 @@ public byte[] transform(String className, byte[] origin) throws Exception {
CtClass ctClass = Global.classPool.makeClass(new ByteArrayInputStream(origin));
for (CtMethod declaredMethod : ctClass.getDeclaredMethods()) {
if (Objects.equals(declaredMethod.getName(), method)) {
addOuterWatchCodeToMethod(declaredMethod);
addTraceCodeToMethod(declaredMethod);
}
}
byte[] result = ctClass.toBytecode();
Expand All @@ -39,19 +40,29 @@ public byte[] transform(String className, byte[] origin) throws Exception {
return result;
}

private void addOuterWatchCodeToMethod(CtMethod ctMethod) throws CannotCompileException, NotFoundException {
private void addTraceCodeToMethod(CtMethod ctMethod) throws CannotCompileException, NotFoundException {
ctMethod.instrument(new ExprEditor() {
public void edit(MethodCall m) throws CannotCompileException {
String code = "{" +
"long start = System.currentTimeMillis();" +
"$_ = $proceed($$);" +
"long duration = System.currentTimeMillis() - start;" +
"w.util.RequestUtils.fillCurThread(\"" + message.getId() + "\");" +
"w.Global.info(\"line" + m.getLineNumber() + "," + m.getSignature() + "cost:\"+duration+\"ms\");" +
"w.util.RequestUtils.clearRequestCtx();" ;
"long start = System.currentTimeMillis();\n" +
"$_ = $proceed($$);\n" +
"long duration = System.currentTimeMillis() - start;\n" +
"w.util.RequestUtils.fillCurThread(\"" + message.getId() + "\");\n" +
"w.Global.info(\"line" + m.getLineNumber() + "," + m.getClassName() + "#" + m.getMethodName() + ",cost:\"+duration+\"ms\");\n" +
"w.util.RequestUtils.clearRequestCtx();" +
"}";
m.replace(code);
}
});
ctMethod.addLocalVariable("s", CtClass.longType);
ctMethod.addLocalVariable("cost", CtClass.longType);
ctMethod.insertBefore("s = System.currentTimeMillis();");
ctMethod.insertAfter("{" +
"cost = System.currentTimeMillis() - s;" +
"w.util.RequestUtils.fillCurThread(\"" + message.getId() + "\");\n" +
"w.Global.info(\"" + className + "#" + method + ", total cost:\"+cost+\"ms\");\n" +
"w.util.RequestUtils.clearRequestCtx();" +
"}");
}
public boolean equals(Object other) {
if (other instanceof OuterWatchTransformer) {
Expand Down

0 comments on commit f7a8173

Please sign in to comment.