1. 程式人生 > >Btrace指令碼對正則應用,註解應用,異常捕獲

Btrace指令碼對正則應用,註解應用,異常捕獲

標籤

Btrace,正則匹配類及方法,監控指定註解,異常捕獲

對btrace應用參考:https://my.oschina.net/yaofull/blog/2966834

所有Controller方法日誌及異常列印

package com.sun.btrace.bin;

import com.sun.btrace.AnyType;
import com.sun.btrace.annotations.*;
import java.util.*;
import com.alibaba.fastjson.JSON; 
import com.alibaba.fastjson.serializer.SerializeConfig; 
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; 
import static com.sun.btrace.BTraceUtils.*;

@BTrace(unsafe = true)

public class Test{

    @TLS
    static Throwable currentException;

    //監控所有controller方法的輸入輸出
    @OnMethod(clazz = "/com\\.cetc\\.hubble\\..*Controller/",method = "/.*/",location=@Location(Kind.RETURN))
    public static void printMethodLog(@ProbeClassName String pcn, // 被攔截的類名
                               @ProbeMethodName String pmn,  //被攔截的方法名
                               AnyType[] args,  //被攔截的方法的引數值
                                @Return AnyType result ,@Duration long duration){
        println("------------------ start ---------------");
        println("className: " + pcn);
        println("MethodName: " + pmn +" duration:"+ duration/1000000 + "ms");
        println("args: " + JSON.toJSONString(args) );
        println("result: " +JSON.toJSONString(result));
        println("------------------ end ---------------");
        println();
    }
    //監控異常
    @OnMethod(
        clazz="/com\\.cetc\\.hubble\\..*/",
        method="/.*/",
        location=@Location(Kind.ERROR)
    )
    public static void onerror(@TargetInstance Throwable exception) { 
       println("------------------ start ---------------");
       // 列印異常堆疊
       Threads.jstack(exception);
       println("------------------ end ---------------");
       println();
    }

}

指定註解的日誌及異常資訊

註解類

package com.cetc.hubble.common.annotation;

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface BTraceEntry {
    boolean required() default true;
}

指令碼

package com.sun.btrace.bin;

import com.sun.btrace.AnyType;
import com.sun.btrace.annotations.*;
import java.util.*;
import com.alibaba.fastjson.JSON; 
import com.alibaba.fastjson.serializer.SerializeConfig; 
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; 
import static com.sun.btrace.BTraceUtils.*;

@BTrace(unsafe = true)

public class Annotation{

    @TLS
    static Throwable currentException;

    //監控某一個方法的輸入輸出
    @OnMethod(clazz = "/com\\.cetc\\.hubble\\..*/",method = "@com.cetc.hubble.common.annotation.BTraceEntry",location=@Location(Kind.RETURN))
    public static void printMethodLog(@ProbeClassName String pcn, // 被攔截的類名
                               @ProbeMethodName String pmn,  //被攔截的方法名
                               AnyType[] args,  //被攔截的方法的引數值
                                @Return AnyType result ,@Duration long duration){
        println("------------------ start ---------------");
        println("className: " + pcn);
        println("MethodName: " + pmn +" duration:"+ duration/1000000 + "ms");
        println("args: " + JSON.toJSONString(args) );
        println("result: " +JSON.toJSONString(result));
        println("------------------ end ---------------");
        println();
    }

    //監控異常
    @OnMethod(
        clazz="/com\\.cetc\\.hubble\\..*/",
        method="/.*/",
        location=@Location(Kind.ERROR)
    )
    public static void onerror(@TargetInstance Throwable exception) { 
       println("------------------ start ---------------");
       // 列印異常堆疊
       Threads.jstack(exception);
       println("------------------ end ---------------");
       println();
    }
}