|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.tooling.muzzle; |
| 7 | + |
| 8 | +import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; |
| 9 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 10 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 11 | +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; |
| 12 | +import java.util.Collection; |
| 13 | +import java.util.HashSet; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.function.Function; |
| 16 | +import javax.annotation.Nullable; |
| 17 | +import net.bytebuddy.agent.builder.AgentBuilder; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import net.bytebuddy.description.annotation.AnnotationDescription; |
| 20 | +import net.bytebuddy.description.annotation.AnnotationValue; |
| 21 | +import net.bytebuddy.description.method.MethodDescription; |
| 22 | +import net.bytebuddy.description.method.MethodList; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.dynamic.ClassFileLocator; |
| 25 | +import net.bytebuddy.matcher.ElementMatcher; |
| 26 | +import net.bytebuddy.pool.TypePool; |
| 27 | + |
| 28 | +/** |
| 29 | + * Inspect advice classes used by the given instrumentation module and determine whether the |
| 30 | + * instrumentation should inject helper classes into the instrumented classloader or load them into |
| 31 | + * separate class loader. The decision is made based on the {@code inline} attribute of {@link |
| 32 | + * Advice.OnMethodEnter} and {@link Advice.OnMethodExit} annotations. |
| 33 | + */ |
| 34 | +public class AdviceInspector { |
| 35 | + |
| 36 | + private final ClassFileLocator classFileLocator; |
| 37 | + |
| 38 | + public AdviceInspector(ClassFileLocator classFileLocator) { |
| 39 | + this.classFileLocator = classFileLocator; |
| 40 | + } |
| 41 | + |
| 42 | + public boolean useIsolatedAdvice(InstrumentationModule instrumentationModule) { |
| 43 | + Set<String> adviceClassNames = new HashSet<>(); |
| 44 | + for (TypeInstrumentation typeInstrumentation : instrumentationModule.typeInstrumentations()) { |
| 45 | + typeInstrumentation.transform( |
| 46 | + new TypeTransformer() { |
| 47 | + @Override |
| 48 | + public void applyAdviceToMethod( |
| 49 | + ElementMatcher<? super MethodDescription> methodMatcher, |
| 50 | + Function<Advice.WithCustomMapping, Advice.WithCustomMapping> mappingCustomizer, |
| 51 | + String adviceClassName) { |
| 52 | + adviceClassNames.add(adviceClassName); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void applyTransformer(AgentBuilder.Transformer transformer) {} |
| 57 | + }); |
| 58 | + } |
| 59 | + Boolean result = useIsolatedAdvice(instrumentationModule, adviceClassNames); |
| 60 | + |
| 61 | + // we aren't able to tell whether the instrumentation is ready for indy instrumentation or not, |
| 62 | + // so we assume that it is not |
| 63 | + return result != null ? result : false; |
| 64 | + } |
| 65 | + |
| 66 | + @Nullable |
| 67 | + public Boolean useIsolatedAdvice( |
| 68 | + InstrumentationModule instrumentationModule, Collection<String> adviceClassNames) { |
| 69 | + TypePool typePool = AgentBuilder.PoolStrategy.Default.FAST.typePool(classFileLocator, null); |
| 70 | + |
| 71 | + int inlineCount = 0; |
| 72 | + int nonInlineCount = 0; |
| 73 | + for (String adviceClassName : adviceClassNames) { |
| 74 | + TypeDescription type = typePool.describe(adviceClassName).resolve(); |
| 75 | + MethodList<MethodDescription.InDefinedShape> methodList = type.getDeclaredMethods(); |
| 76 | + for (MethodDescription.InDefinedShape method : methodList) { |
| 77 | + for (AnnotationDescription annotation : method.getDeclaredAnnotations()) { |
| 78 | + if (Advice.OnMethodEnter.class.getName().equals(annotation.getAnnotationType().getName()) |
| 79 | + || Advice.OnMethodExit.class |
| 80 | + .getName() |
| 81 | + .equals(annotation.getAnnotationType().getName())) { |
| 82 | + AnnotationValue<?, ?> value = annotation.getValue("inline"); |
| 83 | + // While it is possible to use non-inline advice with the non-indy instrumentation, by |
| 84 | + // adding the advice classes as helper classes, we assume that nobody relies on that. |
| 85 | + // Having a non-inline advice is treated as a marker that the instrumentation can use |
| 86 | + // indy. |
| 87 | + // Similarly inline advice could be used with indy instrumentation, but we assume that |
| 88 | + // if inline advice is used, then it is not indy ready. |
| 89 | + if (value.getState().isDefined() && Boolean.FALSE.equals(value.resolve())) { |
| 90 | + nonInlineCount++; |
| 91 | + } else { |
| 92 | + inlineCount++; |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + // mixed inline and non-inline advice |
| 99 | + if (inlineCount > 0 && nonInlineCount > 0) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + if (inlineCount > 0) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + // While it is possible to use non-inline advice with the non-indy instrumentation, by |
| 106 | + // adding the advice classes as helper classes, we assume that nobody relies on that. |
| 107 | + // Having all advice non-inline is treated as a marker that the instrumentation can use indy. |
| 108 | + // Similarly inline advice could be used with indy instrumentation. For now, we default to |
| 109 | + // non-indy instrumentation in that case, but users can override that by explicitly choosing |
| 110 | + // whether to inject or isolate helper classes when mixed advice is used. |
| 111 | + if (nonInlineCount > 0) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + // no advice annotations were used so the instrumentation is using an AgentBuilder.Transformer |
| 116 | + if (instrumentationModule instanceof ExperimentalInstrumentationModule) { |
| 117 | + ExperimentalInstrumentationModule experimentalModule = |
| 118 | + (ExperimentalInstrumentationModule) instrumentationModule; |
| 119 | + // injected class names makes sense only for indy instrumentation |
| 120 | + if (!experimentalModule.injectedClassNames().isEmpty()) { |
| 121 | + return true; |
| 122 | + } |
| 123 | + if (!experimentalModule.exposedClassNames().isEmpty()) { |
| 124 | + return true; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // we aren't able to tell whether the instrumentation is ready for indy instrumentation or not |
| 129 | + return null; |
| 130 | + } |
| 131 | +} |
0 commit comments