Skip to content

Commit 02cbe0c

Browse files
xiepuhuanxiepuhuan
authored and
xiepuhuan
committed
Use API to replace reflection to obtain inheritContext
1 parent c8d681a commit 02cbe0c

File tree

1 file changed

+2
-34
lines changed
  • instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations

1 file changed

+2
-34
lines changed

instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/JoinPointRequest.java

+2-34
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
import io.opentelemetry.api.trace.SpanKind;
99
import io.opentelemetry.instrumentation.annotations.WithSpan;
1010
import io.opentelemetry.instrumentation.api.semconv.util.SpanNames;
11-
import java.lang.invoke.MethodHandle;
12-
import java.lang.invoke.MethodHandles;
13-
import java.lang.invoke.MethodType;
1411
import java.lang.reflect.Method;
15-
import javax.annotation.Nullable;
1612
import org.aspectj.lang.JoinPoint;
1713
import org.aspectj.lang.reflect.MethodSignature;
1814

@@ -68,22 +64,6 @@ interface Factory {
6864

6965
static final class InstrumentationAnnotationFactory implements Factory {
7066

71-
// The reason for using reflection here is that it needs to be compatible with the old version
72-
// of @WithSpan annotation that does not include the inheritContext option to avoid
73-
// NoSuchMethodError
74-
private static MethodHandle inheritContextMethodHandle = null;
75-
76-
static {
77-
try {
78-
inheritContextMethodHandle =
79-
MethodHandles.publicLookup()
80-
.findVirtual(
81-
WithSpan.class, "inheritContext", MethodType.methodType(boolean.class));
82-
} catch (NoSuchMethodException | IllegalAccessException ignore) {
83-
// ignore
84-
}
85-
}
86-
8767
@Override
8868
public JoinPointRequest create(JoinPoint joinPoint) {
8969
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
@@ -96,21 +76,9 @@ public JoinPointRequest create(JoinPoint joinPoint) {
9676
WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class);
9777
String spanName = annotation != null ? annotation.value() : "";
9878
SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL;
79+
boolean inheritContext = annotation == null || annotation.inheritContext();
9980

100-
return new JoinPointRequest(
101-
joinPoint, method, spanName, spanKind, inheritContextValueFrom(annotation));
102-
}
103-
104-
private static boolean inheritContextValueFrom(@Nullable WithSpan annotation) {
105-
if (annotation == null || inheritContextMethodHandle == null) {
106-
return true;
107-
}
108-
109-
try {
110-
return (boolean) inheritContextMethodHandle.invoke(annotation);
111-
} catch (Throwable ignore) {
112-
return true;
113-
}
81+
return new JoinPointRequest(joinPoint, method, spanName, spanKind, inheritContext);
11482
}
11583
}
11684
}

0 commit comments

Comments
 (0)