From de6e0ef675ac43e55b3685e574311207f809d01d Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Fri, 20 Dec 2024 17:52:53 +0200 Subject: [PATCH] Fix ktor3 latest dep test (#12937) --- .../ktor/v3_0/ServerInstrumentation.java | 16 +++++++++++++++- .../indy/AdviceSignatureEraser.java | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/instrumentation/ktor/ktor-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ktor/v3_0/ServerInstrumentation.java b/instrumentation/ktor/ktor-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ktor/v3_0/ServerInstrumentation.java index f5dc7368e78a..9cdb3264c929 100644 --- a/instrumentation/ktor/ktor-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ktor/v3_0/ServerInstrumentation.java +++ b/instrumentation/ktor/ktor-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ktor/v3_0/ServerInstrumentation.java @@ -10,6 +10,7 @@ import io.ktor.server.application.Application; import io.ktor.server.application.ApplicationPluginKt; +import io.ktor.server.engine.EmbeddedServer; import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.instrumentation.ktor.v2_0.common.AbstractKtorServerTelemetryBuilder; import io.opentelemetry.instrumentation.ktor.v2_0.common.internal.KtorBuilderUtil; @@ -17,6 +18,8 @@ import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; import kotlin.Unit; import kotlin.jvm.functions.Function1; import net.bytebuddy.asm.Advice; @@ -39,7 +42,18 @@ public void transform(TypeTransformer transformer) { public static class ConstructorAdvice { @Advice.OnMethodExit - public static void onExit(@Advice.FieldValue("_applicationInstance") Application application) { + public static void onExit( + @Advice.This EmbeddedServer server, @Advice.Origin MethodHandles.Lookup lookup) + throws Throwable { + MethodHandle getter; + try { + // since 3.0.3 + getter = lookup.findGetter(EmbeddedServer.class, "applicationInstance", Application.class); + } catch (NoSuchFieldException exception) { + // before 3.0.3 + getter = lookup.findGetter(EmbeddedServer.class, "_applicationInstance", Application.class); + } + Application application = (Application) getter.invoke(server); ApplicationPluginKt.install( application, KtorServerTelemetryBuilderKt.getKtorServerTelemetry(), new SetupFunction()); } diff --git a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/indy/AdviceSignatureEraser.java b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/indy/AdviceSignatureEraser.java index 429b93d11e1e..627d867fef29 100644 --- a/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/indy/AdviceSignatureEraser.java +++ b/javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/instrumentation/indy/AdviceSignatureEraser.java @@ -85,7 +85,7 @@ private static String eraseTypes(String descriptor) { String reference = matcher.group(); if (reference.startsWith("Ljava/")) { // do not erase java.* references - matcher.appendReplacement(result, reference); + matcher.appendReplacement(result, Matcher.quoteReplacement(reference)); } else { matcher.appendReplacement(result, "Ljava/lang/Object;"); }