-
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.
Ensure tracer resources are included in the final native executable
- Loading branch information
Showing
2 changed files
with
58 additions
and
6 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
50 changes: 50 additions & 0 deletions
50
...java/datadog/trace/instrumentation/graal/nativeimage/ResourcesFeatureInstrumentation.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,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) { | ||
} | ||
} | ||
} | ||
} | ||
} |