-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set up the bootstrap instrumentation plugin framework #3152
Conversation
If you want to understand how I do this, follow Also, I add a demo 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 /**
* @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. |
…pports JDK 1.6 compile, according to https://github.com/jboss-javassist/javassist/blob/rel_3_23_2_ga/pom.xml#L149-L160
This PR is ready to review and test. I have used it to instrument URL and HttpURLConnection successfully. But just a demo code, of course. Part of my test codes are posted here, https://gist.github.com/wu-sheng/25a8877971db089e196ada0fe4395813 In this PR, there is no real plugin. But further pull request is welcome. |
I have tested in but I debug found, eg. I think |
You do the tests quickly :) Thanks. Load the generated class to class loader w/o initialization should be safe. That is why I use This is my guess and understanding from test results, I used to try |
@candyleer If you are OK, please approve this PR. |
is this file ( |
Yes. Removing it. |
@candyleer Have you tested Tomcat? And JDK 6? |
/run ci |
I have test Tomcat .but JDK 6 not test ,maybe tomorrow I can run a test manually. |
Look like javassist has an error. |
I have tested ,javassist v3.21.0-GA is the last version support jdk6,and we can work well by using it |
@AllArguments Object[] allArguments) { | ||
try { | ||
EnhancedInstance targetObject = (EnhancedInstance)obj; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should call prepare()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
apm-sniffer/apm-agent-core/pom.xml
Outdated
@@ -37,6 +38,7 @@ | |||
<grpc.version>1.14.0</grpc.version> | |||
<guava.version>20.0</guava.version> | |||
<bytebuddy.version>1.9.2</bytebuddy.version> | |||
<javaassist.version>3.23.2-GA</javaassist.version> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.21.0-GA support JDK6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I am doing downgrade
* Prepare the context. Link to the agent core in AppClassLoader. | ||
*/ | ||
private static void prepare() { | ||
if (INTERCEPTOR == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the INTERCPETOR
Initialization shoud be synchronized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't' have to. Create multiple instances is still safe. This could just avoid creating too many instances, and also avoid the lock.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
@candyleer Updated. |
@ascrutae Please run our autotests, let's see whether this breaks anything. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BootstrapPluginLogBridge method not implemented
* | ||
* @author wusheng | ||
*/ | ||
public class BootstrapPluginLogBridge implements IBootstrapLog { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the method in this class should be implemented, or the log will be NoopLog
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
} | ||
|
||
@Override public void info(String format, Object... arguments) { | ||
logger.info(format, arguments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@candyleer Could you test this log method? I am little concerned about arguments(Object...) could work or not. I haven't the test codes at my hand now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could simple call log("", arg1, arg2) in InstanceMethodInterTemplate, and see whether log works well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK ,I just tested info(String format)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure you use args list, no matter which method you test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test passed
/run ci |
1 similar comment
/run ci |
From the test report, it is same as before. After CI pass, I will merge this to the core. The new plugin is welcome, especially for HttpUrlConnection. |
Hi, All
This PR is following #2932, in these days, I work on building the bootstrap instrumentation plugin framework by not breaking any existing plugin. Look like it works.
For plugin developer, one line required only in plugin define.