Skip to content

Commit

Permalink
Allow adding multiple ContextStore fields to one key class, part 3 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Sep 14, 2021
1 parent 473f16f commit 74c1f55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
8 changes: 4 additions & 4 deletions gradle-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ dependencies {
implementation("com.google.guava:guava:30.1.1-jre")
implementation("net.bytebuddy:byte-buddy-gradle-plugin:1.11.2")

implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:1.5.0-alpha")
implementation("io.opentelemetry.javaagent:opentelemetry-muzzle:1.5.0-alpha")
implementation("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:1.5.0-alpha")
implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:1.6.0-alpha-SNAPSHOT")
implementation("io.opentelemetry.javaagent:opentelemetry-muzzle:1.6.0-alpha-SNAPSHOT")
implementation("io.opentelemetry.javaagent:opentelemetry-javaagent-extension-api:1.6.0-alpha-SNAPSHOT")

implementation("org.eclipse.aether:aether-connector-basic:1.1.0")
implementation("org.eclipse.aether:aether-transport-http:1.1.0")
Expand All @@ -37,7 +37,7 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter-params")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")

testImplementation("io.opentelemetry.javaagent:opentelemetry-javaagent-instrumentation-api:1.5.0-alpha")
testImplementation("io.opentelemetry.javaagent:opentelemetry-javaagent-instrumentation-api:1.6.0-alpha-SNAPSHOT")
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.javaagent.extension.muzzle.Flag;
import io.opentelemetry.javaagent.extension.muzzle.MethodRef;
import io.opentelemetry.javaagent.extension.muzzle.Source;
import io.opentelemetry.javaagent.tooling.muzzle.ContextStoreMappings;
import io.opentelemetry.javaagent.tooling.muzzle.HelperResource;
import io.opentelemetry.javaagent.tooling.muzzle.HelperResourceBuilderImpl;
import io.opentelemetry.javaagent.tooling.muzzle.ReferenceCollector;
Expand Down Expand Up @@ -52,7 +53,7 @@ final class MuzzleCodeGenerator implements AsmVisitorWrapper {
private static final String MUZZLE_REFERENCES_METHOD_NAME = "getMuzzleReferences";
private static final String MUZZLE_HELPER_CLASSES_METHOD_NAME = "getMuzzleHelperClassNames";
private static final String MUZZLE_CONTEXT_STORE_CLASSES_METHOD_NAME =
"getMuzzleContextStoreClasses";
"registerMuzzleContextStoreClasses";
private final URLClassLoader classLoader;

public MuzzleCodeGenerator(URLClassLoader classLoader) {
Expand Down Expand Up @@ -510,50 +511,40 @@ private void generateMuzzleHelperClassNamesMethod(ReferenceCollector collector)

private void generateMuzzleContextStoreClassesMethod(ReferenceCollector collector) {
/*
* public Map<String, String> getMuzzleContextStoreClasses() {
* Map<String, String> contextStore = new HashMap<>(...);
* contextStore.put(..., ...);
* return contextStore;
* public void registerMuzzleContextStoreClasses(InstrumentationContextBuilder builder) {
* builder.register(..., ...);
* }
*/
MethodVisitor mv =
super.visitMethod(
Opcodes.ACC_PUBLIC,
MUZZLE_CONTEXT_STORE_CLASSES_METHOD_NAME,
"()Ljava/util/Map;",
"(Lio/opentelemetry/javaagent/extension/instrumentation/InstrumentationContextBuilder;)V",
null,
null);
mv.visitCode();

Map<String, String> contextStoreClasses = collector.getContextStoreClasses();
ContextStoreMappings contextStoreMappings = collector.getContextStoreMappings();

writeNewMap(mv, contextStoreClasses.size());
// stack: map
mv.visitVarInsn(Opcodes.ASTORE, 1);
// stack: <empty>

contextStoreClasses.forEach(
mv.visitVarInsn(Opcodes.ALOAD, 1);
// stack: builder
contextStoreMappings.forEach(
(className, contextClassName) -> {
mv.visitVarInsn(Opcodes.ALOAD, 1);
// stack: map
mv.visitLdcInsn(className);
// stack: map, className
// stack: builder, className
mv.visitLdcInsn(contextClassName);
// stack: map, className, contextClassName
// stack: builder, className, contextClassName
mv.visitMethodInsn(
Opcodes.INVOKEINTERFACE,
"java/util/Map",
"put",
"(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;",
"io/opentelemetry/javaagent/extension/instrumentation/InstrumentationContextBuilder",
"register",
"(Ljava/lang/String;Ljava/lang/String;)Lio/opentelemetry/javaagent/extension/instrumentation/InstrumentationContextBuilder;",
/* isInterface= */ true);
// stack: previousValue
mv.visitInsn(Opcodes.POP);
// stack: <empty>
// stack: builder
});

mv.visitVarInsn(Opcodes.ALOAD, 1);
// stack: map
mv.visitInsn(Opcodes.ARETURN);
mv.visitInsn(Opcodes.POP);
// stack: <empty>
mv.visitInsn(Opcodes.RETURN);

mv.visitMaxs(0, 0);
mv.visitEnd();
Expand Down

0 comments on commit 74c1f55

Please sign in to comment.