diff --git a/CHANGELOG.md b/CHANGELOG.md index 65f62ec2ba66..3e067ebf13e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### ⚠️ Breaking changes +- `@WithSpan` and `@AddingSpanAttributes` no longer allow targeting constructors + (which didn't work properly anyways). - Kotlin suspend function `@WithSpan` instrumentation now uses only the `kotlinx-coroutines-annotations` selector and is disabled by default. Use `otel.instrumentation.kotlinx-coroutines-annotations.enabled=true` instead of the diff --git a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt index 394c0034b027..87b2c1b286af 100644 --- a/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt +++ b/docs/apidiffs/current_vs_latest/opentelemetry-instrumentation-annotations.txt @@ -1,2 +1,9 @@ Comparing source compatibility of opentelemetry-instrumentation-annotations-3.0.0-SNAPSHOT.jar against opentelemetry-instrumentation-annotations-2.32.0.jar -No changes. \ No newline at end of file +=== UNCHANGED ANNOTATION: PUBLIC ABSTRACT io.opentelemetry.instrumentation.annotations.AddingSpanAttributes (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + *** MODIFIED ANNOTATION: java.lang.annotation.Target + *** MODIFIED ELEMENT: value=java.lang.annotation.ElementType.METHOD (<- java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.CONSTRUCTOR) +=== UNCHANGED ANNOTATION: PUBLIC ABSTRACT io.opentelemetry.instrumentation.annotations.WithSpan (not serializable) + === CLASS FILE FORMAT VERSION: 52.0 <- 52.0 + *** MODIFIED ANNOTATION: java.lang.annotation.Target + *** MODIFIED ELEMENT: value=java.lang.annotation.ElementType.METHOD (<- java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.CONSTRUCTOR) diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributes.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributes.java index 3914aa7b1b6c..372d451248cc 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributes.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/AddingSpanAttributes.java @@ -14,9 +14,7 @@ * This annotation marks that an execution of this method is able to add attributes to the current * span {@link io.opentelemetry.api.trace.Span}. * - *

