wu-sheng commented on issue #3152: [Don't merge] Set up the bootstrap 
instrumentation plugin framework
URL: https://github.com/apache/skywalking/pull/3152#issuecomment-514252426
 
 
   If you want to understand how I do this, follow `BootstrapInstrumentBoost` 
class, it will link the things together. Also, plugin interceptor point 
definitions have been as public, as `BootstrapInstrumentBoost` needs to access 
it.
   
   Also, I add a demo `jre-HttpUrlConnection` plugin for demo, and instrumen 
the following application codes.
   ```java
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStreamReader;
   import java.net.HttpURLConnection;
   import java.net.URL;
   
   public class Main {
       public static void main(String[] args) throws IOException {
           URL obj = new URL("http://www.baidu.com";);
           HttpURLConnection con = (HttpURLConnection)obj.openConnection();
   
           con.setRequestMethod("GET");
   
           int responseCode = con.getResponseCode();
           System.out.println("Response Code : " + responseCode);
   
           BufferedReader in = new BufferedReader(
               new InputStreamReader(con.getInputStream()));
           String inputLine;
           StringBuffer response = new StringBuffer();
   
           while ((inputLine = in.readLine()) != null) {
               response.append(inputLine);
           }
           in.close();
   
           //print result
           System.out.println(response.toString());
       }
   }
   ```
   
   Like the existing plugins, the bootstrap intrumentation only requires you to 
declare it in plugin define, such as following codes I included in this PR
   ```java
   /**
    * @author wusheng
    */
   public class HttpUrlConnectionInstrumentation extends 
ClassEnhancePluginDefine {
       private static String CLASS_NAME = "java.net.HttpURLConnection";
   
        // This is the only change for plugin developer
       @Override public boolean isBootstrapInstrumentation() {
           return true;
       }
   
       @Override protected ClassMatch enhanceClass() {
           return byName(CLASS_NAME);
       }
   
       @Override public InstanceMethodsInterceptPoint[] 
getInstanceMethodsInterceptPoints() {
           return new InstanceMethodsInterceptPoint[] {
               new InstanceMethodsInterceptPoint() {
                   @Override public ElementMatcher<MethodDescription> 
getMethodsMatcher() {
                       return named("setRequestMethod");
                   }
   
                   @Override public String getMethodsInterceptor() {
                       return 
"org.apache.skywalking.apm.plugin.jre.httpurlconnection.Interceptor";
                   }
   
                   @Override public boolean isOverrideArgs() {
                       return false;
                   }
               }
           };
       }
   }
   ```
   
   Also, I haven't finished construct and static method plugin, but they are 
very similar, as we have this, they are simple. I will push those later.
   
   I hope you could test this new mechanism in other demo codes, Spring Boot 
and Tomcat especially, in case I miss anything.
   
   @ascrutae Please lead the tests.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to