Skip to content

Commit 054ddb4

Browse files
committed
Backward compatibility with older versions @WithSpan
1 parent 7f6c0a0 commit 054ddb4

File tree

3 files changed

+65
-11
lines changed
  • instrumentation
    • opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations
    • spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations

3 files changed

+65
-11
lines changed

instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/AnnotationSingletons.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor;
1818
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1919
import io.opentelemetry.instrumentation.api.semconv.util.SpanNames;
20+
import java.lang.invoke.MethodHandle;
21+
import java.lang.invoke.MethodHandles;
22+
import java.lang.invoke.MethodType;
2023
import java.lang.reflect.Method;
2124
import java.util.logging.Logger;
2225

@@ -31,6 +34,21 @@ public final class AnnotationSingletons {
3134
createInstrumenterWithAttributes();
3235
private static final SpanAttributesExtractor ATTRIBUTES = createAttributesExtractor();
3336

37+
// The reason for using reflection here is that it needs to be compatible with the old version of
38+
// @WithSpan annotation that does not include the withParent option to avoid failing the muzzle
39+
// check.
40+
private static MethodHandle withParentMethodHandle = null;
41+
42+
static {
43+
try {
44+
withParentMethodHandle =
45+
MethodHandles.publicLookup()
46+
.findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class));
47+
} catch (NoSuchMethodException | IllegalAccessException ignore) {
48+
// ignore
49+
}
50+
}
51+
3452
public static Instrumenter<Method, Object> instrumenter() {
3553
return INSTRUMENTER;
3654
}
@@ -115,12 +133,20 @@ private static Context parentContextFromMethodRequest(
115133

116134
private static Context parentContextFromMethod(
117135
Context context, Method method, Attributes attributes) {
118-
WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class);
119-
if (annotation.withParent()) {
136+
if (withParentMethodHandle == null) {
120137
return context;
121138
}
122139

123-
return Context.root();
140+
WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class);
141+
142+
boolean withParent = true;
143+
try {
144+
withParent = (boolean) withParentMethodHandle.invoke(annotation);
145+
} catch (Throwable ignore) {
146+
// ignore
147+
}
148+
149+
return withParent ? context : Context.root();
124150
}
125151

126152
private AnnotationSingletons() {}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
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;
1114
import java.lang.reflect.Method;
15+
import javax.annotation.Nullable;
1216
import org.aspectj.lang.JoinPoint;
1317
import org.aspectj.lang.reflect.MethodSignature;
1418

@@ -60,6 +64,21 @@ interface Factory {
6064

6165
static final class InstrumentationAnnotationFactory implements Factory {
6266

67+
// The reason for using reflection here is that it needs to be compatible with the old version
68+
// of @WithSpan annotation that does not include the withParent option to avoid failing the
69+
// muzzle check.
70+
private static MethodHandle withParentMethodHandle = null;
71+
72+
static {
73+
try {
74+
withParentMethodHandle =
75+
MethodHandles.publicLookup()
76+
.findVirtual(WithSpan.class, "withParent", MethodType.methodType(boolean.class));
77+
} catch (NoSuchMethodException | IllegalAccessException ignore) {
78+
// ignore
79+
}
80+
}
81+
6382
@Override
6483
public JoinPointRequest create(JoinPoint joinPoint) {
6584
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
@@ -72,9 +91,21 @@ public JoinPointRequest create(JoinPoint joinPoint) {
7291
WithSpan annotation = method.getDeclaredAnnotation(WithSpan.class);
7392
String spanName = annotation != null ? annotation.value() : "";
7493
SpanKind spanKind = annotation != null ? annotation.kind() : SpanKind.INTERNAL;
75-
boolean withParent = annotation == null || annotation.withParent();
7694

77-
return new JoinPointRequest(joinPoint, method, spanName, spanKind, withParent);
95+
return new JoinPointRequest(
96+
joinPoint, method, spanName, spanKind, withParentValueFrom(annotation));
97+
}
98+
99+
private static boolean withParentValueFrom(@Nullable WithSpan annotation) {
100+
if (annotation == null || withParentMethodHandle == null) {
101+
return true;
102+
}
103+
104+
try {
105+
return (boolean) withParentMethodHandle.invoke(annotation);
106+
} catch (Throwable ignore) {
107+
return true;
108+
}
78109
}
79110
}
80111
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,9 @@ abstract class WithSpanAspect {
5959
this.requestFactory = requestFactory;
6060
}
6161

62-
static Context parentContext(Context parentContext, JoinPointRequest request, Attributes unused) {
63-
if (request.withParent()) {
64-
return parentContext;
65-
}
66-
67-
return Context.root();
62+
private static Context parentContext(
63+
Context parentContext, JoinPointRequest request, Attributes unused) {
64+
return request.withParent() ? parentContext : Context.root();
6865
}
6966

7067
public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable {

0 commit comments

Comments
 (0)