|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.extensionannotations; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.hasParameters; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.none; |
| 15 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 16 | +import static net.bytebuddy.matcher.ElementMatchers.whereAny; |
| 17 | + |
| 18 | +import io.opentelemetry.context.Context; |
| 19 | +import io.opentelemetry.context.Scope; |
| 20 | +import io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndSupport; |
| 21 | +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; |
| 22 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
| 23 | +import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; |
| 24 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 25 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 26 | +import io.opentelemetry.javaagent.tooling.config.MethodsConfigurationParser; |
| 27 | +import java.lang.reflect.Method; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import net.bytebuddy.asm.Advice; |
| 31 | +import net.bytebuddy.description.ByteCodeElement; |
| 32 | +import net.bytebuddy.description.annotation.AnnotationSource; |
| 33 | +import net.bytebuddy.description.method.MethodDescription; |
| 34 | +import net.bytebuddy.description.type.TypeDescription; |
| 35 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
| 36 | +import net.bytebuddy.matcher.ElementMatcher; |
| 37 | +import net.bytebuddy.matcher.ElementMatchers; |
| 38 | + |
| 39 | +public class WithSpanInstrumentation implements TypeInstrumentation { |
| 40 | + |
| 41 | + private static final String TRACE_ANNOTATED_METHODS_EXCLUDE_CONFIG = |
| 42 | + "otel.instrumentation.opentelemetry-annotations.exclude-methods"; |
| 43 | + |
| 44 | + private final ElementMatcher.Junction<AnnotationSource> annotatedMethodMatcher; |
| 45 | + private final ElementMatcher.Junction<MethodDescription> annotatedParametersMatcher; |
| 46 | + // this matcher matches all methods that should be excluded from transformation |
| 47 | + private final ElementMatcher.Junction<MethodDescription> excludedMethodsMatcher; |
| 48 | + |
| 49 | + WithSpanInstrumentation() { |
| 50 | + annotatedMethodMatcher = |
| 51 | + isAnnotatedWith(named("application.io.opentelemetry.extension.annotations.WithSpan")); |
| 52 | + annotatedParametersMatcher = |
| 53 | + hasParameters( |
| 54 | + whereAny( |
| 55 | + isAnnotatedWith( |
| 56 | + named("application.io.opentelemetry.extension.annotations.SpanAttribute")))); |
| 57 | + excludedMethodsMatcher = configureExcludedMethods(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 62 | + return declaresMethod(annotatedMethodMatcher); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void transform(TypeTransformer transformer) { |
| 67 | + ElementMatcher.Junction<MethodDescription> tracedMethods = |
| 68 | + annotatedMethodMatcher.and(not(excludedMethodsMatcher)); |
| 69 | + |
| 70 | + ElementMatcher.Junction<MethodDescription> tracedMethodsWithParameters = |
| 71 | + tracedMethods.and(annotatedParametersMatcher); |
| 72 | + ElementMatcher.Junction<MethodDescription> tracedMethodsWithoutParameters = |
| 73 | + tracedMethods.and(not(annotatedParametersMatcher)); |
| 74 | + |
| 75 | + transformer.applyAdviceToMethod( |
| 76 | + tracedMethodsWithoutParameters, |
| 77 | + WithSpanInstrumentation.class.getName() + "$WithSpanAdvice"); |
| 78 | + |
| 79 | + // Only apply advice for tracing parameters as attributes if any of the parameters are annotated |
| 80 | + // with @SpanAttribute to avoid unnecessarily copying the arguments into an array. |
| 81 | + transformer.applyAdviceToMethod( |
| 82 | + tracedMethodsWithParameters, |
| 83 | + WithSpanInstrumentation.class.getName() + "$WithSpanAttributesAdvice"); |
| 84 | + } |
| 85 | + |
| 86 | + /* |
| 87 | + Returns a matcher for all methods that should be excluded from auto-instrumentation by |
| 88 | + annotation-based advices. |
| 89 | + */ |
| 90 | + static ElementMatcher.Junction<MethodDescription> configureExcludedMethods() { |
| 91 | + ElementMatcher.Junction<MethodDescription> result = none(); |
| 92 | + |
| 93 | + Map<String, Set<String>> excludedMethods = |
| 94 | + MethodsConfigurationParser.parse( |
| 95 | + InstrumentationConfig.get().getString(TRACE_ANNOTATED_METHODS_EXCLUDE_CONFIG)); |
| 96 | + for (Map.Entry<String, Set<String>> entry : excludedMethods.entrySet()) { |
| 97 | + String className = entry.getKey(); |
| 98 | + ElementMatcher.Junction<ByteCodeElement> matcher = |
| 99 | + isDeclaredBy(ElementMatchers.named(className)); |
| 100 | + |
| 101 | + Set<String> methodNames = entry.getValue(); |
| 102 | + if (!methodNames.isEmpty()) { |
| 103 | + matcher = matcher.and(namedOneOf(methodNames.toArray(new String[0]))); |
| 104 | + } |
| 105 | + |
| 106 | + result = result.or(matcher); |
| 107 | + } |
| 108 | + |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + @SuppressWarnings("unused") |
| 113 | + public static class WithSpanAdvice { |
| 114 | + |
| 115 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 116 | + public static void onEnter( |
| 117 | + @Advice.Origin Method originMethod, |
| 118 | + @Advice.Local("otelMethod") Method method, |
| 119 | + @Advice.Local("otelContext") Context context, |
| 120 | + @Advice.Local("otelScope") Scope scope) { |
| 121 | + // Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it |
| 122 | + // to local variable so that there would be only one call to Class.getMethod. |
| 123 | + method = originMethod; |
| 124 | + |
| 125 | + Instrumenter<Method, Object> instrumenter = WithSpanSingletons.instrumenter(); |
| 126 | + Context current = Java8BytecodeBridge.currentContext(); |
| 127 | + |
| 128 | + if (instrumenter.shouldStart(current, method)) { |
| 129 | + context = instrumenter.start(current, method); |
| 130 | + scope = context.makeCurrent(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 135 | + public static void stopSpan( |
| 136 | + @Advice.Local("otelMethod") Method method, |
| 137 | + @Advice.Local("otelContext") Context context, |
| 138 | + @Advice.Local("otelScope") Scope scope, |
| 139 | + @Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, |
| 140 | + @Advice.Thrown Throwable throwable) { |
| 141 | + if (scope == null) { |
| 142 | + return; |
| 143 | + } |
| 144 | + scope.close(); |
| 145 | + |
| 146 | + AsyncOperationEndSupport<Method, Object> operationEndSupport = |
| 147 | + AsyncOperationEndSupport.create( |
| 148 | + WithSpanSingletons.instrumenter(), Object.class, method.getReturnType()); |
| 149 | + returnValue = operationEndSupport.asyncEnd(context, method, returnValue, throwable); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @SuppressWarnings("unused") |
| 154 | + public static class WithSpanAttributesAdvice { |
| 155 | + |
| 156 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 157 | + public static void onEnter( |
| 158 | + @Advice.Origin Method originMethod, |
| 159 | + @Advice.Local("otelMethod") Method method, |
| 160 | + @Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args, |
| 161 | + @Advice.Local("otelRequest") MethodRequest request, |
| 162 | + @Advice.Local("otelContext") Context context, |
| 163 | + @Advice.Local("otelScope") Scope scope) { |
| 164 | + |
| 165 | + // Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it |
| 166 | + // to local variable so that there would be only one call to Class.getMethod. |
| 167 | + method = originMethod; |
| 168 | + |
| 169 | + Instrumenter<MethodRequest, Object> instrumenter = |
| 170 | + WithSpanSingletons.instrumenterWithAttributes(); |
| 171 | + Context current = Java8BytecodeBridge.currentContext(); |
| 172 | + request = new MethodRequest(method, args); |
| 173 | + |
| 174 | + if (instrumenter.shouldStart(current, request)) { |
| 175 | + context = instrumenter.start(current, request); |
| 176 | + scope = context.makeCurrent(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 181 | + public static void stopSpan( |
| 182 | + @Advice.Local("otelMethod") Method method, |
| 183 | + @Advice.Local("otelRequest") MethodRequest request, |
| 184 | + @Advice.Local("otelContext") Context context, |
| 185 | + @Advice.Local("otelScope") Scope scope, |
| 186 | + @Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, |
| 187 | + @Advice.Thrown Throwable throwable) { |
| 188 | + if (scope == null) { |
| 189 | + return; |
| 190 | + } |
| 191 | + scope.close(); |
| 192 | + AsyncOperationEndSupport<MethodRequest, Object> operationEndSupport = |
| 193 | + AsyncOperationEndSupport.create( |
| 194 | + WithSpanSingletons.instrumenterWithAttributes(), |
| 195 | + Object.class, |
| 196 | + method.getReturnType()); |
| 197 | + returnValue = operationEndSupport.asyncEnd(context, request, returnValue, throwable); |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments