Skip to content

Commit

Permalink
Ensure tracer resources are included in the final native executable
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Dec 17, 2022
1 parent 7c18f3e commit cf44c0b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package datadog.trace.instrumentation.graal.nativeimage;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
Expand All @@ -20,15 +20,17 @@ public String instrumentedType() {
@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
isMethod().and(named("extractDriverArguments")),
NativeImageGeneratorRunnerInstrumentation.class.getName() + "$MainAdvice");
isMethod().and(namedOneOf("extractDriverArguments", "extractImageClassPath")),
NativeImageGeneratorRunnerInstrumentation.class.getName() + "$ExpandArgsAdvice");
}

public static class MainAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit(@Advice.Return List<String> args) {
public static class ExpandArgsAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Argument(0) List<String> args) {
args.add("-H:+AddAllCharsets");
args.add("-H:EnableURLProtocols=http");
// placeholder to trigger resource scanning, ResourcesFeatureInstrumentation does the rest
args.add("-H:IncludeResources=.*dd-.*version$");
args.add(
"-H:ReflectionConfigurationResources="
+ "META-INF/native-image/com.datadoghq/dd-java-agent/reflect-config.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package datadog.trace.instrumentation.graal.nativeimage;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;

import com.google.auto.service.AutoService;
import com.oracle.svm.core.jdk.Resources;
import datadog.trace.agent.tooling.Instrumenter;
import java.io.InputStream;
import net.bytebuddy.asm.Advice;

@AutoService(Instrumenter.class)
public final class ResourcesFeatureInstrumentation extends AbstractNativeImageInstrumentation
implements Instrumenter.ForSingleType {

@Override
public String instrumentedType() {
return "com.oracle.svm.hosted.ResourcesFeature";
}

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
isMethod().and(named("beforeAnalysis")),
ResourcesFeatureInstrumentation.class.getName() + "$InjectResourcesAdvice");
}

public static class InjectResourcesAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit() {
String[] tracerResources = {
"dd-java-agent.version",
"dd-trace-api.version",
"inst/dd-trace-core.version",
"shared/dogstatsd/version.properties",
"shared/okhttp3/internal/publicsuffix/publicsuffixes.gz"
};

for (String original : tracerResources) {
// drop inst/shared prefixes from embedded resources, so we can find them in native-image
// (the final native-image won't have our isolating class-loader to maps these resources)
String flattened = original.substring(original.indexOf('/') + 1);
try (InputStream is = ClassLoader.getSystemResourceAsStream(original)) {
Resources.registerResource(flattened, is);
} catch (Throwable ignore) {
}
}
}
}
}

0 comments on commit cf44c0b

Please sign in to comment.