|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.instrumentationannotations; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.instrumentation.instrumentationannotations.KotlinCoroutineUtil.isKotlinSuspendMethod; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.hasParameters; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.not; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.whereAny; |
| 15 | + |
| 16 | +import com.google.common.base.Stopwatch; |
| 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | +import net.bytebuddy.description.annotation.AnnotationSource; |
| 22 | +import net.bytebuddy.description.method.MethodDescription; |
| 23 | +import net.bytebuddy.description.type.TypeDescription; |
| 24 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
| 25 | +import net.bytebuddy.matcher.ElementMatcher; |
| 26 | + |
| 27 | +public class CountedInstrumentation implements TypeInstrumentation { |
| 28 | + |
| 29 | + private final ElementMatcher.Junction<AnnotationSource> annotatedMethodMatcher; |
| 30 | + private final ElementMatcher.Junction<MethodDescription> annotatedParametersMatcher; |
| 31 | + // this matcher matches all methods that should be excluded from transformation |
| 32 | + private final ElementMatcher.Junction<MethodDescription> excludedMethodsMatcher; |
| 33 | + |
| 34 | + CountedInstrumentation() { |
| 35 | + annotatedMethodMatcher = |
| 36 | + isAnnotatedWith(named("application.io.opentelemetry.instrumentation.annotations.Counted")); |
| 37 | + annotatedParametersMatcher = |
| 38 | + hasParameters( |
| 39 | + whereAny( |
| 40 | + isAnnotatedWith( |
| 41 | + named( |
| 42 | + "application.io.opentelemetry.instrumentation.annotations.MetricAttribute")))); |
| 43 | + // exclude all kotlin suspend methods, these are handle in kotlinx-coroutines instrumentation |
| 44 | + excludedMethodsMatcher = |
| 45 | + AnnotationExcludedMethods.configureExcludedMethods().or(isKotlinSuspendMethod()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 50 | + return declaresMethod(annotatedMethodMatcher); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void transform(TypeTransformer transformer) { |
| 55 | + ElementMatcher.Junction<MethodDescription> countedMethods = |
| 56 | + annotatedMethodMatcher.and(not(excludedMethodsMatcher)); |
| 57 | + |
| 58 | + ElementMatcher.Junction<MethodDescription> timedMethodsWithParameters = |
| 59 | + countedMethods.and(annotatedParametersMatcher); |
| 60 | + |
| 61 | + ElementMatcher.Junction<MethodDescription> timedMethodsWithoutParameters = |
| 62 | + countedMethods.and(not(annotatedParametersMatcher)); |
| 63 | + |
| 64 | + transformer.applyAdviceToMethod( |
| 65 | + timedMethodsWithoutParameters, CountedInstrumentation.class.getName() + "$CountedAdvice"); |
| 66 | + |
| 67 | + // Only apply advice for tracing parameters as attributes if any of the parameters are annotated |
| 68 | + // with @MetricsAttribute to avoid unnecessarily copying the arguments into an array. |
| 69 | + transformer.applyAdviceToMethod( |
| 70 | + timedMethodsWithParameters, |
| 71 | + CountedInstrumentation.class.getName() + "$CountedAttributesAdvice"); |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("unused") |
| 75 | + public static class CountedAttributesAdvice { |
| 76 | + |
| 77 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 78 | + public static void onEnter( |
| 79 | + @Advice.Origin Method originMethod, |
| 80 | + @Advice.Local("otelMethod") Method method, |
| 81 | + @Advice.AllArguments(typing = Assigner.Typing.DYNAMIC) Object[] args, |
| 82 | + @Advice.Local("otelRequest") MethodRequest request, |
| 83 | + @Advice.Local("stopwatch") Stopwatch stopwatch) { |
| 84 | + |
| 85 | + // Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it |
| 86 | + // to local variable so that there would be only one call to Class.getMethod. |
| 87 | + method = originMethod; |
| 88 | + request = new MethodRequest(method, args); |
| 89 | + } |
| 90 | + |
| 91 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 92 | + public static void onExit( |
| 93 | + @Advice.Local("otelMethod") Method method, |
| 94 | + @Advice.Local("otelRequest") MethodRequest request, |
| 95 | + @Advice.Local("stopwatch") Stopwatch stopwatch, |
| 96 | + @Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, |
| 97 | + @Advice.Thrown Throwable throwable) { |
| 98 | + AnnotationSingletons.recordCountWithAttributes(request); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @SuppressWarnings("unused") |
| 103 | + public static class CountedAdvice { |
| 104 | + |
| 105 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 106 | + public static void onEnter( |
| 107 | + @Advice.Origin Method originMethod, |
| 108 | + @Advice.Local("otelMethod") Method method, |
| 109 | + @Advice.Local("stopwatch") Stopwatch stopwatch) { |
| 110 | + // Every usage of @Advice.Origin Method is replaced with a call to Class.getMethod, copy it |
| 111 | + // to local variable so that there would be only one call to Class.getMethod. |
| 112 | + method = originMethod; |
| 113 | + } |
| 114 | + |
| 115 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 116 | + public static void stopSpan( |
| 117 | + @Advice.Local("otelMethod") Method method, |
| 118 | + @Advice.Local("stopwatch") Stopwatch stopwatch, |
| 119 | + @Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue, |
| 120 | + @Advice.Thrown Throwable throwable) { |
| 121 | + AnnotationSingletons.recordCount(method); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments