字节码基于JavaAgent的全链路监控六 开发应用级监控

Posted 九师兄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节码基于JavaAgent的全链路监控六 开发应用级监控相关的知识,希望对你有一定的参考价值。

1.概述

上一篇文章:【字节码】基于JavaAgent的全链路监控五- ThreadLocal链路追踪

本章节转载:基于JavaAgent的全链路监控六《开发应用级监控》

在我们的监控程序中,需要对各种模块进行监控;servlet、rpc、http、jdbc、redis、logic等,那么我们在设计监控程序时就需要对监控的程序进行模块化开发,可以在需要的时候进行组装配置即可,以方便我们监控程序的扩展和可控制性。这一章节我们把监控模块剥离,采用工厂模式进行调用{目前是静态工厂在我们实际使用中可以把工厂做成动态配置化}。

2.环境准备

IntelliJ IDEA Community Edition
jdk1.8.0_45 64位

3.配置信息(路径相关修改为自己的)

配置位置:Run/Debug Configurations -> VM options
配置内容:-javaagent:/Users/lcc/IdeaProjects/lcc_work/test-byte-buddy/byte-buddy-v1x-javaagent-demo3/target/byte-buddy-v1x-javaagent-demo3-1.0-SNAPSHOT.jar=test

4. 核心代码

ApplicationAgent

package com.javaagent.bytebuddy.demo5;

import com.javaagent.bytebuddy.demo5.plugin.IPlugin;
import com.javaagent.bytebuddy.demo5.plugin.InterceptPoint;
import com.javaagent.bytebuddy.demo5.plugin.PluginFactory;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.utility.JavaModule;

import java.lang.instrument.Instrumentation;
import java.util.List;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class ApplicationAgent 

    //JVM 首先尝试在代理类上调用以下方法
    public static void premain(String agentArgs, Instrumentation inst) 

        System.out.println("基于javaagent链路追踪{源码微信公众号:bugstack虫洞栈}");
        System.out.println("==========================================================\\r\\n");
        AgentBuilder agentBuilder = new AgentBuilder.Default();

        List<IPlugin> pluginGroup = PluginFactory.pluginGroup;
        for (IPlugin plugin : pluginGroup) 
            InterceptPoint[] interceptPoints = plugin.buildInterceptPoint();
            for (InterceptPoint point : interceptPoints) 

                AgentBuilder.Transformer transformer = (builder, typeDescription, classLoader, javaModule) -> 
                    builder = builder.visit(Advice.to(plugin.adviceClass()).on(point.buildMethodsMatcher()));
                    return builder;
                ;
                agentBuilder = agentBuilder.type(point.buildTypesMatcher())
                        .transform(transformer).asTerminalTransformation();
            
        

        //监听
        AgentBuilder.Listener listener = new AgentBuilder.Listener() 
            @Override
            public void onDiscovery(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) 

            

            @Override
            public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b, DynamicType dynamicType) 
                System.out.println("onTransformation:" + typeDescription);
            

            @Override
            public void onIgnored(TypeDescription typeDescription, ClassLoader classLoader, JavaModule javaModule, boolean b) 

            

            @Override
            public void onError(String s, ClassLoader classLoader, JavaModule javaModule, boolean b, Throwable throwable) 

            

            @Override
            public void onComplete(String s, ClassLoader classLoader, JavaModule javaModule, boolean b) 

            

        ;

        agentBuilder.with(listener).installOn(inst);

    



TrackManager

package com.javaagent.bytebuddy.demo5.track;

import java.util.Stack;

