|
23 | 23 | import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod; |
24 | 24 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
25 | 25 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 26 | +import java.util.Collection; |
26 | 27 | import java.util.Map; |
27 | 28 | import net.bytebuddy.asm.Advice; |
28 | 29 | import net.bytebuddy.description.enumeration.EnumerationDescription; |
29 | 30 | import net.bytebuddy.description.type.TypeDescription; |
30 | 31 | import net.bytebuddy.implementation.bytecode.assign.Assigner; |
31 | 32 | import net.bytebuddy.matcher.ElementMatcher; |
32 | 33 |
|
33 | | -@SuppressWarnings("EnumOrdinal") |
34 | 34 | public class MethodInstrumentation implements TypeInstrumentation { |
35 | 35 | private final String className; |
36 | | - private final Map<String, SpanKind> methodNames; |
| 36 | + private final Map<SpanKind, Collection<String>> methodNames; |
37 | 37 |
|
38 | | - public MethodInstrumentation(String className, Map<String, SpanKind> methodNames) { |
| 38 | + public MethodInstrumentation(String className, Map<SpanKind, Collection<String>> methodNames) { |
39 | 39 | this.className = className; |
40 | 40 | this.methodNames = methodNames; |
41 | 41 | } |
@@ -63,36 +63,37 @@ public ElementMatcher<TypeDescription> typeMatcher() { |
63 | 63 |
|
64 | 64 | @Override |
65 | 65 | public void transform(TypeTransformer transformer) { |
66 | | - transformer.applyAdviceToMethod( |
67 | | - namedOneOf(methodNames.keySet().toArray(new String[0])).and(isMethod()), |
68 | | - mapping -> |
69 | | - mapping |
70 | | - .bind( |
71 | | - MethodReturnType.class, |
72 | | - (instrumentedType, instrumentedMethod, assigner, argumentHandler, sort) -> |
73 | | - Advice.OffsetMapping.Target.ForStackManipulation.of( |
74 | | - instrumentedMethod.getReturnType().asErasure())) |
75 | | - .bind( |
76 | | - SpanKindOrdinal.class, |
77 | | - (instrumentedType, instrumentedMethod, assigner, argumentHandler, sort) -> |
78 | | - Advice.OffsetMapping.Target.ForStackManipulation.of( |
79 | | - new EnumerationDescription.ForLoadedEnumeration( |
80 | | - methodNames.get(instrumentedMethod.getName())))), |
81 | | - MethodInstrumentation.class.getName() + "$MethodAdvice"); |
| 66 | + for (Map.Entry<SpanKind, Collection<String>> entry : methodNames.entrySet()) { |
| 67 | + SpanKind spanKind = entry.getKey(); |
| 68 | + Collection<String> names = entry.getValue(); |
| 69 | + transformer.applyAdviceToMethod( |
| 70 | + namedOneOf(names.toArray(new String[0])).and(isMethod()), |
| 71 | + mapping -> |
| 72 | + mapping |
| 73 | + .bind( |
| 74 | + MethodReturnType.class, |
| 75 | + (instrumentedType, instrumentedMethod, assigner, argumentHandler, sort) -> |
| 76 | + Advice.OffsetMapping.Target.ForStackManipulation.of( |
| 77 | + instrumentedMethod.getReturnType().asErasure())) |
| 78 | + .bind( |
| 79 | + MethodSpanKind.class, |
| 80 | + new EnumerationDescription.ForLoadedEnumeration(spanKind)), |
| 81 | + MethodInstrumentation.class.getName() + "$MethodAdvice"); |
| 82 | + } |
82 | 83 | } |
83 | 84 |
|
84 | 85 | // custom annotation that represents the return type of the method |
85 | 86 | @interface MethodReturnType {} |
86 | 87 |
|
87 | 88 | // custom annotation that represents the SpanKind of the method |
88 | | - @interface SpanKindOrdinal {} |
| 89 | + @interface MethodSpanKind {} |
89 | 90 |
|
90 | 91 | @SuppressWarnings("unused") |
91 | 92 | public static class MethodAdvice { |
92 | 93 |
|
93 | 94 | @Advice.OnMethodEnter(suppress = Throwable.class) |
94 | 95 | public static void onEnter( |
95 | | - @SpanKindOrdinal SpanKind spanKind, |
| 96 | + @MethodSpanKind SpanKind spanKind, |
96 | 97 | @Advice.Origin("#t") Class<?> declaringClass, |
97 | 98 | @Advice.Origin("#m") String methodName, |
98 | 99 | @Advice.Local("otelMethod") MethodAndType classAndMethod, |
|
0 commit comments