Agent injection mechanism principle of UAV MOF working principle

  java

The topic of middleware enhancement framework (MOF) of UAVStack] tells us in detail how MOF Agent in UAV accomplishes data capture with javaagent(premain) and Java Assistant technology on the premise of non-invasive application. Welcome to continue to pay attention to UAVStack and learn more about UAV technology innovation.

Preface

As a UAV application data capture framework, MOF(Moniter Framwork) not only realizes non-invasive data capture of applications, but also realizes flexible control of functions at the framework level, and ensures good scalability. It plays an important role in UAV.

As the foundation of UAV MOF, MOF Agent injection mechanism also provides the possibility for UAV to capture application data without invasion. UAV Agent code injection mechanism combines javaagent(premain) and javaassist technology to rewrite bytecode before application bytecode is loaded into JVM. Adaptor is used to adapt different application servers. Currently, the supports include Tomcat(6+), SpringBoot, Jetty(7+) and so on. interceptor is used to realize the cut-off of different application servers, which provides the basis for MOF framework and application data capture.

Java Agent technology

Since JDK1.5, java.lang.Instrument package has been introduced into JDK to provide the ability to modify Class bytecode and dynamically modify Class types in the system at runtime before Java program classes are loaded. one of the core concepts, Java Agent, can be understood as a bytecode converter or class object converter. For bytecode conversion and Class object conversion, Java agent provides corresponding methods, namely Java agent premain method and Java Agent Main method. UAV MOF uses Java agent premain mode, so this article mainly explains this conversion mode.

Java agent premain provides the magic ability to intercept and modify bytecode class files before they are loaded by JVM. Currently, almost all probe-based monitoring systems (such as ONEAPM Servers) are non-invasive monitoring of applications based on this ability. There are two important concepts in Java agent premain, namely premium and transformer.

Premain will be executed before the main method of the program. We know that the entry of the program is the main method. premain represents the actions to be executed before the program is officially started and has AOP-like capabilities. Transformer, meaning converter, provides the ability to convert bytecode file streams.

uav1.png

Figure 1 Class File Transformation Diagram

The collection of premain and transformer artifacts can modify any Class file loaded into the JVM. The flow is shown in Figure 1. Any Class file will go through premain. Through a series of transformer, the Class bytecode file stream will eventually become the perfect one and then be loaded into the JVM. Of course, modifying the Class bytecode file stream is done in transformer. This has a problem, got the bytecode file stream, how to modify it? Of course, it is to give full play to human’s special skills and use tools such as javassist.

Javassist technology

Javaassist is an open source class library that analyzes, edits, and creates Java bytecodes. It can dynamically generate classes, modify classes, and directly use Java coding.

In the Java agent technology mentioned above, the bytecode file stream of the class is obtained in the transformer, and the bytecode stream is parsed as a class object by Javaassist and modified, which is very fast and convenient. Javassist and Java agent will get twice the result with half the effort. Please refer to the official website for the use of Javaassist.http://www.javassist.org.

MOF Agent injection mechanism

The previous article introduced a pair of good partners: Javaagent technology and Javassit technology. Java agent is responsible for intercepting and converting byte stream, and Javaassist is used to analyze and modify it during conversion. These two technologies provide the technical basis for the MOF Agent injection mechanism. It is believed that the small partners have already had certain conjectures about the MOF Agent injection mechanism.

Fig. 2 is a component diagram of MOF Agent, which is implemented based on java agent premain technology and intercepts all loaded Class bytecode file streams. Byte code hijacking and conversion are carried out through UAV’s transformer(MOFClsTransformer). UAV not only does this, but also can automatically sense different application servers and inject cut-off points into important positions in the life cycle of different application servers. UAV adapts different application services through Adaptor, and implements specific tangent point injection through interceptor.

uav2.png

Figure 2 MOF Agent component diagram

The MOFAgent injection mechanism will inject cut-off points into key locations in the application server life cycle, providing a basis for MOF framework initialization, application portrait information and real-time monitoring data information capture. Different tangent points injected by MOF Agent will produce different events, which will drive the subsequent MOF framework. The main tangents supported by MOF are as follows:

Application server portal

Applying the server entry tangent point, UAV will complete MOF Jar package loading and configuration file initialization;

Application server startup

When the application server is started, it must be ensured that the UAV MOF will complete the self-starting and initialization of the MOF internal code along with the application server startup.

Application Server Request Processing and Reply

The cut-off point of application server request and reply is an important cut-off point for UAV to capture application real-time monitoring data. It monitors the performance index of application server, application and all URL.

Application initialization

When applying the initialization tangent point, UAV will rewrite the applied Filter to support MOF’s Global Filter mechanism; At the same time, the capture of application portrait information is completed.

Application stop

When the cut-off point is applied when stopping, the UAV will complete operations such as stopping MOF-related mechanisms.

The main purpose of this article is to let readers understand the principle and related implementation of UAV MOF Agent code injection mechanism. Other important frameworks in MOF and their implementation principles will be analyzed in subsequent articles. Please look forward to it.
Yixin Institute of Technology