/**
 * 追踪管控
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class TrackManager 

    private static final ThreadLocal<Stack<Span>> track = new ThreadLocal<>();

    private static Span createSpan() 
        Stack<Span> stack = track.get();
        if (stack == null) 
            stack = new Stack<>();
            track.set(stack);
        
        String linkId;
        if (stack.isEmpty()) 
            linkId = TrackContext.getLinkId();
            if (linkId == null) 
                linkId = "nvl";
                TrackContext.setLinkId(linkId);
            
         else 
            Span span = stack.peek();
            linkId = span.getLinkId();
            TrackContext.setLinkId(linkId);
        
        return new Span(linkId);
    

    public static Span createEntrySpan() 
        Span span = createSpan();
        Stack<Span> stack = track.get();
        stack.push(span);
        return span;
    


    public static Span getExitSpan() 
        Stack<Span> stack = track.get();
        if (stack == null || stack.isEmpty()) 
            TrackContext.clear();
            return null;
        
        return stack.pop();
    

    public static Span getCurrentSpan() 
        Stack<Span> stack = track.get();
        if (stack == null || stack.isEmpty()) 
            return null;
        
        return stack.peek();
    




TrackContext

package com.javaagent.bytebuddy.demo5.track;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class TrackContext 

    private static final ThreadLocal<String> trackLocal = new ThreadLocal<>();

    public static void clear()
        trackLocal.remove();
    

    public static String getLinkId()
        return trackLocal.get();
    

    public static void setLinkId(String linkId)
        trackLocal.set(linkId);
    



Span

package com.javaagent.bytebuddy.demo5.track;

import java.util.Date;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class Span 

    private String linkId;  //链路ID
    private Date enterTime; //方法进入时间

    public Span()

    public Span(String linkId)
        this.linkId = linkId;
        this.enterTime = new Date();
    

    public String getLinkId() 
        return linkId;
    

    public void setLinkId(String linkId) 
        this.linkId = linkId;
    

    public Date getEnterTime() 
        return enterTime;
    

    public void setEnterTime(Date enterTime) 
        this.enterTime = enterTime;
    


PluginFactory

package com.javaagent.bytebuddy.demo5.plugin;

import com.javaagent.bytebuddy.demo5.plugin.impl.jvm.JvmPlugin;
import com.javaagent.bytebuddy.demo5.plugin.impl.link.LinkPlugin;

import java.util.ArrayList;
import java.util.List;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class PluginFactory 

    public static List<IPlugin> pluginGroup = new ArrayList<>();

    static 
        //链路监控
        pluginGroup.add(new LinkPlugin());
        //Jvm监控
        pluginGroup.add(new JvmPlugin());
    



IPlugin

package com.javaagent.bytebuddy.demo5.plugin;

/**
 * 监控组件
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public interface IPlugin 

    //名称
    String name();

    //监控点
    InterceptPoint[] buildInterceptPoint();

    //拦截器类
    Class adviceClass();



InterceptPoint

package com.javaagent.bytebuddy.demo5.plugin;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

/**
 * 拦截点
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public interface InterceptPoint 

    //类匹配规则
    ElementMatcher<TypeDescription> buildTypesMatcher();

    //方法匹配规则
    ElementMatcher<MethodDescription> buildMethodsMatcher();



LinkPlugin

package com.javaagent.bytebuddy.demo5.plugin.impl.link;

import com.javaagent.bytebuddy.demo5.plugin.IPlugin;
import com.javaagent.bytebuddy.demo5.plugin.InterceptPoint;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class LinkPlugin implements IPlugin 

    @Override
    public String name() 
        return "link";
    

    @Override
    public InterceptPoint[] buildInterceptPoint() 
        return new InterceptPoint[]
                new InterceptPoint() 
                    @Override
                    public ElementMatcher<TypeDescription> buildTypesMatcher() 
                        return ElementMatchers.nameStartsWith("com.javaagent.bytebuddy.demo5_1");
                    

                    @Override
                    public ElementMatcher<MethodDescription> buildMethodsMatcher() 
                        return ElementMatchers.isMethod()
                                .and(ElementMatchers.any())
                                .and(ElementMatchers.not(ElementMatchers.nameStartsWith("main")));
                    
                
        ;
    

    @Override
    public Class adviceClass() 
        return LinkAdvice.class;
    



LinkAdvice

package com.javaagent.bytebuddy.demo5.plugin.impl.link;

import com.javaagent.bytebuddy.demo5.track.Span;
import com.javaagent.bytebuddy.demo5.track.TrackContext;
import com.javaagent.bytebuddy.demo5.track.TrackManager;
import net.bytebuddy.asm.Advice;

import java.util.UUID;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class LinkAdvice 

    @Advice.OnMethodEnter()
    public static void enter(@Advice.Origin("#t") String className, @Advice.Origin("#m") String methodName) 
        Span currentSpan = TrackManager.getCurrentSpan();
        if (null == currentSpan) 
            String linkId = UUID.randomUUID().toString();
            TrackContext.setLinkId(linkId);
        
        TrackManager.createEntrySpan();
    

    @Advice.OnMethodExit()
    public static void exit(@Advice.Origin("#t") String className, @Advice.Origin("#m") String methodName) 
        Span exitSpan = TrackManager.getExitSpan();
        if (null == exitSpan) return;
        System.out.println("链路追踪(MQ):" + exitSpan.getLinkId() + " " + className + "." + methodName + " 耗时:" + (System.currentTimeMillis() - exitSpan.getEnterTime().getTime()) + "ms");
    



JvmAdvice

package com.javaagent.bytebuddy.demo5.plugin.impl.jvm;

import net.bytebuddy.asm.Advice;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class JvmAdvice 

    @Advice.OnMethodExit()
    public static void exit() 
        JvmStack.printMemoryInfo();
        JvmStack.printGCInfo();
    



JvmPlugin

package com.javaagent.bytebuddy.demo5.plugin.impl.jvm;

import com.javaagent.bytebuddy.demo5.plugin.IPlugin;
import com.javaagent.bytebuddy.demo5.plugin.InterceptPoint;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class JvmPlugin implements IPlugin 

    @Override
    public String name() 
        return "jvm";
    

    @Override
    public InterceptPoint[] buildInterceptPoint() 
        return new InterceptPoint[]
                new InterceptPoint() 
                    @Override
                    public ElementMatcher<TypeDescription> buildTypesMatcher() 
                        return ElementMatchers.nameStartsWith("org.itstack.demo.test");
                    

                    @Override
                    public ElementMatcher<MethodDescription> buildMethodsMatcher() 
                        return ElementMatchers.isMethod()
                                .and(ElementMatchers.any())
                                .and(ElementMatchers.not(ElementMatchers.nameStartsWith("main")));
                    
                
        ;
    

    @Override
    public Class adviceClass() 
        return JvmAdvice.class;
    
    


JvmStack

package com.javaagent.bytebuddy.demo5.plugin.impl.jvm;

import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.Arrays;
import java.util.List;

/**
 * 博客:http://itstack.org
 * 论坛:http://bugstack.cn
 * 公众号:bugstack虫洞栈  {获取学习源码}
 * create by fuzhengwei on 2019
 */
