Skip to content

Commit

Permalink
Faster type matching
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Mar 31, 2022
1 parent fbc100a commit dd252f8
Show file tree
Hide file tree
Showing 28 changed files with 685 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ dependencies {

// Requires old Guava. Can't use enforcedPlatform since predates BOM
configurations.testRuntimeClasspath.resolutionStrategy.force("com.google.guava:guava:19.0")

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.javaagent.instrumentation.extannotations;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType;
import static io.opentelemetry.javaagent.instrumentation.extannotations.ExternalAnnotationSingletons.instrumenter;
import static java.util.logging.Level.WARNING;
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
Expand Down Expand Up @@ -100,7 +99,7 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {

@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return hasSuperType(declaresMethod(isAnnotatedWith(traceAnnotationMatcher)));
return declaresMethod(isAnnotatedWith(traceAnnotationMatcher));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public List<TypeInstrumentation> typeInstrumentations() {
return asList(
new BootDelegationInstrumentation(),
new LoadInjectedClassInstrumentation(),
new ResourceInjectionInstrumentation());
new ResourceInjectionInstrumentation(),
new DefineClassInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

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

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

import io.opentelemetry.javaagent.bootstrap.DefineClassHelper;
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler.DefineClassContext;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.nio.ByteBuffer;
import java.security.ProtectionDomain;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class DefineClassInstrumentation implements TypeInstrumentation {

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

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
named("defineClass")
.and(
takesArguments(
String.class, byte[].class, int.class, int.class, ProtectionDomain.class)),
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice");
transformer.applyAdviceToMethod(
named("defineClass")
.and(takesArguments(String.class, ByteBuffer.class, ProtectionDomain.class)),
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice2");
}

@SuppressWarnings("unused")
public static class DefineClassAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static DefineClassContext onEnter(
@Advice.This ClassLoader classLoader,
@Advice.Argument(0) String className,
@Advice.Argument(1) byte[] classBytes,
@Advice.Argument(2) int offset,
@Advice.Argument(3) int length) {
return DefineClassHelper.beforeDefineClass(
classLoader, className, classBytes, offset, length);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Enter DefineClassContext context) {
DefineClassHelper.afterDefineClass(context);
}
}

@SuppressWarnings("unused")
public static class DefineClassAdvice2 {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static DefineClassContext onEnter(
@Advice.This ClassLoader classLoader,
@Advice.Argument(0) String className,
@Advice.Argument(1) ByteBuffer classBytes) {
return DefineClassHelper.beforeDefineClass(classLoader, className, classBytes);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit(@Advice.Enter DefineClassContext context) {
DefineClassHelper.afterDefineClass(context);
}
}
}
4 changes: 4 additions & 0 deletions instrumentation/jaxrs/jaxrs-1.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ dependencies {
testImplementation("io.dropwizard:dropwizard-testing:0.7.1")
testImplementation("javax.xml.bind:jaxb-api:2.2.3")
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new JaxrsAnnotationsInstrumentation());
}

@Override
protected boolean defaultEnabled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new JaxrsAnnotationsInstrumentation(),
new JaxrsAsyncResponseInstrumentation());
}

