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 2021/10/13 14:56:47 UTC

[skywalking-java] branch main updated: Fix instrumentation v2 API doesn't work for constructor instrumentation. (#51)

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-java.git


The following commit(s) were added to refs/heads/main by this push:
     new 71dd60d  Fix instrumentation v2 API doesn't work for constructor instrumentation. (#51)
71dd60d is described below

commit 71dd60d5e8ef4b95defda450ee0a795bb07e79ca
Author: 吴晟 Wu Sheng <wu...@foxmail.com>
AuthorDate: Wed Oct 13 22:56:38 2021 +0800

    Fix instrumentation v2 API doesn't work for constructor instrumentation. (#51)
---
 CHANGES.md                                         |  1 +
 .../enhance/v2/ClassEnhancePluginDefineV2.java     | 28 +++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/CHANGES.md b/CHANGES.md
index 28b587b..5e977cd 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -36,6 +36,7 @@ Release Notes.
 * Add plugin to support ClickHouse JDBC driver.
 * Fix version compatibility for JsonRPC4J plugin.
 * Add plugin to support Apache Kylin-jdbc 2.6.x 3.x 4.x
+* Fix instrumentation v2 API doesn't work for constructor instrumentation.
 
 #### Documentation
 
diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/v2/ClassEnhancePluginDefineV2.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/v2/ClassEnhancePluginDefineV2.java
index 459e150..ff26034 100644
--- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/v2/ClassEnhancePluginDefineV2.java
+++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/interceptor/enhance/v2/ClassEnhancePluginDefineV2.java
@@ -22,6 +22,7 @@ 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.SuperMethodCall;
 import net.bytebuddy.implementation.bind.annotation.Morph;
 import net.bytebuddy.matcher.ElementMatcher;
 import net.bytebuddy.matcher.ElementMatchers;
@@ -29,9 +30,11 @@ import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDef
 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.bootstrap.BootstrapInstrumentBoost;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.EnhanceException;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint;
+import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ConstructorInter;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.OverrideCallable;
 import org.apache.skywalking.apm.agent.core.plugin.interceptor.v2.ConstructorInterceptV2Point;
@@ -105,14 +108,20 @@ public abstract class ClassEnhancePluginDefineV2 extends AbstractClassEnhancePlu
     protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription,
                                                      DynamicType.Builder<?> newClassBuilder, ClassLoader classLoader,
                                                      EnhanceContext context) throws PluginException {
+        ConstructorInterceptPoint[] constructorInterceptPoints = getConstructorsInterceptPoints();
         InstanceMethodsInterceptV2Point[] instanceMethodsInterceptV2Points = getInstanceMethodsInterceptV2Points();
         String enhanceOriginClassName = typeDescription.getTypeName();
+
+        boolean existedConstructorInterceptPoint = false;
+        if (constructorInterceptPoints != null && constructorInterceptPoints.length > 0) {
+            existedConstructorInterceptPoint = true;
+        }
         boolean existedMethodsInterceptV2Points = false;
         if (instanceMethodsInterceptV2Points != null && instanceMethodsInterceptV2Points.length > 0) {
             existedMethodsInterceptV2Points = true;
         }
 
-        if (!existedMethodsInterceptV2Points) {
+        if (!existedConstructorInterceptPoint && !existedMethodsInterceptV2Points) {
             return newClassBuilder;
         }
 
@@ -126,6 +135,23 @@ public abstract class ClassEnhancePluginDefineV2 extends AbstractClassEnhancePlu
             }
         }
 
+        if (existedConstructorInterceptPoint) {
+            for (ConstructorInterceptPoint constructorInterceptPoint : constructorInterceptPoints) {
+                if (isBootstrapInstrumentation()) {
+                    newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher())
+                                                     .intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.withDefaultConfiguration()
+                                                                                                                 .to(BootstrapInstrumentBoost
+                                                                                                                         .forInternalDelegateClass(constructorInterceptPoint
+                                                                                                                                                       .getConstructorInterceptor()))));
+                } else {
+                    newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher())
+                                                     .intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.withDefaultConfiguration()
+                                                                                                                 .to(new ConstructorInter(constructorInterceptPoint
+                                                                                                                                              .getConstructorInterceptor(), classLoader))));
+                }
+            }
+        }
+
         if (existedMethodsInterceptV2Points) {
             for (InstanceMethodsInterceptV2Point instanceMethodsInterceptV2Point : instanceMethodsInterceptV2Points) {
                 String interceptor = instanceMethodsInterceptV2Point.getMethodsInterceptorV2();