|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.ratpack.v1_7; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.isConstructor; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 11 | + |
| 12 | +import com.google.common.collect.ImmutableList; |
| 13 | +import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInitializer; |
| 14 | +import io.opentelemetry.instrumentation.ratpack.v1_7.OpenTelemetryExecInterceptor; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 16 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 17 | +import net.bytebuddy.asm.Advice; |
| 18 | +import net.bytebuddy.description.type.TypeDescription; |
| 19 | +import net.bytebuddy.matcher.ElementMatcher; |
| 20 | +import ratpack.exec.ExecInitializer; |
| 21 | +import ratpack.exec.ExecInterceptor; |
| 22 | + |
| 23 | +public class DefaultExecControllerInstrumentation implements TypeInstrumentation { |
| 24 | + |
| 25 | + @Override |
| 26 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 27 | + return named("ratpack.exec.internal.DefaultExecController"); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void transform(TypeTransformer transformer) { |
| 32 | + transformer.applyAdviceToMethod( |
| 33 | + named("setInitializers") |
| 34 | + .and(takesArgument(0, named("com.google.common.collect.ImmutableList"))), |
| 35 | + DefaultExecControllerInstrumentation.class.getName() + "$SetInitializersAdvice"); |
| 36 | + |
| 37 | + transformer.applyAdviceToMethod( |
| 38 | + named("setInterceptors") |
| 39 | + .and(takesArgument(0, named("com.google.common.collect.ImmutableList"))), |
| 40 | + DefaultExecControllerInstrumentation.class.getName() + "$SetInterceptorsAdvice"); |
| 41 | + |
| 42 | + transformer.applyAdviceToMethod( |
| 43 | + isConstructor(), |
| 44 | + DefaultExecControllerInstrumentation.class.getName() + "$ConstructorAdvice"); |
| 45 | + } |
| 46 | + |
| 47 | + public static class SetInitializersAdvice { |
| 48 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 49 | + public static void enter( |
| 50 | + @Advice.Argument(value = 0, readOnly = false) |
| 51 | + ImmutableList<? extends ExecInitializer> initializers) { |
| 52 | + initializers = |
| 53 | + ImmutableList.<ExecInitializer>builder() |
| 54 | + .addAll(initializers) |
| 55 | + .add(OpenTelemetryExecInitializer.INSTANCE) |
| 56 | + .build(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public static class SetInterceptorsAdvice { |
| 61 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 62 | + public static void enter( |
| 63 | + @Advice.Argument(value = 0, readOnly = false) |
| 64 | + ImmutableList<? extends ExecInterceptor> interceptors) { |
| 65 | + interceptors = |
| 66 | + ImmutableList.<ExecInterceptor>builder() |
| 67 | + .addAll(interceptors) |
| 68 | + .add(OpenTelemetryExecInterceptor.INSTANCE) |
| 69 | + .build(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static class ConstructorAdvice { |
| 74 | + |
| 75 | + @SuppressWarnings("UnusedVariable") |
| 76 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 77 | + public static void exit( |
| 78 | + @Advice.FieldValue(value = "initializers", readOnly = false) |
| 79 | + ImmutableList<? extends ExecInitializer> initializers, |
| 80 | + @Advice.FieldValue(value = "interceptors", readOnly = false) |
| 81 | + ImmutableList<? extends ExecInterceptor> interceptors) { |
| 82 | + initializers = ImmutableList.of(OpenTelemetryExecInitializer.INSTANCE); |
| 83 | + interceptors = ImmutableList.of(OpenTelemetryExecInterceptor.INSTANCE); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments