Btrace协助处理线上应用动态分析和跟踪

Btrace协助处理线上应用动态分析和跟踪

场景

需要动态统计线上一些Servlet性能耗时,打印堆栈信息、cpu信息、内存信息等。

简单环境

一、BTrace

https://github.com/btraceio/btrace/releases/tag/v1.3.8.1

二、maven引用

	
		
		
			com.sun.tools.btrace
			btrace-boot
			1.2.3
		
	

三、BTrace脚本

package org.ybygjy.jvm;

import com.sun.btrace.BTraceUtils;
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.TLS;

@BTrace
public class BTraceMonitor {
    @TLS
    private static long startTime;
    @OnMethod(clazz = "/org\\.ybygjy\\.spring\\.web\\..+/", method = "/.+/", location = @Location(Kind.ENTRY))
    public static void startExecute() {
        startTime = BTraceUtils.timeMillis();
    }

    @OnMethod(clazz = "/org\\.ybygjy\\.spring\\.web\\..+/", method = "/.+/", location = @Location(Kind.RETURN))
    public static void endExecute() {
        long costTime = BTraceUtils.timeMillis() - startTime;
        BTraceUtils.println(BTraceUtils.Sys.VM.libraryPath() + ">>" + BTraceUtils.Threads.jstackAllStr() + ">>time cost=>" + costTime);
    }
}

四、被跟踪对象

package org.ybygjy.spring.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BTraceServlet extends HttpServlet {
    /**
     * serial number
     */
    private static final long serialVersionUID = 3802654659007695958L;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Service entrance.......");
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        resp.getOutputStream().write("BTrace Monitor...".getBytes());
        System.out.println("Service finished.......");
    }
}

web.xml

	
		btraceMonitor
		org.ybygjy.spring.web.BTraceServlet
	
	
		btraceMonitor
		/btrace
	

图示

Btrace协助处理线上应用动态分析和跟踪_第1张图片

 

你可能感兴趣的:(2001_编程语言,5001_辅助开发)