public class JvmStack 

    private static final long MB = 1048576L;

    public static void printMemoryInfo() 
        MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
        MemoryUsage headMemory = memory.getHeapMemoryUsage();

        String info = String.format("\\ninit: %s\\t max: %s\\t used: %s\\t committed: %s\\t use rate: %s\\n",
                headMemory.getInit() / MB + "MB",
                headMemory.getMax() / MB + "MB", headMemory.getUsed() / MB + "MB",
                headMemory.getCommitted() / MB + "MB",
                headMemory.getUsed() * 100 / headMemory.getCommitted() + "%"

        );

        System.out.print(info);

        MemoryUsage nonheadMemory = memory.getNonHeapMemoryUsage();

        info = String.format("init: %s\\t max: %s\\t used: %s\\t committed: %s\\t use rate: %s\\n",
                nonheadMemory.getIn

以上是关于字节码基于JavaAgent的全链路监控六 开发应用级监控的主要内容,如果未能解决你的问题,请参考以下文章

字节码基于JavaAgent的全链路监控五- ThreadLocal链路追踪

字节码基于JavaAgent的全链路监控四-JVM内存与GC信息

字节码JavaAgent的全链路监控篇二,通过字节码增加监控执行 耗时

基线监控:基于依赖关系的全链路智能监控报警

字节码JavaAgent ByteBuddy操作监控方法 字节码

字节码javaagent 入门 案例 最简单的案例