From 093aeaab3e134628372511129d90e2433c2ddb85 Mon Sep 17 00:00:00 2001 From: John Engelman Date: Wed, 6 Nov 2024 21:20:18 -0600 Subject: [PATCH] (chore) make indy compatible --- .../DefaultExecControllerInstrumentation.java | 59 +++++++++++-------- .../v1_7/HttpClientInstrumentation.java | 8 +-- .../v1_7/RatpackInstrumentationModule.java | 5 ++ .../RequestActionSupportInstrumentation.java | 35 +++++++---- .../v1_7/ServerRegistryInstrumentation.java | 14 ++--- 5 files changed, 72 insertions(+), 49 deletions(-) diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/DefaultExecControllerInstrumentation.java b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/DefaultExecControllerInstrumentation.java index 7bdef2aaa609..3d16352fbe5a 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/DefaultExecControllerInstrumentation.java +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/DefaultExecControllerInstrumentation.java @@ -15,6 +15,8 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import net.bytebuddy.asm.Advice; +import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; +import net.bytebuddy.asm.Advice.AssignReturned.ToFields.ToField; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; import ratpack.exec.ExecInitializer; @@ -44,43 +46,48 @@ public void transform(TypeTransformer transformer) { DefaultExecControllerInstrumentation.class.getName() + "$ConstructorAdvice"); } + @SuppressWarnings("unused") public static class SetInitializersAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void enter( - @Advice.Argument(value = 0, readOnly = false) - ImmutableList initializers) { - initializers = - ImmutableList.builder() - .addAll(initializers) - .add(OpenTelemetryExecInitializer.INSTANCE) - .build(); + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToArguments(@ToArgument(0)) + public static ImmutableList enter( + @Advice.Argument(0) ImmutableList initializers) { + return ImmutableList.builder() + .addAll(initializers) + .add(OpenTelemetryExecInitializer.INSTANCE) + .build(); } } + @SuppressWarnings("unused") public static class SetInterceptorsAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static void enter( - @Advice.Argument(value = 0, readOnly = false) - ImmutableList interceptors) { - interceptors = - ImmutableList.builder() - .addAll(interceptors) - .add(OpenTelemetryExecInterceptor.INSTANCE) - .build(); + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToArguments(@ToArgument(0)) + public static ImmutableList enter( + @Advice.Argument(0) ImmutableList interceptors) { + return ImmutableList.builder() + .addAll(interceptors) + .add(OpenTelemetryExecInterceptor.INSTANCE) + .build(); } } + @SuppressWarnings("unused") public static class ConstructorAdvice { @SuppressWarnings("UnusedVariable") - @Advice.OnMethodExit(suppress = Throwable.class) - public static void exit( - @Advice.FieldValue(value = "initializers", readOnly = false) - ImmutableList initializers, - @Advice.FieldValue(value = "interceptors", readOnly = false) - ImmutableList interceptors) { - initializers = ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE); - interceptors = ImmutableList.of(OpenTelemetryExecInterceptor.INSTANCE); + @Advice.OnMethodExit(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToFields({ + @ToField(value = "initializers", index = 0), + @ToField(value = "interceptors", index = 1) + }) + public static Object[] exit( + @Advice.FieldValue("initializers") ImmutableList initializers, + @Advice.FieldValue("interceptors") ImmutableList interceptors) { + return new Object[] { + ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE), + ImmutableList.of(OpenTelemetryExecInterceptor.INSTANCE) + }; } } } diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/HttpClientInstrumentation.java b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/HttpClientInstrumentation.java index d14e25458201..0fcdef428d80 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/HttpClientInstrumentation.java +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/HttpClientInstrumentation.java @@ -37,10 +37,10 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class OfAdvice { - @Advice.OnMethodExit(suppress = Throwable.class) - public static void injectTracing(@Advice.Return(readOnly = false) HttpClient httpClient) - throws Exception { - httpClient = RatpackSingletons.telemetry().instrumentHttpClient(httpClient); + @Advice.OnMethodExit(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToReturned + public static HttpClient injectTracing(@Advice.Return HttpClient httpClient) throws Exception { + return RatpackSingletons.telemetry().instrumentHttpClient(httpClient); } } } diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RatpackInstrumentationModule.java b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RatpackInstrumentationModule.java index a98e61d4e0f2..3f00fa32bfc0 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RatpackInstrumentationModule.java +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RatpackInstrumentationModule.java @@ -28,6 +28,11 @@ public String getModuleGroup() { return "netty"; } + @Override + public boolean isIndyModule() { + return true; + } + @Override public ElementMatcher.Junction classLoaderMatcher() { // Only activate when running ratpack 1.7 or later diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RequestActionSupportInstrumentation.java b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RequestActionSupportInstrumentation.java index a156ddb638ba..e57b93735fd7 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RequestActionSupportInstrumentation.java +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/RequestActionSupportInstrumentation.java @@ -18,6 +18,7 @@ import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import net.bytebuddy.asm.Advice; +import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; import ratpack.exec.Downstream; @@ -41,7 +42,10 @@ public void transform(TypeTransformer transformer) { RequestActionSupportInstrumentation.class.getName() + "$SendAdvice"); transformer.applyAdviceToMethod( isMethod().and(named("connect")).and(takesArgument(0, named("ratpack.exec.Downstream"))), - RequestActionSupportInstrumentation.class.getName() + "$ConnectAdvice"); + RequestActionSupportInstrumentation.class.getName() + "$ConnectDownstreamAdvice"); + transformer.applyAdviceToMethod( + isMethod().and(named("connect")).and(takesArgument(0, named("ratpack.exec.Downstream"))), + RequestActionSupportInstrumentation.class.getName() + "$ContextAdvice"); } @SuppressWarnings("unused") @@ -49,22 +53,29 @@ public static class SendAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void injectChannelAttribute( - @Advice.FieldValue("execution") Execution execution, - @Advice.Argument(value = 0, readOnly = false) Downstream downstream, - @Advice.Argument(value = 1, readOnly = false) Channel channel) { + @Advice.FieldValue("execution") Execution execution, @Advice.Argument(1) Channel channel) { RatpackSingletons.propagateContextToChannel(execution, channel); } } - public static class ConnectAdvice { + @SuppressWarnings("unused") + public static class ConnectDownstreamAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static Scope injectChannelAttribute( - @Advice.FieldValue("execution") Execution execution, - @Advice.Argument(value = 0, readOnly = false) Downstream downstream) { + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToArguments(@ToArgument(0)) + public static Object wrapDownstream(@Advice.Argument(0) Downstream downstream) { // Propagate the current context to downstream - // since that the is the subsequent - downstream = DownstreamWrapper.wrapIfNeeded(downstream); + // since that is the subsequent code chained to the http client call + return DownstreamWrapper.wrapIfNeeded(downstream); + } + } + + @SuppressWarnings("unused") + public static class ContextAdvice { + + @Advice.OnMethodEnter(suppress = Throwable.class, inline = false) + public static Scope injectChannelAttribute( + @Advice.FieldValue("execution") Execution execution) { // Capture the CLIENT span and make it current before cally Netty layer return execution @@ -74,7 +85,7 @@ public static Scope injectChannelAttribute( .orElse(null); } - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false) public static void exit(@Advice.Enter Scope scope) { if (scope != null) { scope.close(); diff --git a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/ServerRegistryInstrumentation.java b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/ServerRegistryInstrumentation.java index df23c3a650e9..50d055bdb991 100644 --- a/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/ServerRegistryInstrumentation.java +++ b/instrumentation/ratpack/ratpack-1.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/v1_7/ServerRegistryInstrumentation.java @@ -34,13 +34,13 @@ public void transform(TypeTransformer transformer) { @SuppressWarnings("unused") public static class BuildAdvice { - @Advice.OnMethodExit(suppress = Throwable.class) - public static void injectTracing(@Advice.Return(readOnly = false) Registry registry) { - registry = - registry.join( - Registry.single( - HandlerDecorator.prepend( - RatpackSingletons.telemetry().getOpenTelemetryServerHandler()))); + @Advice.OnMethodExit(suppress = Throwable.class, inline = false) + @Advice.AssignReturned.ToReturned + public static Registry injectTracing(@Advice.Return Registry registry) { + return registry.join( + Registry.single( + HandlerDecorator.prepend( + RatpackSingletons.telemetry().getOpenTelemetryServerHandler()))); } } }