Recording online traffic using Java Agent and instrument technology __java

Source: Internet
Author: User
Tags java se

Using Java Agent and instrument technology to record online traffic Java instrument Technology encountered problems automatic packaging dependency classnotfound problem httpservletrequest body can only get once

recording online traffic using Java Agent and instrument technology

In the performance of the pressure test, you need to prepare the pressure test data, you can use the manual manufacturing method, can also record the flow online, the line next. Here, we use the Java Agent and instrument technology, a proxy agent implemented without modifying the code can be recorded online request data function. Java Instrument Technology

Java instrument Technology How to use here is not repeated, online article a lot, you can look at this: Java SE 6 new features instrumentation new features. the problems encountered

Let's talk a little bit about the challenges involved in developing the Agent. Automatic Packaging dependencies

The recording agent depends on some of the packages, also to be packaged together, otherwise, when loading agent execution will not find the problem of the class. Specifically how to do it. You can add the following configuration to the Pmo.xml:

            <plugin> <groupId>org.apache.maven.plugins</groupId> <art Ifactid>maven-assembly-plugin</artifactid> <configuration> <archive&
                        Gt <!--manifestentries is used to configure package generation MANIFEST.
                            MF file Required Description information, if not configured below, the JVM will not find the Agent class portal--> <manifestEntries> <Premain-Class>Agent</Premain-Class> &LT;AGENT-CLASS&GT;AGENT&LT;/AGENT-CLASS&G
                            T <Can-Redefine-Classes>true</Can-Redefine-Classes> <can-retransform-classes>t
                    Rue</can-retransform-classes> </manifestEntries> </archive> <descriptorRefs> <descriptorref>jar-with-dependencies</descript Orref> </descriptorrefs> </configuration> <executions> <execution>
                        <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </ goals> </execution> </executions> </plugin>
classnotfound Problem

Our systems are basically developed with the spring framework, and when we record HTTP requests, we intercept the Dispatcherservlet.dodispatch class in spring that distributes requests, and get httpservletrequest at the entrance of the method and outputs the data to the storage service where the data is requested. When outputting the data, the record method of a recorder class in the recording Agent is called in the Dispatcherservlet:

public class Recorder {
    private static final Logger Logger = Loggerfactory.getlogger (recorder.class);

    The public static void record (HttpServletRequest request) {
        //Gets the HTTP body, Parametermap, cookie, and storage
    }
from request }

During the actual operation, we found that the HttpServletRequest class was always not found when executing the Recorder.record method. This class should be tomcat default loaded, why not find it.

After some research, it is found that the problem is on the ClassLoader mechanism of Java. In Java, ClassLoader is a multi-level parent-child structure, and the child ClassLoader can use a class that is loaded by the parent ClassLoader, but the reverse is not. You can look at the picture below, recorder in the Appclassloader, and HttpServletRequest loaded in URLClassLoader, and Appclassloader is the parent of URLClassLoader. So recorder can not see httpservletrequest, then how to do it.

Initially thought, can be in the Agent with Servlet.api, let Appclassloader also loaded again httpservletrequest, there are two problems: repeated loading classes, resulting in a system bloated unreasonable. Servlet.api is a Tomcat API, the version that is brought in by the Agent often does not fit the corresponding Tomcat, which may cause a Tomcat boot exception.

What else can I do? At this time, we thought of URLClassLoader has a Addurl interface, you can add a new class library. So if we let urlclassloader to load the recorder class, the same can be done to let recorder access httpservletrequest.

By splitting the original traffic recording agent into two packages, the new recording agent contains only the Agentmain and instrument transform related classes and puts the recorder data collection class into the other package client. Add code to the new recording Agent find URLClassLoader Call the Addurl method to load the client package. (with WebappClassLoader load the client package can also achieve the purpose) HttpServletRequest body can only get one time

When the HttpServletRequest getInputStream method is invoked to read the data, it can cause nothing to be read again at the Controller end. You need to read the HTTP body, and then forge a httpservletrequest backward delivery, the specific way to see: The solution in the filter read in the request in the stream, and then read the controller in the practice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.