@Override
protected boolean defaultEnabled() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ dependencies {
tasks.withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.jaxrs.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ tasks {
withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.jaxrs.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ tasks {
withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.jaxrs.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ tasks {
withType<Test>().configureEach {
// TODO run tests both with and without experimental span attributes
jvmArgs("-Dotel.instrumentation.jaxrs.experimental-span-attributes=true")
jvmArgs("-Dotel.instrumentation.jaxrs.enabled=true")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ dependencies {
testImplementation("com.sun.xml.ws:jaxws-rt:2.2.8")
testImplementation("com.sun.xml.ws:jaxws-tools:2.2.8")
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.jaxws.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ dependencies {
testImplementation("javax.annotation:javax.annotation-api:1.2")
testImplementation("com.sun.xml.messaging.saaj:saaj-impl:1.5.2")
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.jaxws.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ tasks.withType<Test>().configureEach {
jvmArgs("--add-exports=java.xml/com.sun.org.apache.xerces.internal.dom=ALL-UNNAMED")
jvmArgs("--add-exports=java.xml/com.sun.org.apache.xerces.internal.jaxp=ALL-UNNAMED")
jvmArgs("-XX:+IgnoreUnrecognizedVMOptions")

jvmArgs("-Dotel.instrumentation.jaxws.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ dependencies {
library("javax.jws:javax.jws-api:1.1")
implementation(project(":instrumentation:jaxws:jaxws-common:library"))
}

tasks.withType<Test>().configureEach {
jvmArgs("-Dotel.instrumentation.jaxws.enabled=true")
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public JwsInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new JwsAnnotationsInstrumentation());
}

@Override
protected boolean defaultEnabled() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.bootstrap;

import java.nio.ByteBuffer;

public class DefineClassHelper {

/** Helper class for {@code ClassLoader.defineClass} callbacks. */
public interface Handler {
DefineClassContext beforeDefineClass(
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length);

void afterDefineClass(DefineClassContext context);

/** Context returned from {@code beforeDefineClass} and passed to {@code afterDefineClass}. */
interface DefineClassContext {
void exit();
}
}

private static volatile Handler handler;

public static Handler.DefineClassContext beforeDefineClass(
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length) {
return handler.beforeDefineClass(classLoader, className, classBytes, offset, length);
}

public static Handler.DefineClassContext beforeDefineClass(
ClassLoader classLoader, String className, ByteBuffer byteBuffer) {
// see how ClassLoader handles ByteBuffer
// https:/openjdk/jdk11u/blob/487c3344fee3502b4843e7e11acceb77ad16100c/src/java.base/share/classes/java/lang/ClassLoader.java#L1095
int length = byteBuffer.remaining();
if (byteBuffer.hasArray()) {
return beforeDefineClass(
classLoader,
className,
byteBuffer.array(),
byteBuffer.position() + byteBuffer.arrayOffset(),
length);
} else {
byte[] classBytes = new byte[length];
byteBuffer.duplicate().get(classBytes);
return beforeDefineClass(classLoader, className, classBytes, 0, length);
}
}

public static void afterDefineClass(Handler.DefineClassContext context) {
handler.afterDefineClass(context);
}

/**
* Sets the {@link Handler} with callbacks to execute when {@code ClassLoader.defineClass} is
* called.
*/
public static void internalSetHandler(Handler handler) {
if (DefineClassHelper.handler != null) {
// Only possible by misuse of this API, just ignore.
return;
}
DefineClassHelper.handler = handler;
}

private DefineClassHelper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.opentelemetry.javaagent.bootstrap.AgentClassLoader;
import io.opentelemetry.javaagent.bootstrap.BootstrapPackagePrefixesHolder;
import io.opentelemetry.javaagent.bootstrap.ClassFileTransformerHolder;
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper;
import io.opentelemetry.javaagent.extension.AgentListener;
import io.opentelemetry.javaagent.extension.bootstrap.BootstrapPackagesConfigurer;
import io.opentelemetry.javaagent.extension.ignore.IgnoredTypesConfigurer;
Expand Down Expand Up @@ -114,6 +115,7 @@ public static ResettableClassFileTransformer installBytebuddyAgent(
Config config = Config.get();

setBootstrapPackages(config);
setDefineClassHandler();

// If noop OpenTelemetry is enabled, autoConfiguredSdk will be null and AgentListeners are not
// called
Expand All @@ -129,6 +131,7 @@ public static ResettableClassFileTransformer installBytebuddyAgent(
runBeforeAgentListeners(agentListeners, config, autoConfiguredSdk);
}

AgentTooling.init(Utils.getBootstrapProxy());
AgentBuilder agentBuilder =
new AgentBuilder.Default()
.disableClassFormatChanges()
Expand Down Expand Up @@ -203,6 +206,10 @@ private static void setBootstrapPackages(Config config) {
BootstrapPackagePrefixesHolder.setBoostrapPackagePrefixes(builder.build());
}

private static void setDefineClassHandler() {
DefineClassHelper.internalSetHandler(DefineClassHandler.INSTANCE);
}

private static void runBeforeAgentListeners(
Iterable<AgentListener> agentListeners,
Config config,
Expand Down
Loading

0 comments on commit dd252f8

Please sign in to comment.