Btrace介绍
Btrace (Byte Trace)是sun推出的一款java 动态、安全追踪工具,可以不停机的情况下监控线上情况,并且做到最少的侵入,占用最少的系统资源。BTrace应用较为广泛的原因应该是其安全性和无侵入性,其中热交互技术,使得我们无需启动Agent的情况下动态跟踪分析,其安全性不会导致对目标Java进程的任何破坏性影响,使得BTrace成为我们线上产品问题定位的利器。无侵入性无需我们对原有代码做任何修改,降低上线风险和测试成本,并且无需重启启动目标Java进程进行Agent加载即可动态分析和跟踪目标程序,可以说BTrace可以满足大部分的应用场景。
限制:
- 不能创建对象
- 不能创建数组
- 不能抛出和捕获异常
- 不能调用任何对象方法和静态方法
- 不能给目标程序中的类静态属性和对象的属性进行赋值
- 不能有外部、内部和嵌套类
- 不能有同步块和同步方法
- 不能有循环(for, while, do…while)
- 不能继承任何的类
- 不能实现接口
- 不能包含assert断言语句
下载安装
源码地址: https://github.com/btraceio/btrace.git
mac配置环境变量
vim ~/.bash_profile
#Btrace
BTRACE_HOME=/Users/edwinghu/Desktop/workplace/JVM/btrace-2.0.2-bin
export BTRACE_HOME
export PATH=${PATH}:${BTRACE_HOME}/bin
source ~/.bash_profile
查看版本
btrace --version
创建测试接口
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController //注意模板 需要这个
@RequestMapping("/btrace")
public class BtraceServiceController {
@GetMapping("/test")
public String getTestReturn(@RequestParam String input) {
System.out.println("btrace test return" + input);
return "btrace test return";
}
}
编译启动查看PID
java -jar createBtraceService-0.0.1-SNAPSHOT.jar (注意这里)
jps查看PID 3016 jar
使用samples中的文件监控
btrace -p 2021 3016 AllMethods.java -p 制定启动端口