Skip to content
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

Improve bytebuddy class enhance for retransform classes #561

Merged
merged 24 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.skywalking.apm.agent.core.logging.core.LogOutput;
import org.apache.skywalking.apm.agent.core.logging.core.ResolverType;
import org.apache.skywalking.apm.agent.core.logging.core.WriterFactory;
import org.apache.skywalking.apm.agent.core.plugin.bytebuddy.ClassCacheMode;
import org.apache.skywalking.apm.util.Length;

/**
Expand Down Expand Up @@ -110,19 +109,6 @@ public static class Agent {
*/
public static boolean IS_OPEN_DEBUGGING_CLASS = false;

/**
* If true, SkyWalking agent will cache all instrumented classes to memory or disk files (decided by class cache
* mode), allow other javaagent to enhance those classes that enhanced by SkyWalking agent.
*/
public static boolean IS_CACHE_ENHANCED_CLASS = false;

/**
* The instrumented classes cache mode: MEMORY or FILE MEMORY: cache class bytes to memory, if instrumented
* classes is too many or too large, it may take up more memory FILE: cache class bytes in `/class-cache`
* folder, automatically clean up cached class files when the application exits
*/
public static ClassCacheMode CLASS_CACHE_MODE = ClassCacheMode.MEMORY;

/**
* The identifier of the instance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,10 @@ public class Constants {
public static String EVENT_LAYER_NAME = "GENERAL";

public static int NULL_VALUE = 0;

/**
* The name trait for auxiliary type names, field names and method names which generated by ByteBuddy.
*/
public static final String NAME_TRAIT = "sw$";

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public boolean matches(T target) {
return target.getAnnotationType().asErasure().getName().equals(annotationTypeName);
}

@Override
public String toString() {
return "AnnotationTypeNameMatch{" +
"annotationTypeName='" + annotationTypeName + '\'' +
'}';
}

/**
* The static method to create {@link AnnotationTypeNameMatch} This is a delegate method to follow byte-buddy {@link
* ElementMatcher}'s code style.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public boolean matches(MethodDescription target) {
return false;
}

@Override
public String toString() {
return "ArgumentTypeNameMatch{" +
"index=" + index +
", argumentTypeName='" + argumentTypeName + '\'' +
'}';
}

/**
* The static method to create {@link ArgumentTypeNameMatch} This is a delegate method to follow byte-buddy {@link
* ElementMatcher}'s code style.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public boolean matches(MethodDescription target) {
return target.getReturnType().asErasure().getName().equals(returnTypeName);
}

@Override
public String toString() {
return "ReturnTypeNameMatch{" +
"returnTypeName='" + returnTypeName + '\'' +
'}';
}

/**
* The static method to create {@link ReturnTypeNameMatch} This is a delegate method to follow byte-buddy {@link
* ElementMatcher}'s code style.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Objects;

/**
* One of the three "Intercept Point". "Intercept Point" is a definition about where and how intercept happens. In this
* "Intercept Point", the definition targets class's constructors, and the interceptor.
Expand All @@ -41,4 +43,8 @@ public interface ConstructorInterceptPoint {
* org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor}
*/
String getConstructorInterceptor();