Using this annotation on constructors is not supported by OpenTelemetry instrumentation. The - * {@link ElementType#CONSTRUCTOR} target exists only for backward compatibility and is planned to - * be removed in the 3.0 release. + *

This annotation can only be applied to methods. Constructor instrumentation is not supported. * *

Application developers can use this annotation to signal OpenTelemetry auto-instrumentation * that attributes annotated with the {@link @@ -34,6 +32,6 @@ *

If you are a library developer, then probably you should NOT use this annotation, because it * is non-functional without some form of auto-instrumentation. */ -@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) // CONSTRUCTOR target to be removed in 3.0 +@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AddingSpanAttributes {} diff --git a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java index ef6785f6c4cc..15f4e38964ac 100644 --- a/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java +++ b/instrumentation-annotations/src/main/java/io/opentelemetry/instrumentation/annotations/WithSpan.java @@ -16,9 +16,7 @@ * This annotation marks that an execution of this method should result in a new {@link * io.opentelemetry.api.trace.Span}. * - *

Using this annotation on constructors is not supported by OpenTelemetry instrumentation. The - * {@link ElementType#CONSTRUCTOR} target exists only for backward compatibility and is planned to - * be removed in the 3.0 release. + *

This annotation can only be applied to methods. Constructor instrumentation is not supported. * *

Application developers can use this annotation to signal OpenTelemetry auto-instrumentation * that a new span should be created whenever the marked method is executed. @@ -26,7 +24,7 @@ *

If you are a library developer, then probably you should NOT use this annotation, because it * is non-functional without some form of auto-instrumentation. */ -@Target({ElementType.METHOD, ElementType.CONSTRUCTOR}) // CONSTRUCTOR target to be removed in 3.0 +@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface WithSpan { /** diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java index 5b6dad3fc7dd..f6dfad84f15c 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/AddingSpanAttributesInstrumentationTest.java @@ -12,7 +12,10 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.annotations.AddingSpanAttributes; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -77,37 +80,9 @@ void noExistingSpan() { } @Test - void constructorOnlyAddingSpanAttributesDoesNotTransformType() { - testing.runWithSpan( - "root", () -> new ConstructorOnlyAddingSpanAttributes("foo", "bar", null, "baz")); - - testing.waitAndAssertTraces( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasNoParent() - .hasTotalAttributeCount(0))); - } - - @Test - void constructorAndMethodAddingSpanAttributesIgnoresConstructor() { - testing.runWithSpan( - "root", - () -> - new ConstructedWithAddingSpanAttributes("foo", "bar", null, "baz") - .addAttributes("method")); - - testing.waitAndAssertTraces( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName("root") - .hasKind(SpanKind.INTERNAL) - .hasNoParent() - .hasAttributesSatisfyingExactly( - equalTo(stringKey("methodAttribute"), "method")))); + void annotationTargetsMethodsOnly() { + assertThat(AddingSpanAttributes.class.getAnnotation(Target.class).value()) + .containsExactly(ElementType.METHOD); } @Test diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructedWithAddingSpanAttributes.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructedWithAddingSpanAttributes.java deleted file mode 100644 index 55e08433d566..000000000000 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructedWithAddingSpanAttributes.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.test.annotation; - -import io.opentelemetry.instrumentation.annotations.AddingSpanAttributes; -import io.opentelemetry.instrumentation.annotations.SpanAttribute; - -class ConstructedWithAddingSpanAttributes { - - @AddingSpanAttributes - ConstructedWithAddingSpanAttributes( - @SpanAttribute String implicitName, - @SpanAttribute("explicitName") String parameter, - @SpanAttribute("nullAttribute") String nullAttribute, - String notTraced) {} - - @AddingSpanAttributes - void addAttributes(@SpanAttribute("methodAttribute") String methodAttribute) {} -} diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructorOnlyAddingSpanAttributes.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructorOnlyAddingSpanAttributes.java deleted file mode 100644 index 45eda9e12887..000000000000 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/ConstructorOnlyAddingSpanAttributes.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.test.annotation; - -import io.opentelemetry.instrumentation.annotations.AddingSpanAttributes; -import io.opentelemetry.instrumentation.annotations.SpanAttribute; - -class ConstructorOnlyAddingSpanAttributes { - - @AddingSpanAttributes - ConstructorOnlyAddingSpanAttributes( - @SpanAttribute String implicitName, - @SpanAttribute("explicitName") String parameter, - @SpanAttribute("nullAttribute") String nullAttribute, - String notTraced) {} -} 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 cb17cf9376b3..9d3f9ee343b1 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 @@ -15,10 +15,6 @@ class TracedWithSpan { TracedWithSpan() {} - // used to verify that constructor with @WithSpan annotation doesn't break instrumentation - @WithSpan - TracedWithSpan(String unused) {} - @WithSpan String otel() { return "hello!"; diff --git a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java index da5890d8876e..b5f033b0dd15 100644 --- a/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java +++ b/instrumentation/opentelemetry-instrumentation-annotations-1.16/javaagent/src/test/java/io/opentelemetry/test/annotation/WithSpanInstrumentationTest.java @@ -19,6 +19,8 @@ import io.opentelemetry.instrumentation.annotations.WithSpan; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.sdk.trace.data.StatusData; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; import java.lang.reflect.Modifier; import java.util.concurrent.CompletableFuture; import net.bytebuddy.ByteBuddy; @@ -146,11 +148,9 @@ void excludedMethod() throws InterruptedException { } @Test - void annotatedConstructorDoesNotCreateSpan() throws InterruptedException { - new TracedWithSpan("unused"); - - Thread.sleep(500); // sleep a bit just to make sure no span is captured - assertThat(testing.waitForTraces(0)).isEmpty(); + void annotationTargetsMethodsOnly() { + assertThat(WithSpan.class.getAnnotation(Target.class).value()) + .containsExactly(ElementType.METHOD); } @Test