Skip to content

Commit

Permalink
(chore) make indy compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
johnrengelman committed Nov 7, 2024
1 parent cf59135 commit 4a71280
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends ExecInitializer> initializers) {
initializers =
ImmutableList.<ExecInitializer>builder()
.addAll(initializers)
.add(OpenTelemetryExecInitializer.INSTANCE)
.build();
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
@Advice.AssignReturned.ToArguments(@ToArgument(0))
public static ImmutableList<? extends ExecInitializer> enter(
@Advice.Argument(0) ImmutableList<? extends ExecInitializer> initializers) {
return ImmutableList.<ExecInitializer>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<? extends ExecInterceptor> interceptors) {
interceptors =
ImmutableList.<ExecInterceptor>builder()
.addAll(interceptors)
.add(OpenTelemetryExecInterceptor.INSTANCE)
.build();
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
@Advice.AssignReturned.ToArguments(@ToArgument(0))
public static ImmutableList<? extends ExecInterceptor> enter(
@Advice.Argument(0) ImmutableList<? extends ExecInterceptor> interceptors) {
return ImmutableList.<ExecInterceptor>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<? extends ExecInitializer> initializers,
@Advice.FieldValue(value = "interceptors", readOnly = false)
ImmutableList<? extends ExecInterceptor> 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<? extends ExecInitializer> initializers,
@Advice.FieldValue("interceptors") ImmutableList<? extends ExecInterceptor> interceptors) {
return new Object[] {
ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE),
ImmutableList.of(OpenTelemetryExecInterceptor.INSTANCE)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public String getModuleGroup() {
return "netty";
}

@Override
public boolean isIndyModule() {
return true;
}

@Override
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
// Only activate when running ratpack 1.7 or later
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,30 +42,40 @@ 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")
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
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())));
}
}
}

0 comments on commit 4a71280

Please sign in to comment.