-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
native-image instrumentation (#4333)
* Ignore .gz resource dependencies when pre-processing Instrumenters as byte-buddy can't process them * Automatically adjust native-image configuration to support dd-java-agent instrumentation * Only apply instrumentation, don't start other services, when native-image build is happening * Turn off weak-map background thread during native-image instrumentation
- Loading branch information
Showing
11 changed files
with
205 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
apply from: "$rootDir/gradle/java.gradle" |
41 changes: 41 additions & 0 deletions
41
dd-java-agent/instrumentation/graal/native-image/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
ext { | ||
minJavaVersionForTests = JavaVersion.VERSION_11 | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group = "org.graalvm.nativeimage" | ||
module = "svm" | ||
versions = "[20,)" | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
apply plugin: "idea" | ||
|
||
[compileMain_java11Java, compileTestJava].each { | ||
it.sourceCompatibility = JavaVersion.VERSION_11 | ||
it.targetCompatibility = JavaVersion.VERSION_11 | ||
setJavaVersion(it, 11) | ||
it.options.compilerArgs.addAll([ | ||
'-Xlint:all,-processing,-options,-path', | ||
'--add-modules', | ||
'jdk.internal.vm.ci', | ||
'--add-exports', | ||
'jdk.internal.vm.ci/jdk.vm.ci.meta=ALL-UNNAMED' | ||
]) | ||
} | ||
|
||
dependencies { | ||
main_java11CompileOnly group: 'org.graalvm.nativeimage', name: 'svm', version: '20.0.0' | ||
} | ||
|
||
forbiddenApisMain_java11 { | ||
failOnMissingClasses = false | ||
} | ||
|
||
idea { | ||
module { | ||
jdkName = '11' | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...a/datadog/trace/instrumentation/graal/nativeimage/AbstractNativeImageInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package datadog.trace.instrumentation.graal.nativeimage; | ||
|
||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.api.Platform; | ||
import java.util.Set; | ||
|
||
public abstract class AbstractNativeImageInstrumentation extends Instrumenter.Default { | ||
public AbstractNativeImageInstrumentation() { | ||
super("native-image"); | ||
} | ||
|
||
@Override | ||
public boolean isApplicable(Set<TargetSystem> enabledSystems) { | ||
return Platform.isNativeImageBuilder(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...ace/instrumentation/graal/nativeimage/AnnotationSubstitutionProcessorInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package datadog.trace.instrumentation.graal.nativeimage; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
|
||
@AutoService(Instrumenter.class) | ||
public final class AnnotationSubstitutionProcessorInstrumentation | ||
extends AbstractNativeImageInstrumentation implements Instrumenter.ForSingleType { | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "com.oracle.svm.hosted.substitute.AnnotationSubstitutionProcessor"; | ||
} | ||
|
||
@Override | ||
public void adviceTransformations(AdviceTransformation transformation) { | ||
transformation.applyAdvice( | ||
isMethod() | ||
.and(named("lookup")) | ||
.and(takesArgument(0, named("jdk.vm.ci.meta.ResolvedJavaField"))), | ||
packageName + ".DeleteFieldAdvice"); | ||
} | ||
|
||
@Override | ||
public String[] muzzleIgnoredClassNames() { | ||
// ignore JVMCI classes which are part of GraalVM but aren't available in public repositories | ||
return new String[] {"jdk.vm.ci.meta.ResolvedJavaType", "jdk.vm.ci.meta.ResolvedJavaField"}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...og/trace/instrumentation/graal/nativeimage/ClassInitializationFeatureInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package datadog.trace.instrumentation.graal.nativeimage; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
|
||
@AutoService(Instrumenter.class) | ||
public final class ClassInitializationFeatureInstrumentation | ||
extends AbstractNativeImageInstrumentation implements Instrumenter.ForSingleType { | ||
|
||
@Override | ||
public String instrumentedType() { | ||
return "com.oracle.svm.hosted.classinitialization.ClassInitializationFeature"; | ||
} | ||
|
||
@Override | ||
public void adviceTransformations(AdviceTransformation transformation) { | ||
transformation.applyAdvice( | ||
isMethod() | ||
.and(named("processClassInitializationOptions")) | ||
.and( | ||
takesArgument( | ||
0, | ||
named("com.oracle.svm.hosted.classinitialization.ClassInitializationSupport"))), | ||
packageName + ".ClassInitializationAdvice"); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...ain/java11/datadog/trace/instrumentation/graal/nativeimage/ClassInitializationAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package datadog.trace.instrumentation.graal.nativeimage; | ||
|
||
import net.bytebuddy.asm.Advice; | ||
import org.graalvm.nativeimage.impl.RuntimeClassInitializationSupport; | ||
|
||
public class ClassInitializationAdvice { | ||
@Advice.OnMethodEnter | ||
public static void onEnter( | ||
@Advice.Argument(0) RuntimeClassInitializationSupport classInitializationSupport) { | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.api.env.CapturedEnvironment", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.api.ConfigDefaults", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.api.Functions", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.api.InstrumenterConfig", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.api.Platform", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.api.GlobalTracer", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.config.provider.ConfigConverter", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.config.provider.ConfigProvider", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.bootstrap.Agent", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.bootstrap.BootstrapProxy", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.CallDepthThreadLocalMap", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.DatadogClassLoader", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.FieldBackedContextStores", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.instrumentation.java.concurrent.ConcurrentState", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.bootstrap.instrumentation.java.concurrent.TPEHelper", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.logging.LoggingSettingsDescription", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"datadog.trace.logging.simplelogger.SLCompatFactory", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.trace.util.CollectionUtils", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.slf4j.impl.StaticLoggerBinder", ""); | ||
classInitializationSupport.initializeAtBuildTime("datadog.slf4j.LoggerFactory", ""); | ||
classInitializationSupport.initializeAtBuildTime( | ||
"com.blogspot.mydailyjava.weaklockfree.WeakConcurrentMap", ""); | ||
classInitializationSupport.initializeAtBuildTime("net.bytebuddy.", ""); | ||
classInitializationSupport.initializeAtBuildTime("com.sun.proxy.", ""); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...ge/src/main/java11/datadog/trace/instrumentation/graal/nativeimage/DeleteFieldAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package datadog.trace.instrumentation.graal.nativeimage; | ||
|
||
import com.oracle.svm.core.annotate.Delete; | ||
import com.oracle.svm.hosted.substitute.AnnotatedField; | ||
import jdk.vm.ci.meta.ResolvedJavaField; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
public class DeleteFieldAdvice { | ||
@Advice.OnMethodExit | ||
public static void onExit( | ||
@Advice.Argument(0) ResolvedJavaField field, | ||
@Advice.Return(readOnly = false) ResolvedJavaField result, | ||
@Advice.FieldValue("SUBSTITUTION_DELETE") Delete SUBSTITUTION_DELETE) { | ||
if ("datadog.trace.bootstrap.DatadogClassLoader" | ||
.equals(field.getDeclaringClass().toClassName())) { | ||
result = new AnnotatedField(field, SUBSTITUTION_DELETE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters