Skip to content

Commit c36eedd

Browse files
authored
Fix instrumentation failure when constructor has @WithSpan annotation (#13929)
1 parent 8604da3 commit c36eedd

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static net.bytebuddy.matcher.ElementMatchers.hasParameters;
1111
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
1212
import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy;
13+
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1314
import static net.bytebuddy.matcher.ElementMatchers.named;
1415
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
1516
import static net.bytebuddy.matcher.ElementMatchers.none;
@@ -79,13 +80,13 @@ public void transform(TypeTransformer transformer) {
7980
tracedMethods.and(not(annotatedParametersMatcher));
8081

8182
transformer.applyAdviceToMethod(
82-
tracedMethodsWithoutParameters,
83+
tracedMethodsWithoutParameters.and(isMethod()),
8384
WithSpanInstrumentation.class.getName() + "$WithSpanAdvice");
8485

8586
// Only apply advice for tracing parameters as attributes if any of the parameters are annotated
8687
// with @SpanAttribute to avoid unnecessarily copying the arguments into an array.
8788
transformer.applyAdviceToMethod(
88-
tracedMethodsWithParameters,
89+
tracedMethodsWithParameters.and(isMethod()),
8990
WithSpanInstrumentation.class.getName() + "$WithSpanAttributesAdvice");
9091
}
9192

instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
@SuppressWarnings("deprecation") // testing instrumentation of deprecated class
1313
public class TracedWithSpan {
1414

15+
public TracedWithSpan() {}
16+
17+
// used to verify that constructor with @WithSpan annotation doesn't break instrumentation
18+
@io.opentelemetry.extension.annotations.WithSpan
19+
public TracedWithSpan(String unused) {}
20+
1521
@io.opentelemetry.extension.annotations.WithSpan
1622
public String otel() {
1723
return "hello!";

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import static net.bytebuddy.matcher.ElementMatchers.declaresMethod;
1212
import static net.bytebuddy.matcher.ElementMatchers.hasParameters;
1313
import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith;
14+
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
1415
import static net.bytebuddy.matcher.ElementMatchers.named;
1516
import static net.bytebuddy.matcher.ElementMatchers.not;
1617
import static net.bytebuddy.matcher.ElementMatchers.whereAny;
@@ -66,13 +67,13 @@ public void transform(TypeTransformer transformer) {
6667
tracedMethods.and(not(annotatedParametersMatcher));
6768

6869
transformer.applyAdviceToMethod(
69-
tracedMethodsWithoutParameters,
70+
tracedMethodsWithoutParameters.and(isMethod()),
7071
WithSpanInstrumentation.class.getName() + "$WithSpanAdvice");
7172

7273
// Only apply advice for tracing parameters as attributes if any of the parameters are annotated
7374
// with @SpanAttribute to avoid unnecessarily copying the arguments into an array.
7475
transformer.applyAdviceToMethod(
75-
tracedMethodsWithParameters,
76+
tracedMethodsWithParameters.and(isMethod()),
7677
WithSpanInstrumentation.class.getName() + "$WithSpanAttributesAdvice");
7778
}
7879

instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
public class TracedWithSpan {
1515

16+
TracedWithSpan() {}
17+
18+
// used to verify that constructor with @WithSpan annotation doesn't break instrumentation
19+
@WithSpan
20+
TracedWithSpan(String unused) {}
21+
1622
@WithSpan
1723
public String otel() {
1824
return "hello!";

0 commit comments

Comments
 (0)