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

Activate tracer when native-image runtime starts #4437

Merged
merged 15 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,63 @@
[
{
"name" : "java.lang.Boolean",
"methods": [
{"name": "booleanValueOf", "parameterTypes": ["java.lang.String"]}
]
},
{
"name" : "java.lang.Integer",
"methods": [
{"name": "valueOf", "parameterTypes": ["java.lang.String"]}
]
},
{
"name" : "java.lang.Long",
"methods": [
{"name": "valueOf", "parameterTypes": ["java.lang.String"]}
]
},
{
"name" : "java.lang.Float",
"methods": [
{"name": "valueOf", "parameterTypes": ["java.lang.String"]}
]
},
{
"name" : "java.lang.Double",
"methods": [
{"name": "valueOf", "parameterTypes": ["java.lang.String"]}
]
},
{
"name" : "datadog.trace.instrumentation.springweb.HandlerMappingResourceNameFilter",
"methods": [
{"name": "<init>", "parameterTypes": []}
]
},
{
"name" : "datadog.trace.instrumentation.springweb.OrderedServletPathRequestFilter",
"methods": [
{"name": "<init>", "parameterTypes": []}
]
},
{
"name" : "org.jctools.queues.MpscBlockingConsumerArrayQueueColdProducerFields",
"fields": [
{"name": "producerLimit", "allowUnsafeAccess": true}
]
},
{
"name" : "org.jctools.queues.MpscBlockingConsumerArrayQueueProducerFields",
"fields": [
{"name": "producerIndex", "allowUnsafeAccess": true}
]
},
{
"name" : "org.jctools.queues.MpscBlockingConsumerArrayQueueConsumerFields",
"fields": [
{"name": "consumerIndex", "allowUnsafeAccess": true},
{"name": "blocked", "allowUnsafeAccess": true}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import datadog.trace.agent.tooling.bytebuddy.matcher.DDElementMatchers;
import datadog.trace.api.InstrumenterConfig;
import datadog.trace.api.IntegrationsCollector;
import datadog.trace.api.Platform;
import datadog.trace.api.ProductActivation;
import datadog.trace.bootstrap.FieldBackedContextAccessor;
import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter;
Expand Down Expand Up @@ -46,7 +47,9 @@ public class AgentInstaller {
WeakMaps.registerAsSupplier();
WeakCaches.registerAsSupplier();
// supply PID fall-back on Java 8 as early as possible
PidHelper.supplyIfAbsent(new PosixPidSupplier());
if (!Platform.isNativeImageBuilder()) {
PidHelper.supplyIfAbsent(new PosixPidSupplier());
}
}

public static void installBytebuddyAgent(final Instrumentation inst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public class AgentInstaller {
WeakMaps.registerAsSupplier();
WeakCaches.registerAsSupplier();
// supply PID fall-back on Java 8 as early as possible
PidHelper.supplyIfAbsent(new PosixPidSupplier());
if (!Platform.isNativeImageBuilder()) {
PidHelper.supplyIfAbsent(new PosixPidSupplier());
}
}

public static void installBytebuddyAgent(Instrumentation inst) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ public void visit(
final String signature,
final String superName,
final String[] interfaces) {
apply(superName, REQUIRES);
apply(interfaces, REQUIRES);
record(superName, REQUIRES);
record(interfaces, REQUIRES);
}

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
if (this.className.equals(name)) {
apply(outerName, REQUIRES);
record(outerName, REQUIRES);
}
}

Expand All @@ -156,7 +156,7 @@ public FieldVisitor visitField(
final String descriptor,
final String signature,
final Object value) {
apply(Type.getType(descriptor), REQUIRES);
record(Type.getType(descriptor), REQUIRES);
return null;
}

Expand All @@ -167,8 +167,8 @@ public MethodVisitor visitMethod(
final String descriptor,
final String signature,
final String[] exceptions) {
apply(Type.getMethodType(descriptor), REQUIRES);
apply(exceptions, REQUIRES);
record(Type.getMethodType(descriptor), REQUIRES);
record(exceptions, REQUIRES);
return methodScanner;
}

Expand All @@ -181,8 +181,8 @@ class MethodScanner extends MethodVisitor {
@Override
public void visitFieldInsn(
final int opcode, final String owner, final String name, final String descriptor) {
apply(Type.getObjectType(owner), USES);
apply(Type.getType(descriptor), USES);
record(Type.getObjectType(owner), USES);
record(Type.getType(descriptor), USES);
}

@Override
Expand All @@ -192,13 +192,13 @@ public void visitMethodInsn(
final String name,
final String descriptor,
final boolean isInterface) {
apply(Type.getObjectType(owner), USES);
apply(Type.getMethodType(descriptor), USES);
record(Type.getObjectType(owner), USES);
record(Type.getMethodType(descriptor), USES);
}

@Override
public void visitTypeInsn(final int opcode, final String type) {
apply(Type.getObjectType(type), USES);
record(Type.getObjectType(type), USES);
}

@Override
Expand All @@ -207,23 +207,23 @@ public void visitInvokeDynamicInsn(
String descriptor,
Handle bootstrapMethodHandle,
Object... bootstrapMethodArguments) {
apply(Type.getType(descriptor), USES);
apply(bootstrapMethodHandle, USES);
record(Type.getType(descriptor), USES);
record(bootstrapMethodHandle, USES);
for (Object value : bootstrapMethodArguments) {
if (value instanceof Type) {
apply((Type) value, USES);
record((Type) value, USES);
} else if (value instanceof Handle) {
apply((Handle) value, USES);
record((Handle) value, USES);
}
}
}

@Override
public void visitLdcInsn(final Object value) {
if (value instanceof Type) {
apply((Type) value, USES);
record((Type) value, USES);
} else if (value instanceof Handle) {
apply((Handle) value, USES);
record((Handle) value, USES);
}
}
}
Expand All @@ -239,14 +239,14 @@ void usesClass(String className) {
uses.add(className);
}

void apply(Type type, Consumer<String> action) {
void record(Type type, Consumer<String> action) {
if (null != type) {
while (type.getSort() == Type.ARRAY) {
type = type.getElementType();
}
if (type.getSort() == Type.METHOD) {
apply(type.getArgumentTypes(), action);
apply(type.getReturnType(), action);
record(type.getArgumentTypes(), action);
record(type.getReturnType(), action);
} else if (type.getSort() == Type.OBJECT) {
String className = type.getClassName();
// ignore types that we expect to be on the boot-class-path
Expand All @@ -270,31 +270,31 @@ void apply(Type type, Consumer<String> action) {
}
}

void apply(Type[] types, Consumer<String> action) {
void record(Type[] types, Consumer<String> action) {
if (null != types) {
for (Type t : types) {
apply(t, action);
record(t, action);
}
}
}

void apply(Handle handle, Consumer<String> action) {
void record(Handle handle, Consumer<String> action) {
if (null != handle) {
apply(Type.getObjectType(handle.getOwner()), action);
apply(Type.getType(handle.getDesc()), action);
record(Type.getObjectType(handle.getOwner()), action);
record(Type.getType(handle.getDesc()), action);
}
}

void apply(String internalName, Consumer<String> action) {
void record(String internalName, Consumer<String> action) {
if (null != internalName) {
apply(Type.getObjectType(internalName), action);
record(Type.getObjectType(internalName), action);
}
}

void apply(String[] internalNames, Consumer<String> action) {
void record(String[] internalNames, Consumer<String> action) {
if (null != internalNames) {
for (String n : internalNames) {
apply(Type.getObjectType(n), action);
record(Type.getObjectType(n), action);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package datadog.trace.agent.tooling.nativeimage;

import datadog.communication.ddagent.SharedCommunicationObjects;
import datadog.trace.agent.tooling.TracerInstaller;
import datadog.trace.bootstrap.instrumentation.api.ProfilingContextIntegration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Activates the tracer in native executables, see {@code VMRuntimeInstrumentation}. */
public final class TracerActivation {
private static final Logger log = LoggerFactory.getLogger(TracerActivation.class);

public static void activate() {
try {
TracerInstaller.installGlobalTracer(
new SharedCommunicationObjects(), ProfilingContextIntegration.NoOp.INSTANCE);
} catch (Throwable e) {
log.warn("Problem activating datadog tracer", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ apply plugin: "idea"
}

dependencies {
compileOnly project(':dd-java-agent:agent-logging')

main_java11CompileOnly group: 'org.graalvm.nativeimage', name: 'svm', version: '20.0.0'
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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;
import net.bytebuddy.asm.Advice;

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

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

@Override
public void adviceTransformations(AdviceTransformation transformation) {
transformation.applyAdvice(
isMethod().and(named("linkAtBuildTime")).and(takesArgument(0, Class.class)),
LinkAtBuildTimeInstrumentation.class.getName() + "$LinkAtBuildTimeAdvice");
}

public static class LinkAtBuildTimeAdvice {
@Advice.OnMethodExit(suppress = Throwable.class)
public static void onExit(
@Advice.Argument(0) Class<?> declaringClass,
@Advice.Return(readOnly = false) boolean linkAtBuildTime) {
// skip AndroidPlatform from build-time linking because we're not building on Android
if ("okhttp3.internal.platform.AndroidPlatform".equals(declaringClass.getName())) {
linkAtBuildTime = false;
}
}
}
}
Loading