You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/07/23 02:09:15 UTC
[skywalking] branch bootstrap-cl updated: Refactor codes
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch bootstrap-cl
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/bootstrap-cl by this push:
new dffc9e3 Refactor codes
dffc9e3 is described below
commit dffc9e3fec928a135b63ee39410f41fc5535ec4a
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Jul 22 23:01:26 2019 +0800
Refactor codes
---
.../apm/agent/core/plugin/PluginFinder.java | 17 ++-
.../enhance/ClassEnhancePluginDefine.java | 45 ++----
.../BootstrapClassEnhancePluginDefine.java | 162 +++++++++++++++++++++
.../{ => bootstrap}/BootstrapInstMethodsInter.java | 2 +-
.../BootstrapInstanceMethodsInterceptPoint.java | 43 ++++++
.../skywalking/apm/agent/SkyWalkingAgent.java | 74 ++++++----
.../HttpUrlConnectionInstrumentation.java | 29 ++--
.../plugin/jre/httpurlconnection/Interceptor.java} | 9 +-
.../jre/httpurlconnection/define/Interceptor.java | 51 -------
9 files changed, 299 insertions(+), 133 deletions(-)
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java
index 5beed29..84a5a94 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/PluginFinder.java
@@ -19,6 +19,7 @@
package org.apache.skywalking.apm.agent.core.plugin;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
@@ -27,6 +28,7 @@ import net.bytebuddy.description.NamedElement;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.skywalking.apm.agent.core.plugin.bytebuddy.AbstractJunction;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap.BootstrapClassEnhancePluginDefine;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.IndirectMatch;
import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch;
@@ -36,14 +38,15 @@ import static net.bytebuddy.matcher.ElementMatchers.isInterface;
import static net.bytebuddy.matcher.ElementMatchers.not;
/**
- * The <code>PluginFinder</code> represents a finder , which assist to find the one
- * from the given {@link AbstractClassEnhancePluginDefine} list.
+ * The <code>PluginFinder</code> represents a finder , which assist to find the one from the given {@link
+ * AbstractClassEnhancePluginDefine} list.
*
* @author wusheng
*/
public class PluginFinder {
private final Map<String, LinkedList<AbstractClassEnhancePluginDefine>> nameMatchDefine = new HashMap<String, LinkedList<AbstractClassEnhancePluginDefine>>();
- private final List<AbstractClassEnhancePluginDefine> signatureMatchDefine = new LinkedList<AbstractClassEnhancePluginDefine>();
+ private final List<AbstractClassEnhancePluginDefine> signatureMatchDefine = new ArrayList<AbstractClassEnhancePluginDefine>();
+ private final List<BootstrapClassEnhancePluginDefine> bootstrapClassMatchDefine = new ArrayList<BootstrapClassEnhancePluginDefine>();
public PluginFinder(List<AbstractClassEnhancePluginDefine> plugins) {
for (AbstractClassEnhancePluginDefine plugin : plugins) {
@@ -64,6 +67,10 @@ public class PluginFinder {
} else {
signatureMatchDefine.add(plugin);
}
+
+ if (plugin instanceof BootstrapClassEnhancePluginDefine) {
+ bootstrapClassMatchDefine.add((BootstrapClassEnhancePluginDefine)plugin);
+ }
}
}
@@ -100,4 +107,8 @@ public class PluginFinder {
}
return new ProtectiveShieldMatcher(judge);
}
+
+ public List<BootstrapClassEnhancePluginDefine> getBootstrapClassMatchDefine() {
+ return bootstrapClassMatchDefine;
+ }
}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
index ddefb33..fe0e09b 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/ClassEnhancePluginDefine.java
@@ -119,12 +119,10 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
*
*/
if (!context.isObjectExtended()) {
- if (!instrumentJDKClass()) {
- newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, Object.class, ACC_PRIVATE | ACC_VOLATILE)
- .implement(EnhancedInstance.class)
- .intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
- context.extendObjectCompleted();
- }
+ newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, Object.class, ACC_PRIVATE | ACC_VOLATILE)
+ .implement(EnhancedInstance.class)
+ .intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
+ context.extendObjectCompleted();
}
/**
@@ -153,32 +151,23 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
if (instanceMethodsInterceptPoint instanceof DeclaredInstanceMethodsInterceptPoint) {
junction = junction.and(ElementMatchers.<MethodDescription>isDeclaredBy(typeDescription));
}
- if (instrumentJDKClass()) {
+ if (instanceMethodsInterceptPoint.isOverrideArgs()) {
newClassBuilder =
newClassBuilder.method(junction)
.intercept(
MethodDelegation.withDefaultConfiguration()
- .to(BootstrapInstMethodsInter.class)
+ .withBinders(
+ Morph.Binder.install(OverrideCallable.class)
+ )
+ .to(new InstMethodsInterWithOverrideArgs(interceptor, classLoader))
);
} else {
- if (instanceMethodsInterceptPoint.isOverrideArgs()) {
- newClassBuilder =
- newClassBuilder.method(junction)
- .intercept(
- MethodDelegation.withDefaultConfiguration()
- .withBinders(
- Morph.Binder.install(OverrideCallable.class)
- )
- .to(new InstMethodsInterWithOverrideArgs(interceptor, classLoader))
- );
- } else {
- newClassBuilder =
- newClassBuilder.method(junction)
- .intercept(
- MethodDelegation.withDefaultConfiguration()
- .to(new InstMethodsInter(interceptor, classLoader))
- );
- }
+ newClassBuilder =
+ newClassBuilder.method(junction)
+ .intercept(
+ MethodDelegation.withDefaultConfiguration()
+ .to(new InstMethodsInter(interceptor, classLoader))
+ );
}
}
}
@@ -200,10 +189,6 @@ public abstract class ClassEnhancePluginDefine extends AbstractClassEnhancePlugi
*/
protected abstract InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints();
- protected boolean instrumentJDKClass() {
- return false;
- }
-
/**
* Enhance a class to intercept class static methods.
*
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapClassEnhancePluginDefine.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapClassEnhancePluginDefine.java
new file mode 100644
index 0000000..60f694f
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapClassEnhancePluginDefine.java
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap;
+
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.dynamic.DynamicType;
+import net.bytebuddy.implementation.FieldAccessor;
+import net.bytebuddy.implementation.MethodDelegation;
+import net.bytebuddy.implementation.bind.annotation.Morph;
+import net.bytebuddy.matcher.ElementMatcher;
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext;
+import org.apache.skywalking.apm.agent.core.plugin.PluginException;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.DeclaredInstanceMethodsInterceptPoint;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.EnhanceException;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.OverrideCallable;
+
+import static net.bytebuddy.jar.asm.Opcodes.ACC_PRIVATE;
+import static net.bytebuddy.jar.asm.Opcodes.ACC_VOLATILE;
+import static net.bytebuddy.matcher.ElementMatchers.isStatic;
+import static net.bytebuddy.matcher.ElementMatchers.not;
+import static org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassEnhancePluginDefine.CONTEXT_ATTR_NAME;
+
+/**
+ * This plugin define explicitly the instrumentation for JRE classes(mostly rt.jar). JRE class Instrumentation is more
+ * class loader sensitive than other framework class instrumentation.
+ *
+ * The ways to instrument are more original, include more coding requirements.
+ *
+ * @author wusheng
+ */
+public abstract class BootstrapClassEnhancePluginDefine extends AbstractClassEnhancePluginDefine {
+ @Override
+ protected DynamicType.Builder<?> enhance(TypeDescription typeDescription, DynamicType.Builder<?> newClassBuilder,
+ ClassLoader classLoader, EnhanceContext context) throws PluginException {
+ newClassBuilder = this.enhanceInstance(typeDescription, newClassBuilder, classLoader, context);
+
+ return newClassBuilder;
+ }
+
+ /**
+ * Enhance a class to intercept constructors and class instance methods.
+ *
+ * @param typeDescription target class description
+ * @param newClassBuilder byte-buddy's builder to manipulate class bytecode.
+ * @return new byte-buddy's builder for further manipulation.
+ */
+ private DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription,
+ DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
+ EnhanceContext context) throws PluginException {
+// ConstructorInterceptPoint[] constructorInterceptPoints = getConstructorsInterceptPoints();
+ BootstrapInstanceMethodsInterceptPoint[] instanceMethodsInterceptPoints = getInstanceMethodsInterceptPoints();
+ String enhanceOriginClassName = typeDescription.getTypeName();
+ boolean existedConstructorInterceptPoint = false;
+// if (constructorInterceptPoints != null && constructorInterceptPoints.length > 0) {
+// existedConstructorInterceptPoint = true;
+// }
+ boolean existedMethodsInterceptPoints = false;
+ if (instanceMethodsInterceptPoints != null && instanceMethodsInterceptPoints.length > 0) {
+ existedMethodsInterceptPoints = true;
+ }
+
+ /**
+ * nothing need to be enhanced in class instance, maybe need enhance static methods.
+ */
+ if (!existedConstructorInterceptPoint && !existedMethodsInterceptPoints) {
+ return newClassBuilder;
+ }
+
+ /**
+ * Manipulate class source code.<br/>
+ *
+ * new class need:<br/>
+ * 1.Add field, name {@link #CONTEXT_ATTR_NAME}.
+ * 2.Add a field accessor for this field.
+ *
+ * And make sure the source codes manipulation only occurs once.
+ *
+ */
+ if (!context.isObjectExtended()) {
+ newClassBuilder = newClassBuilder.defineField(CONTEXT_ATTR_NAME, Object.class, ACC_PRIVATE | ACC_VOLATILE)
+ .implement(EnhancedInstance.class)
+ .intercept(FieldAccessor.ofField(CONTEXT_ATTR_NAME));
+ context.extendObjectCompleted();
+ }
+
+ /**
+ * 2. enhance constructors
+ */
+// if (existedConstructorInterceptPoint) {
+// for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) {
+// newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher()).intercept(SuperMethodCall.INSTANCE
+// .andThen(MethodDelegation.withDefaultConfiguration()
+// .to(new ConstructorInter(constructorInterceptPoint.getConstructorInterceptor(), classLoader))
+// )
+// );
+// }
+// }
+
+ /**
+ * 3. enhance instance methods
+ */
+ if (existedMethodsInterceptPoints) {
+ for (BootstrapInstanceMethodsInterceptPoint instanceMethodsInterceptPoint : instanceMethodsInterceptPoints) {
+ Class interceptor = instanceMethodsInterceptPoint.getMethodsInterceptor();
+ if (interceptor == null) {
+ throw new EnhanceException("no BootstrapInstanceMethodsInterceptPoint define to enhance class " + enhanceOriginClassName);
+ }
+
+ ElementMatcher.Junction<MethodDescription> junction = not(isStatic()).and(instanceMethodsInterceptPoint.getMethodsMatcher());
+ if (instanceMethodsInterceptPoint instanceof DeclaredInstanceMethodsInterceptPoint) {
+ junction = junction.and(ElementMatchers.<MethodDescription>isDeclaredBy(typeDescription));
+ }
+ if (instanceMethodsInterceptPoint.isOverrideArgs()) {
+ newClassBuilder =
+ newClassBuilder.method(junction)
+ .intercept(
+ MethodDelegation.withDefaultConfiguration()
+ .withBinders(
+ Morph.Binder.install(OverrideCallable.class)
+ )
+ .to(interceptor)
+ );
+ } else {
+ newClassBuilder =
+ newClassBuilder.method(junction)
+ .intercept(
+ MethodDelegation.withDefaultConfiguration()
+ .to(interceptor)
+ );
+ }
+ }
+ }
+
+ return newClassBuilder;
+ }
+
+ private void checkInstanceInterceptor(Class interceptor) {
+ //TODO
+ }
+
+ public abstract BootstrapInstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints();
+}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstMethodsInter.java
similarity index 99%
copy from apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java
copy to apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstMethodsInter.java
index 5f0b623..6da7a6b 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstMethodsInter.java
@@ -16,7 +16,7 @@
*
*/
-package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance;
+package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstanceMethodsInterceptPoint.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstanceMethodsInterceptPoint.java
new file mode 100644
index 0000000..44d043d
--- /dev/null
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/bootstrap/BootstrapInstanceMethodsInterceptPoint.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap;
+
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+
+/**
+ * Very similar to InstanceMethodsInterceptPoint, but due to do JVM instrumentation, a ByteBuddy interceptor required.
+ *
+ * @author wusheng
+ */
+public interface BootstrapInstanceMethodsInterceptPoint {
+ /**
+ * class instance methods matcher.
+ *
+ * @return methods matcher
+ */
+ ElementMatcher<MethodDescription> getMethodsMatcher();
+
+ /**
+ * @return class will use as interceptor, this Class need to follow ByteBuddy static @RuntimeType interceptor usage.
+ */
+ Class getMethodsInterceptor();
+
+ boolean isOverrideArgs();
+}
diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
index ca15216..b2503ff 100644
--- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
+++ b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java
@@ -48,8 +48,9 @@ import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext;
import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap;
import org.apache.skywalking.apm.agent.core.plugin.PluginException;
import org.apache.skywalking.apm.agent.core.plugin.PluginFinder;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.BootstrapInstMethodsInter;
import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap.BootstrapClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap.BootstrapInstanceMethodsInterceptPoint;
import static net.bytebuddy.matcher.ElementMatchers.nameContains;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
@@ -78,39 +79,20 @@ public class SkyWalkingAgent {
pluginFinder = new PluginFinder(new PluginBootstrap().loadPlugins());
} catch (ConfigNotFoundException ce) {
- logger.error(ce, "Skywalking agent could not find config. Shutting down.");
+ logger.error(ce, "SkyWalking agent could not find config. Shutting down.");
return;
} catch (AgentPackageNotFoundException ape) {
logger.error(ape, "Locate agent.jar failure. Shutting down.");
return;
} catch (Exception e) {
- logger.error(e, "Skywalking agent initialized failure. Shutting down.");
+ logger.error(e, "SkyWalking agent initialized failure. Shutting down.");
return;
}
- File temp = null;
- try {
- temp = new File(AgentPackagePath.getPath(), "logs");
- } catch (AgentPackageNotFoundException e) {
- logger.error(e, "Bootstrap temp path can't be found.");
- return;
- }
- Map<TypeDescription.ForLoadedType, byte[]> loadedTypeMap = new HashMap<TypeDescription.ForLoadedType, byte[]>();
- loadedTypeMap.put(
- new TypeDescription.ForLoadedType(BootstrapInstMethodsInter.class),
- ClassFileLocator.ForClassLoader.read(BootstrapInstMethodsInter.class)
- );
- loadedTypeMap.put(new TypeDescription.ForLoadedType(EnhancedInstance.class),
- ClassFileLocator.ForClassLoader.read(BootstrapInstMethodsInter.class));
- loadedTypeMap.put(new TypeDescription.ForLoadedType(BootstrapInstMethodsInter.class),
- ClassFileLocator.ForClassLoader.read(BootstrapInstMethodsInter.class));
-
- ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, instrumentation).inject(loadedTypeMap);
-
final ByteBuddy byteBuddy = new ByteBuddy()
.with(TypeValidation.of(Config.Agent.IS_OPEN_DEBUGGING_CLASS));
- new AgentBuilder.Default(byteBuddy)
+ AgentBuilder agentBuilder = new AgentBuilder.Default(byteBuddy)
.ignore(
nameStartsWith("net.bytebuddy.")
.or(nameStartsWith("org.slf4j."))
@@ -120,8 +102,16 @@ public class SkyWalkingAgent {
.or(nameContains(".asm."))
.or(nameStartsWith("sun.reflect"))
.or(allSkyWalkingAgentExcludeToolkit())
- .or(ElementMatchers.<TypeDescription>isSynthetic()))
- .enableBootstrapInjection(instrumentation, temp)
+ .or(ElementMatchers.<TypeDescription>isSynthetic()));
+
+ try {
+ injectBootstrapInstrumentation(pluginFinder, agentBuilder, instrumentation);
+ } catch (Exception e) {
+ logger.error(e, "SkyWalking agent inject boostrap instrumentation failure. Shutting down.");
+ return;
+ }
+
+ agentBuilder
.type(pluginFinder.buildMatch())
.transform(new Transformer(pluginFinder))
.with(new Listener())
@@ -172,6 +162,40 @@ public class SkyWalkingAgent {
}
}
+ private static AgentBuilder injectBootstrapInstrumentation(PluginFinder pluginFinder, AgentBuilder agentBuilder,
+ Instrumentation instrumentation) {
+ Map<TypeDescription.ForLoadedType, byte[]> loadedTypeMap = new HashMap<TypeDescription.ForLoadedType, byte[]>();
+
+ boolean isHasJREInstrumentation = false;
+ for (BootstrapClassEnhancePluginDefine define : pluginFinder.getBootstrapClassMatchDefine()) {
+ for (BootstrapInstanceMethodsInterceptPoint point : define.getInstanceMethodsInterceptPoints()) {
+ Class methodsInterceptor = point.getMethodsInterceptor();
+ loadedTypeMap.put(new TypeDescription.ForLoadedType(methodsInterceptor),
+ ClassFileLocator.ForClassLoader.read(methodsInterceptor));
+ isHasJREInstrumentation = true;
+ }
+ }
+
+ if (!isHasJREInstrumentation) {
+ return agentBuilder;
+ }
+
+ loadedTypeMap.put(new TypeDescription.ForLoadedType(EnhancedInstance.class),
+ ClassFileLocator.ForClassLoader.read(EnhancedInstance.class));
+
+ File temp = null;
+ try {
+ temp = new File(AgentPackagePath.getPath(), "bootstrapJarTmp");
+ } catch (AgentPackageNotFoundException e) {
+ logger.error(e, "Bootstrap plugin exist, but SkyWalking agent can't create bootstrapJarTmp folder. Shutting down.");
+ throw new UnsupportedOperationException("Bootstrap plugin exist, but SkyWalking agent can't create bootstrapJarTmp folder. Shutting down.", e);
+ }
+
+ ClassInjector.UsingInstrumentation.of(temp, ClassInjector.UsingInstrumentation.Target.BOOTSTRAP, instrumentation).inject(loadedTypeMap);
+ agentBuilder = agentBuilder.enableBootstrapInjection(instrumentation, temp);
+ return agentBuilder;
+ }
+
private static ElementMatcher.Junction<NamedElement> allSkyWalkingAgentExcludeToolkit() {
return nameStartsWith("org.apache.skywalking.").and(not(nameStartsWith("org.apache.skywalking.apm.toolkit.")));
}
diff --git a/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/HttpUrlConnectionInstrumentation.java b/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/HttpUrlConnectionInstrumentation.java
index bd3152f..eb8e7af 100644
--- a/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/HttpUrlConnectionInstrumentation.java
+++ b/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/HttpUrlConnectionInstrumentation.java
@@ -20,9 +20,8 @@ package org.apache.skywalking.apm.plugin.jre.httpurlconnection;
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap.BootstrapClassEnhancePluginDefine;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.bootstrap.BootstrapInstanceMethodsInterceptPoint;
import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -31,22 +30,22 @@ import static org.apache.skywalking.apm.agent.core.plugin.match.NameMatch.byName
/**
* @author wusheng
*/
-public class HttpUrlConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {
+public class HttpUrlConnectionInstrumentation extends BootstrapClassEnhancePluginDefine {
private static String CLASS_NAME = "java.net.HttpURLConnection";
- @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
- return new ConstructorInterceptPoint[0];
+ @Override protected ClassMatch enhanceClass() {
+ return byName(CLASS_NAME);
}
- @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
- return new InstanceMethodsInterceptPoint[] {
- new InstanceMethodsInterceptPoint() {
+ @Override public BootstrapInstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
+ return new BootstrapInstanceMethodsInterceptPoint[] {
+ new BootstrapInstanceMethodsInterceptPoint() {
@Override public ElementMatcher<MethodDescription> getMethodsMatcher() {
return named("setRequestMethod");
}
- @Override public String getMethodsInterceptor() {
- return "org.apache.skywalking.apm.plugin.jre.httpurlconnection.define.Interceptor";
+ @Override public Class getMethodsInterceptor() {
+ return Interceptor.class;
}
@Override public boolean isOverrideArgs() {
@@ -55,12 +54,4 @@ public class HttpUrlConnectionInstrumentation extends ClassInstanceMethodsEnhanc
}
};
}
-
- @Override protected ClassMatch enhanceClass() {
- return byName(CLASS_NAME);
- }
-
- @Override protected boolean instrumentJDKClass() {
- return true;
- }
}
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java b/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/Interceptor.java
similarity index 94%
rename from apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java
rename to apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/Interceptor.java
index 5f0b623..d436e7b 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/BootstrapInstMethodsInter.java
+++ b/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/Interceptor.java
@@ -16,7 +16,7 @@
*
*/
-package org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance;
+package org.apache.skywalking.apm.plugin.jre.httpurlconnection;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
@@ -26,8 +26,10 @@ import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import net.bytebuddy.implementation.bind.annotation.This;
-public class BootstrapInstMethodsInter {
-
+/**
+ * @author wusheng
+ */
+public class Interceptor {
/**
* Intercept the target instance method.
*
@@ -49,4 +51,3 @@ public class BootstrapInstMethodsInter {
return result;
}
}
-
diff --git a/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/define/Interceptor.java b/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/define/Interceptor.java
deleted file mode 100644
index 482965d..0000000
--- a/apm-sniffer/apm-sdk-plugin/jre-HttpURLConnection/src/main/java/org/apache/skywalking/apm/plugin/jre/httpurlconnection/define/Interceptor.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package org.apache.skywalking.apm.plugin.jre.httpurlconnection.define;
-
-import java.lang.reflect.Method;
-import org.apache.skywalking.apm.agent.core.logging.api.ILog;
-import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor;
-import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult;
-
-/**
- * @author wusheng
- */
-public class Interceptor implements InstanceMethodsAroundInterceptor {
- private static final ILog logger = LogManager.getLogger(Interceptor.class);
-
- @Override
- public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- MethodInterceptResult result) throws Throwable {
-
- }
-
- @Override
- public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
- Object ret) throws Throwable {
- logger.debug("interceptor after");
- return ret;
- }
-
- @Override public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,
- Class<?>[] argumentsTypes, Throwable t) {
-
- }
-}