diff --git a/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java b/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java index 4d99fcafbd3d..0891e1f9e297 100644 --- a/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java +++ b/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/extensionannotations/WithSpanInstrumentation.java @@ -10,6 +10,7 @@ import static net.bytebuddy.matcher.ElementMatchers.hasParameters; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import static net.bytebuddy.matcher.ElementMatchers.none; @@ -79,13 +80,13 @@ public void transform(TypeTransformer transformer) { tracedMethods.and(not(annotatedParametersMatcher)); transformer.applyAdviceToMethod( - tracedMethodsWithoutParameters, + tracedMethodsWithoutParameters.and(isMethod()), WithSpanInstrumentation.class.getName() + "$WithSpanAdvice"); // Only apply advice for tracing parameters as attributes if any of the parameters are annotated // with @SpanAttribute to avoid unnecessarily copying the arguments into an array. transformer.applyAdviceToMethod( - tracedMethodsWithParameters, + tracedMethodsWithParameters.and(isMethod()), WithSpanInstrumentation.class.getName() + "$WithSpanAttributesAdvice"); } diff --git a/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java b/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java index b7bd5eb34f00..66505ca93721 100644 --- a/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java +++ b/instrumentation/opentelemetry-extension-annotations-1.0/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java @@ -12,6 +12,12 @@ @SuppressWarnings("deprecation") // testing instrumentation of deprecated class public class TracedWithSpan { + public TracedWithSpan() {} + + // used to verify that constructor with @WithSpan annotation doesn't break instrumentation + @io.opentelemetry.extension.annotations.WithSpan + public TracedWithSpan(String unused) {} + @io.opentelemetry.extension.annotations.WithSpan public String otel() { return "hello!"; diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java index 74d9d84c00d3..975b4d866132 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/instrumentationannotations/WithSpanInstrumentation.java @@ -11,6 +11,7 @@ import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; import static net.bytebuddy.matcher.ElementMatchers.hasParameters; import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; +import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.not; import static net.bytebuddy.matcher.ElementMatchers.whereAny; @@ -66,13 +67,13 @@ public void transform(TypeTransformer transformer) { tracedMethods.and(not(annotatedParametersMatcher)); transformer.applyAdviceToMethod( - tracedMethodsWithoutParameters, + tracedMethodsWithoutParameters.and(isMethod()), WithSpanInstrumentation.class.getName() + "$WithSpanAdvice"); // Only apply advice for tracing parameters as attributes if any of the parameters are annotated // with @SpanAttribute to avoid unnecessarily copying the arguments into an array. transformer.applyAdviceToMethod( - tracedMethodsWithParameters, + tracedMethodsWithParameters.and(isMethod()), WithSpanInstrumentation.class.getName() + "$WithSpanAttributesAdvice"); } diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java index 29e58a4212cc..2e032cdbddf5 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java @@ -13,6 +13,12 @@ public class TracedWithSpan { + TracedWithSpan() {} + + // used to verify that constructor with @WithSpan annotation doesn't break instrumentation + @WithSpan + TracedWithSpan(String unused) {} + @WithSpan public String otel() { return "hello!";