Background
With the rapid development of enterprises, related businesses will become increasingly complex, and accordingly, a technical team is required to refine more micro-services to meet the increasingly complex requirements. Meanwhile, the problems of request statistics, link topology, application dependency, failure diagnosis efficiency and the like of each application need to be faced, so that a set of monitoring components needs to be developed, implanted into the application, collect data during program operation, and restore the program operation condition.
Based on the requirements, the AOP section-oriented programming idea can be adopted, and the interception of the operation method is realized through dynamic proxy in the program operation period, so that the effect of acquiring program operation data is achieved.
However, in the AOP dynamic proxy technology, the proxy object is dynamically generated based on the original java object during the operation period to achieve the effect of intercepting the operation method. Under the condition that different methods of the same java object are mutually called, the interception effect is invalid because the dynamic proxy object is not called, and the running data of the acquisition program is lost.
Disclosure of Invention
The invention aims to provide a java code tracking type-based data acquisition method, which aims to solve the problems that the interception effect is invalid and the operation data of an acquisition program is lost due to the fact that dynamic proxy object calling is not performed in the background technology.
In order to achieve the purpose, the invention provides the following technical scheme:
a java code tracking type based data acquisition method comprises the following steps:
s1: defining a spi interceptor interface: defining a uniform spi interceptor interface by adopting an spi plug-in mechanism, and realizing different spi interceptor interfaces by different enhancement logics;
s2: defining a spi interceptor scheduler, providing an @ RuntimeType annotation entry;
s3: defining an agent entry: in the agent's domain method, loading the class realized by the spi interceptor through the ServiceLoader, and circulating each spi interceptor to realize the class;
s4: constructing a Transformer: injecting the spi interceptor into an interceptor scheduler, and then injecting the spi interceptor into a Transformer;
s5: constructing an AgentBuilder, and injecting a Transformer into the AgentBuilder to realize request statistics enhancement, link topology enhancement and remote call enhancement;
s6: packing a java-shadow-plugin into a java jar;
s7: and modifying the application starting command, and adding the java-java agent jar package full-path starting parameters to acquire the application data based on the java code tracking type.
Furthermore, in step S1, for defining a uniform dynamic code enhancement spi interceptor interface, pre-execution, post-execution, exception handling, class matching, and method matching are provided.
Further, the specific steps in S2 are: and injecting the spi interceptor into the scheduler, and in an execution method of the scheduler, identifying the @ RuntimeType annotation by a java reflection technology and combining a front-end execution interception method and a rear-end execution interception method of the spi interceptor to realize front-end and rear-end enhancement of the target bytecode.
Further, the specific method in S6 is: and (3) adopting a maven-shade-plug-in, and designating the pre-class as a pre-main full path of S3, thereby realizing packaging the current engineering code into a javaagent jar.
Furthermore, the java-java agent jar package full path startup parameters in S7 include java-java agent:/data/agent.
Compared with the prior art, the invention has the beneficial effects that:
the invention provides a java code tracking type-based data acquisition method, which adopts a java byte code enhancement technology to dynamically enhance byte codes in the byte code loading period, solves the problem of agent failure caused by mutual calling of the same object and the same method of an AOP dynamic agent technology, has no invasion and no perception to an application developer, and completes application data acquisition on the premise of not modifying any application code.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Referring to fig. 1, an embodiment of the present invention provides a java code tracking type-based data acquisition method, including the following steps:
the first step is as follows: defining a spi interceptor interface: defining a uniform spi interceptor interface by adopting an spi plug-in mechanism, and realizing different spi interceptor interfaces by different enhancement logics; wherein, for defining a uniform dynamic code enhancement spi interceptor interface, pre-execution, post-execution, exception handling, class matching and method matching are provided in a matching way;
the second step is that: defining a spi interceptor scheduler, providing an @ RuntimeType annotation entry;
the third step: defining an agent entry: in the agent's domain method, loading the class realized by the spi interceptor through the ServiceLoader, and circulating each spi interceptor to realize the class; specifically, an spi interceptor is injected into the scheduler, and in an execution method of the scheduler, a java reflection technology is used for identifying @ RuntimeType annotation and combining a front-end execution interception method and a rear-end execution interception method of the spi interceptor, so that front-end and rear-end enhancement of a target byte code is realized;
the fourth step: constructing a Transformer: injecting the spi interceptor into an interceptor scheduler, and then injecting the spi interceptor into a Transformer;
the fifth step: constructing an AgentBuilder, and injecting a Transformer into the AgentBuilder to realize request statistics enhancement, link topology enhancement and remote call enhancement;
and a sixth step: packing a java-shadow-plugin into a java jar; the method specifically comprises the steps that a maven-shadow-plug-in is adopted, a pre-class is designated as a pre-main full path of S3, and therefore the current engineering code is packaged into a java jar;
the seventh step: the method comprises the steps that a start command of application is modified, and java-java jar package full-path start parameters are added to achieve collection of application data based on a java code tracking type; the java-java agent jar package full path starting parameter comprises java-java agent:/data/agent.
The working principle is as follows: the invention provides a java code tracking type-based data acquisition method, which adopts a technical means to dynamically enhance java byte codes, and generally has more than single enhancement logic for the dynamic enhancement of the java byte codes, such as request statistics enhancement, link topology enhancement, remote call enhancement and the like, so that the embodiment of the invention adopts a spi plug-in mechanism to define a uniform spi interceptor interface, and different enhancement logics realize different spi interceptor interfaces.
Defining a uniform dynamic code enhanced spi interceptor interface, and providing methods such as pre-execution, post-execution, exception handling, class matching, method matching and the like; in the agent's premier method, loading all spi interceptor implementation classes through a ServiceLoader, circulating each spi interceptor implementation class, constructing a corresponding Transformer, injecting the spi interceptor into the Transformer, then constructing an AgentBuilder, and injecting the Transformer into the AgentBuilder; it should be noted that, in the method, an interceptor scheduler needs to be defined, and the spi interceptor is injected into the scheduler, and in the execution method of the scheduler, the @ RuntimeType annotation is identified by the java reflection technology, and the method of pre-execution interception and post-execution interception of the spi interceptor are combined, so as to implement pre-and post-enhancement of the target bytecode.
Meanwhile, it is also noted that in the method of the present invention, because the responsibilities of different interceptors are different, and the classes and methods that they need to intercept are also different, when constructing the Transformer and the AgentBuilder, only the assigned classes and methods of the interceptors will be matched, and no interception will be performed on other classes and methods, thereby implementing an overall framework for dynamically increasing java byte codes, according to this framework, the present invention can implement spi interceptors such as request statistics enhancement, link topology enhancement, remote invocation enhancement, etc. according to the actual requirements, and complete a java code tracking type based data acquisition monitoring component; the specific method comprises the following steps: adopting a maven-shade-plug-in, and designating the pre-class as a pre-main full path, thus realizing packaging the current engineering code into a java jar; modifying the starting command of the application, and adding the full path starting parameters of the java-java agent jar package, such as: java-java agent/data/agent.jar-jar app.jar, and the method can be used for realizing application data acquisition based on java code tracking.
In summary, the following steps: the invention provides a java code tracking type-based data acquisition method, which adopts a java byte code enhancement technology to dynamically enhance byte codes in the byte code loading period, solves the problem of agent failure caused by mutual calling of the same object and the same method of an AOP dynamic agent technology, has no invasion and no perception to an application developer, and completes application data acquisition on the premise of not modifying any application code.
The above description is only for the preferred embodiment of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art should be able to cover the technical solutions and the inventive concepts of the present invention within the technical scope of the present invention.