btrace是很好跟踪线上环境出现异常且棘手的问题,但是在配置环境变量之后,按照前辈们所说不需要编译,
直接btrace jps 脚本文件就可以,但是我下载current版本之后,但是我在执行过程中,却并未打印出来(有时候可以)
但是我采用编译后的class,是可以的,这个都无妨。那就简单展示效果吧!package com.tonghuashun.btrace.course01.chenyang;
public class KPIApp { /** * @param args */ public static void main(String[] args) throws Exception { int i = 1; while (true) { Thread.sleep(2000); CalcKPI kpi = new CalcKPI(); i++; kpi.calc(i + "%"); } } }
package com.tonghuashun.btrace.course01.chenyang;
public class CalcKPI { public String calc(String kpi) throws Exception { System.out.println("KPI :" + kpi); return "KPI :" + kpi; } }
package com.tonghuashun.btrace.course01.chenyang;
import com.sun.btrace.annotations.*; import static com.sun.btrace.BTraceUtils.*; /** * This script demonstrates the possibility to intercept method calls that are * about to be executed from the body of a certain method. This is achieved by * using the {@linkplain Kind#CALL} location value. */ // 在CalcKPI累中调用任何的方法体内,所执行的方法 @BTrace public class AllCalls1 { @OnMethod(clazz = "com.tonghuashun.btrace.course01.chenyang.CalcKPI", method = "/.*/", location = @Location(value = Kind.CALL, clazz = "/.*/", method = "/.*/")) public static void m(@Self Object self, @TargetMethodOrField String method, @ProbeMethodName String probeMethod) { // all calls to the methods // with signature "()" println(Strings.strcat(method, Strings.strcat(" in ", probeMethod))); } }package com.tonghuashun.btrace.course01;
import static com.sun.btrace.BTraceUtils.println; import static com.sun.btrace.BTraceUtils.str; import static com.sun.btrace.BTraceUtils.strcat; import com.sun.btrace.annotations.BTrace; import com.sun.btrace.annotations.Kind; import com.sun.btrace.annotations.Location; import com.sun.btrace.annotations.OnMethod; import com.sun.btrace.annotations.OnTimer; import com.sun.btrace.annotations.ProbeClassName; import com.sun.btrace.annotations.Return; import com.sun.btrace.annotations.Self; @BTrace public class TraceMethodArgsAndReturn { @OnMethod( clazz = "com.tonghuashun.btrace.course01.CaseObject", method = "execute", location = @Location(Kind.RETURN) ) public static void traceExecute(@Self CaseObject instance, int sleepTime, @Return String result, @ProbeClassName String className) { println("call CaseObject.execute"); println(strcat("sleepTime is:", str(sleepTime))); println(strcat("return value is:", str(result))); println(strcat("CLASSNAME is:", str(className))); // BTraceUtils.print("return value is:" + result); // // println(strcat("sleepTotalTime is:", // str(get(field("CaseObject", "sleepTotalTime"), instance)))); // // println(strcat("return value is:", str(result))); } @OnTimer(value = 4000) public static void print() { } }