default int computeHashCode() {
return Objects.hash(this.getClass().getName(), this.getConstructorMatcher().toString(), this.getConstructorInterceptor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Objects;

/**
* One of the three "Intercept Point". "Intercept Point" is a definition about where and how intercept happens. In this
* "Intercept Point", the definition targets class's instance methods, and the interceptor.
Expand All @@ -42,4 +44,8 @@ public interface InstanceMethodsInterceptPoint {
String getMethodsInterceptor();

boolean isOverrideArgs();

default int computeHashCode() {
return Objects.hash(this.getClass().getName(), this.getMethodsMatcher().toString(), this.getMethodsInterceptor(), this.isOverrideArgs());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

import java.util.Objects;

/**
* One of the three "Intercept Point". "Intercept Point" is a definition about where and how intercept happens. In this
* "Intercept Point", the definition targets class's static methods, and the interceptor.
Expand All @@ -42,4 +44,8 @@ public interface StaticMethodsInterceptPoint {
String getMethodsInterceptor();

boolean isOverrideArgs();

default int computeHashCode() {
return Objects.hash(this.getClass().getName(), this.getMethodsMatcher().toString(), this.getMethodsInterceptor(), this.isOverrideArgs());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
if (instanceMethodsInterceptPoints != null && instanceMethodsInterceptPoints.length > 0) {
existedMethodsInterceptPoints = true;
}
DelegateNamingResolver delegateNamingResolver = new DelegateNamingResolver(typeDescription.getTypeName(), this);

/**
* nothing need to be enhanced in class instance, maybe need enhance static methods.
Expand Down Expand Up @@ -121,7 +122,7 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
newClassBuilder = newClassBuilder.constructor(constructorInterceptPoint.getConstructorMatcher())
.intercept(SuperMethodCall.INSTANCE.andThen(MethodDelegation.withDefaultConfiguration()
.to(new ConstructorInter(constructorInterceptPoint
.getConstructorInterceptor(), classLoader))));
.getConstructorInterceptor(), classLoader), delegateNamingResolver.resolve(constructorInterceptPoint))));
}
}
}
Expand Down Expand Up @@ -149,7 +150,7 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
newClassBuilder = newClassBuilder.method(junction)
.intercept(MethodDelegation.withDefaultConfiguration()
.withBinders(Morph.Binder.install(OverrideCallable.class))
.to(new InstMethodsInterWithOverrideArgs(interceptor, classLoader)));
.to(new InstMethodsInterWithOverrideArgs(interceptor, classLoader), delegateNamingResolver.resolve(instanceMethodsInterceptPoint)));
}
} else {
if (isBootstrapInstrumentation()) {
Expand All @@ -159,7 +160,7 @@ protected DynamicType.Builder<?> enhanceInstance(TypeDescription typeDescription
} else {
newClassBuilder = newClassBuilder.method(junction)
.intercept(MethodDelegation.withDefaultConfiguration()
.to(new InstMethodsInter(interceptor, classLoader)));
.to(new InstMethodsInter(interceptor, classLoader), delegateNamingResolver.resolve(instanceMethodsInterceptPoint)));
}
}
}
Expand All @@ -183,6 +184,7 @@ protected DynamicType.Builder<?> enhanceClass(TypeDescription typeDescription, D
if (staticMethodsInterceptPoints == null || staticMethodsInterceptPoints.length == 0) {
return newClassBuilder;
}
DelegateNamingResolver delegateNamingResolver = new DelegateNamingResolver(typeDescription.getTypeName(), this);

for (StaticMethodsInterceptPoint staticMethodsInterceptPoint : staticMethodsInterceptPoints) {
String interceptor = staticMethodsInterceptPoint.getMethodsInterceptor();
Expand All @@ -200,7 +202,7 @@ protected DynamicType.Builder<?> enhanceClass(TypeDescription typeDescription, D
newClassBuilder = newClassBuilder.method(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher()))
.intercept(MethodDelegation.withDefaultConfiguration()
.withBinders(Morph.Binder.install(OverrideCallable.class))
.to(new StaticMethodsInterWithOverrideArgs(interceptor)));
.to(new StaticMethodsInterWithOverrideArgs(interceptor), delegateNamingResolver.resolve(staticMethodsInterceptPoint)));
}
} else {
if (isBootstrapInstrumentation()) {
Expand All @@ -210,7 +212,7 @@ protected DynamicType.Builder<?> enhanceClass(TypeDescription typeDescription, D
} else {
newClassBuilder = newClassBuilder.method(isStatic().and(staticMethodsInterceptPoint.getMethodsMatcher()))
.intercept(MethodDelegation.withDefaultConfiguration()
.to(new StaticMethodsInter(interceptor)));
.to(new StaticMethodsInter(interceptor), delegateNamingResolver.resolve(staticMethodsInterceptPoint)));
}
}

Expand Down
Loading