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

Transform lambda classes #4182

Merged
merged 2 commits into from
Sep 24, 2021
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
Expand Up @@ -15,11 +15,8 @@
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.CallableWrapper;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.PropagatedContext;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.RunnableWrapper;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -74,7 +71,6 @@ public static PropagatedContext enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = RunnableWrapper.wrapIfNeeded(task);
ContextStore<Runnable, PropagatedContext> contextStore =
InstrumentationContext.get(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down Expand Up @@ -119,7 +115,6 @@ public static PropagatedContext enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = RunnableWrapper.wrapIfNeeded(task);
ContextStore<Runnable, PropagatedContext> contextStore =
InstrumentationContext.get(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down Expand Up @@ -149,7 +144,6 @@ public static PropagatedContext enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Callable<?> task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = CallableWrapper.wrapIfNeeded(task);
ContextStore<Callable<?>, PropagatedContext> contextStore =
InstrumentationContext.get(Callable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down Expand Up @@ -181,30 +175,23 @@ public static Collection<?> submitEnter(
return Collections.emptyList();
}

Collection<Callable<?>> wrappedTasks = new ArrayList<>(tasks.size());
Context context = Java8BytecodeBridge.currentContext();
for (Callable<?> task : tasks) {
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
Callable<?> newTask = CallableWrapper.wrapIfNeeded(task);
wrappedTasks.add(newTask);
ContextStore<Callable<?>, PropagatedContext> contextStore =
InstrumentationContext.get(Callable.class, PropagatedContext.class);
ExecutorAdviceHelper.attachContextToTask(context, contextStore, newTask);
} else {
// note that task may be null here
wrappedTasks.add(task);
ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
}
}
tasks = wrappedTasks;

// returning tasks and not propagatedContexts to avoid allocating another list just for an
// edge case (exception)
return tasks;
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void submitExit(
@Advice.Enter Collection<? extends Callable<?>> wrappedTasks,
@Advice.Thrown Throwable throwable) {
@Advice.Enter Collection<? extends Callable<?>> tasks, @Advice.Thrown Throwable throwable) {
/*
Note1: invokeAny doesn't return any futures so all we need to do for it
is to make sure we close all scopes in case of an exception.
Expand All @@ -215,7 +202,7 @@ public static void submitExit(
(according to ExecutorService docs and AbstractExecutorService code)
*/
if (throwable != null) {
for (Callable<?> task : wrappedTasks) {
for (Callable<?> task : tasks) {
if (task != null) {
ContextStore<Callable<?>, PropagatedContext> contextStore =
InstrumentationContext.get(Callable.class, PropagatedContext.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.PropagatedContext;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.RunnableWrapper;
import java.util.concurrent.Executor;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
Expand Down Expand Up @@ -55,7 +54,6 @@ public static PropagatedContext addListenerEnter(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = RunnableWrapper.wrapIfNeeded(task);
ContextStore<Runnable, PropagatedContext> contextStore =
InstrumentationContext.get(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
id("otel.javaagent-instrumentation")
}

dependencies {
compileOnly(project(":javaagent-bootstrap"))

testImplementation(project(":javaagent-bootstrap"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.lambda;

import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.AsmVisitorWrapper;
import net.bytebuddy.description.field.FieldDescription;
import net.bytebuddy.description.field.FieldList;
import net.bytebuddy.description.method.MethodList;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.Implementation;
import net.bytebuddy.jar.asm.ClassVisitor;
import net.bytebuddy.jar.asm.ClassWriter;
import net.bytebuddy.jar.asm.MethodVisitor;
import net.bytebuddy.jar.asm.Opcodes;
import net.bytebuddy.jar.asm.Type;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.pool.TypePool;

public class InnerClassLambdaMetafactoryInstrumentation implements TypeInstrumentation {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("java.lang.invoke.InnerClassLambdaMetafactory");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyTransformer(
(builder, typeDescription, classLoader, module) ->
builder.visit(
new AsmVisitorWrapper() {
@Override
public int mergeWriter(int flags) {
return flags | ClassWriter.COMPUTE_MAXS;
}

@Override
public int mergeReader(int flags) {
return flags;
}

@Override
public ClassVisitor wrap(
TypeDescription instrumentedType,
ClassVisitor classVisitor,
Implementation.Context implementationContext,
TypePool typePool,
FieldList<FieldDescription.InDefinedShape> fields,
MethodList<?> methods,
int writerFlags,
int readerFlags) {
return new MetaFactoryClassVisitor(
classVisitor, instrumentedType.getInternalName());
}
}));
}

private static class MetaFactoryClassVisitor extends ClassVisitor {
private final String slashClassName;

MetaFactoryClassVisitor(ClassVisitor cv, String slashClassName) {
super(Opcodes.ASM7, cv);
this.slashClassName = slashClassName;
}

@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
// The version of InnerClassLambdaMetafactory used in first version of jdk8 can be seen at
// https://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java
// Depending on jdk version we instrument either spinInnerClass or generateInnerClass.
// We look for a call to ASM ClassWriter.toByteArray() and insert our lambda class
// transformation after it so that defining lambda class will proceed with replaced bytecode.
// This transformation uses ASM instead of Byte-Buddy advice because advice allows adding
// code to the start and end of the method, but here we are modifying a call in the middle of
// the method.
if (("spinInnerClass".equals(name) || "generateInnerClass".equals(name))
&& "()Ljava/lang/Class;".equals(descriptor)) {
mv =
new MethodVisitor(api, mv) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment on why we can't use an Advice method (assuming we can't)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I wrote this I didn't realize that actually advice could be used. For this we would need to instrument spinInnerClass to enter/exit a ThreadLocal and toByteArray of ClassWriter bundled in jdk to only transform when ThreadLocal is set. I do like the asm way a bit more because it is straightforward. Do you think it would be better to use advice for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Advice is nice for making this instrumentation more consistent with our others. But if it adds significant complexity it's not worth it, a comment just describing that is fine.

@Override
public void visitMethodInsn(
int opcode, String owner, String name, String descriptor, boolean isInterface) {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
// if current instruction is a call to ASM ClassWriter.toByteArray() insert call to
// our lambda transformer
if (opcode == Opcodes.INVOKEVIRTUAL
&& "toByteArray".equals(name)
&& "()[B".equals(descriptor)) {
mv.visitVarInsn(Opcodes.ALOAD, 0);
mv.visitFieldInsn(
Opcodes.GETFIELD, slashClassName, "lambdaClassName", "Ljava/lang/String;");
mv.visitVarInsn(Opcodes.ALOAD, 0);
// targetClass is used to get the ClassLoader where lambda class will be defined
mv.visitFieldInsn(
Opcodes.GETFIELD, slashClassName, "targetClass", "Ljava/lang/Class;");
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
Type.getInternalName(LambdaTransformer.class),
"transform",
"([BLjava/lang/String;Ljava/lang/Class;)[B",
false);
}
}
};
}
return mv;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.lambda;

import static java.util.Collections.singletonList;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class LambdaInstrumentationModule extends InstrumentationModule {
public LambdaInstrumentationModule() {
super("internal-lambda");
}

@Override
public boolean defaultEnabled() {
// internal instrumentations are always enabled by default
return true;
}

@Override
public List<String> getMuzzleHelperClassNames() {
// this instrumentation uses ASM not ByteBuddy so muzzle doesn't automatically add helper
// classes
return singletonList(
"io.opentelemetry.javaagent.instrumentation.internal.lambda.LambdaTransformer");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new InnerClassLambdaMetafactoryInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.internal.lambda;

import io.opentelemetry.javaagent.bootstrap.ClassFileTransformerHolder;
import java.lang.instrument.ClassFileTransformer;

/** Helper class for transforming lambda class bytes. */
public final class LambdaTransformer {

private LambdaTransformer() {}

/**
* Called from {@code java.lang.invoke.InnerClassLambdaMetafactory} to transform lambda class
* bytes.
*/
public static byte[] transform(byte[] classBytes, String slashClassName, Class<?> targetClass) {
ClassFileTransformer transformer = ClassFileTransformerHolder.getClassFileTransformer();
if (transformer != null) {
try {
byte[] result =
transformer.transform(
targetClass.getClassLoader(), slashClassName, null, null, classBytes);
if (result != null) {
return result;
}
} catch (Throwable throwable) {
// sun.instrument.TransformerManager catches Throwable from ClassFileTransformer and ignores
// it, we do the same.
}
}

return classBytes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.javaagent.bootstrap.FieldBackedContextStoreAppliedMarker

class LambdaInstrumentationTest extends AgentInstrumentationSpecification {

def "test transform Runnable lambda"() {
setup:
Runnable runnable = TestLambda.makeRunnable()

expect:
// RunnableInstrumentation adds a ContextStore to all implementors of Runnable. If lambda class
// is transformed then it must have context store marker interface.
runnable instanceof FieldBackedContextStoreAppliedMarker
!FieldBackedContextStoreAppliedMarker.isAssignableFrom(Runnable)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

public class TestLambda {
static Runnable makeRunnable() {
return () -> {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.PropagatedContext;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.RunnableWrapper;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand All @@ -44,7 +43,6 @@ public static PropagatedContext enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = RunnableWrapper.wrapIfNeeded(task);
ContextStore<Runnable, PropagatedContext> contextStore =
InstrumentationContext.get(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import io.opentelemetry.api.trace.SpanKind
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.javaagent.instrumentation.jetty.JavaLambdaMaker
import org.eclipse.jetty.util.thread.QueuedThreadPool

import static org.junit.Assume.assumeTrue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jetty;

public class JavaLambdaMaker {

@SuppressWarnings("FunctionalExpressionCanBeFolded")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.ExecutorAdviceHelper;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.PropagatedContext;
import io.opentelemetry.javaagent.instrumentation.api.concurrent.RunnableWrapper;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -50,7 +49,6 @@ public static PropagatedContext enterJobSubmit(
@Advice.Argument(value = 0, readOnly = false) Runnable task) {
Context context = Java8BytecodeBridge.currentContext();
if (ExecutorAdviceHelper.shouldPropagateContext(context, task)) {
task = RunnableWrapper.wrapIfNeeded(task);
ContextStore<Runnable, PropagatedContext> contextStore =
InstrumentationContext.get(Runnable.class, PropagatedContext.class);
return ExecutorAdviceHelper.attachContextToTask(context, contextStore, task);
Expand Down
Loading