BTrace常用场景示例

BTrace是线上不重启服务的情况下定位问题的神器,本文介绍了最常用的几种场景

定位慢调用

查看com.tim.BtraceCase#count耗时很长时的上下文:入参、成员变量等信息

@BTrace
public class Debug {
    @OnMethod(
            clazz = "com.tim.BtraceCase", 
            method = "count", 
            location = @Location(value = Kind.RETURN)
    )
    public static void run(@Self Object self, int number, @Duration long duration) {
        if (duration / 1000000 > 100) {
            println("cost: " + duration / 1000000);
            final Object sum =get(field(classOf(self), "sum"), self); //成员变量
            println("sum: " + sum);
            jstack();  //调用栈
            println("----");  //刷新缓存,否则有部分信息没有显示
        }
    }
}

异常上下文

比如当com.tim.BtraceCase#count方法抛出异常时,捕获异常信息及入参以及对象实例信息。其中关键是location = @Location(value = Kind.ERROR)

@BTrace
public class Debug {
    @OnMethod(
            clazz = "com.tim.BtraceCase", 
            method = "count", 
            location = @Location(value = Kind.ERROR)
    )
    public static void run(@Self Object self, int number, @TargetInstance Throwable e) {
        println("number: " + number); //入参
        println(e); //异常信息
        final Object sum =get(field(classOf(self), "sum"), self); //成员变量
        println("sum: " + sum); 
        jstack();  //调用栈
        println("----");  //刷新缓存,否则有部分信息没有显示
    }
}

输出内容:

number: 4100
java.lang.IllegalArgumentException: data should not be divided by 100
sum: 8407050
com.tim.BtraceCase.count(BtraceCase.java:22)
com.tim.BtraceCase.main(BtraceCase.java:10)
----

输出统计信息

http://github.com/btraceio/btrace/blob/master/samples/Histogram.java

BTrace系列

  • BTrace实现原理

参考

  • Btrace入门到熟练小工完全指南
  • 动态追踪技术漫谈
  • 后端技术杂谈
  • btrace一些你不知道的事(源码入手)

你可能感兴趣的:(BTrace常用场景示例)