diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java index 00a7aaae704c..1f6a55910ceb 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java @@ -39,7 +39,7 @@ */ @Aspect final class SpringSchedulingInstrumentationAspect { - public static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-scheduling-3.1"; + private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-boot-autoconfigure"; private final Instrumenter instrumenter; public SpringSchedulingInstrumentationAspect( diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java index 0b4294895d7c..fbacd93f47a4 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/InstrumentationWithSpanAspectTest.java @@ -81,19 +81,17 @@ void withSpanWithDefaults() { testing.runWithSpan("parent", withSpanTester::testWithSpan); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testWithSpan") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testWithSpan")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testWithSpan") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testWithSpan")))); } @Test @@ -126,18 +124,16 @@ void withSpanError() { assertThatThrownBy(() -> withSpanTester.testWithSpanWithException()) .isInstanceOf(Exception.class); - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testWithSpanWithException") - .hasKind(INTERNAL) - .hasStatus(StatusData.error()) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testWithSpanWithException")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testWithSpanWithException") + .hasKind(INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testWithSpanWithException")))); } @Test @@ -148,19 +144,17 @@ void withSpanKind() { testing.runWithSpan("parent", () -> withSpanTester.testWithClientSpan()); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testWithClientSpan") - .hasKind(CLIENT) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testWithClientSpan")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testWithClientSpan") + .hasKind(CLIENT) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testWithClientSpan")))); } @Test @@ -170,22 +164,20 @@ void withSpanAttributes() { "parent", () -> withSpanTester.withSpanAttributes("foo", "bar", "baz", null, "fizz")); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".withSpanAttributes") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "withSpanAttributes"), - equalTo(stringKey("discoveredName"), "foo"), - equalTo(stringKey("implicitName"), "bar"), - equalTo(stringKey("explicitName"), "baz")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".withSpanAttributes") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "withSpanAttributes"), + equalTo(stringKey("discoveredName"), "foo"), + equalTo(stringKey("implicitName"), "bar"), + equalTo(stringKey("explicitName"), "baz")))); } static class InstrumentationWithSpanTester { @@ -244,30 +236,27 @@ void onComplete() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletionStage(future)); // then - assertThat(testing.waitForTraces(1)) - .hasTracesSatisfyingExactly( - trace -> - trace - .hasSize(1) - .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); + testing.waitAndAssertTraces( + trace -> + trace + .hasSize(1) + .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); // when future.complete("DONE"); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); } @Test @@ -279,31 +268,28 @@ void onCompleteExceptionally() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletionStage(future)); // then - assertThat(testing.waitForTraces(1)) - .hasTracesSatisfyingExactly( - trace -> - trace - .hasSize(1) - .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); + testing.waitAndAssertTraces( + trace -> + trace + .hasSize(1) + .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); // when future.completeExceptionally(new Exception("Test @WithSpan With completeExceptionally")); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") - .hasKind(INTERNAL) - .hasStatus(StatusData.error()) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") + .hasKind(INTERNAL) + .hasStatus(StatusData.error()) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); } @Test @@ -313,19 +299,17 @@ void onIncompatibleReturnValue() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletionStage(null)); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletionStage") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletionStage")))); } } @@ -342,31 +326,27 @@ void onComplete() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletableFuture(future)); // then - assertThat(testing.waitForTraces(1)) - .hasTracesSatisfyingExactly( - trace -> - trace - .hasSize(1) - .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); + testing.waitAndAssertTraces( + trace -> + trace + .hasSize(1) + .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); // when future.complete("DONE"); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName( - unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); } @Test @@ -378,32 +358,28 @@ void onCompleteExceptionally() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletableFuture(future)); // then - assertThat(testing.waitForTraces(1)) - .hasTracesSatisfyingExactly( - trace -> - trace - .hasSize(1) - .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); + testing.waitAndAssertTraces( + trace -> + trace + .hasSize(1) + .hasSpansSatisfyingExactly(span -> span.hasName("parent").hasKind(INTERNAL))); // when future.completeExceptionally(new Exception("Test @WithSpan With completeExceptionally")); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName( - unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") - .hasKind(INTERNAL) - .hasStatus(StatusData.error()) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") + .hasKind(INTERNAL) + .hasStatus(StatusData.error()) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); } @Test @@ -415,20 +391,17 @@ void onCompletedFuture() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletableFuture(future)); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName( - unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); } @Test @@ -441,21 +414,18 @@ void onFailedFuture() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletableFuture(future)); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName( - unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") - .hasKind(INTERNAL) - .hasStatus(StatusData.error()) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") + .hasKind(INTERNAL) + .hasStatus(StatusData.error()) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); } @Test @@ -465,20 +435,17 @@ void onIncompatibleReturnValue() { testing.runWithSpan("parent", () -> withSpanTester.testAsyncCompletableFuture(null)); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), - span -> - span.hasName( - unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") - .hasKind(INTERNAL) - .hasParent(trace.getSpan(0)) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + parentSpan -> parentSpan.hasName("parent").hasKind(INTERNAL), + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testAsyncCompletableFuture") + .hasKind(INTERNAL) + .hasParent(trace.getSpan(0)) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testAsyncCompletableFuture")))); } } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SchedulingInstrumentationAspectTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SchedulingInstrumentationAspectTest.java index 79e15e180b9d..b6953c7060fc 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SchedulingInstrumentationAspectTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SchedulingInstrumentationAspectTest.java @@ -7,7 +7,6 @@ import static io.opentelemetry.api.trace.SpanKind.INTERNAL; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; -import static io.opentelemetry.sdk.testing.assertj.TracesAssert.assertThat; import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -20,10 +19,8 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; -import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.data.StatusData; import java.util.Collections; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -72,17 +69,15 @@ void scheduled() { schedulingTester.testScheduled(); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testScheduled") - .hasKind(INTERNAL) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testScheduled")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testScheduled") + .hasKind(INTERNAL) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testScheduled")))); } @Test @@ -92,17 +87,15 @@ void multiScheduled() { schedulingTester.testMultiScheduled(); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testMultiScheduled") - .hasKind(INTERNAL) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testMultiScheduled")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testMultiScheduled") + .hasKind(INTERNAL) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testMultiScheduled")))); } @Test @@ -112,17 +105,15 @@ void schedules() { schedulingTester.testSchedules(); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testSchedules") - .hasKind(INTERNAL) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testSchedules")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testSchedules") + .hasKind(INTERNAL) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testSchedules")))); } @Test @@ -133,19 +124,17 @@ void nestedSpanInScheduled() { schedulingTester.testNestedSpan(); // then - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testNestedSpan") - .hasKind(INTERNAL) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testNestedSpan")), - nestedSpan -> - nestedSpan.hasParent(trace.getSpan(0)).hasKind(INTERNAL).hasName("test"))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testNestedSpan") + .hasKind(INTERNAL) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testNestedSpan")), + nestedSpan -> + nestedSpan.hasParent(trace.getSpan(0)).hasKind(INTERNAL).hasName("test"))); } @Test @@ -155,18 +144,16 @@ void scheduledError() { assertThatThrownBy(() -> schedulingTester.testScheduledWithException()) .isInstanceOf(Exception.class); - List> traces = testing.waitForTraces(1); - assertThat(traces) - .hasTracesSatisfyingExactly( - trace -> - trace.hasSpansSatisfyingExactly( - span -> - span.hasName(unproxiedTesterSimpleClassName + ".testScheduledWithException") - .hasKind(INTERNAL) - .hasStatus(StatusData.error()) - .hasAttributesSatisfyingExactly( - equalTo(CODE_NAMESPACE, unproxiedTesterClassName), - equalTo(CODE_FUNCTION, "testScheduledWithException")))); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName(unproxiedTesterSimpleClassName + ".testScheduledWithException") + .hasKind(INTERNAL) + .hasStatus(StatusData.error()) + .hasAttributesSatisfyingExactly( + equalTo(CODE_NAMESPACE, unproxiedTesterClassName), + equalTo(CODE_FUNCTION, "testScheduledWithException")))); } static class InstrumentationSchedulingTester { diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderTest.java index 86a366b14fce..ad677c0520c2 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderTest.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/testLogbackAppender/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/logging/LogbackAppenderTest.java @@ -97,7 +97,7 @@ void shouldInitializeAppender() { void shouldNotInitializeAppenderWhenDisabled() { Map properties = new HashMap<>(); properties.put("logging.config", "classpath:logback-test.xml"); - properties.put("otel.springboot.logback-appender.enabled", "false"); + properties.put("otel.instrumentation.logback-appender.enabled", "false"); SpringApplication app = new SpringApplication( diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java index c70a9d73b4ab..2437e2537d40 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/LibraryTestRunner.java @@ -7,6 +7,10 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.TracerBuilder; +import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.ContextPropagators; @@ -40,7 +44,8 @@ */ public final class LibraryTestRunner extends InstrumentationTestRunner { - private static final OpenTelemetrySdk openTelemetry; + private static final OpenTelemetrySdk openTelemetrySdk; + private static final OpenTelemetry openTelemetry; private static final InMemorySpanExporter testSpanExporter; private static final InMemoryMetricExporter testMetricExporter; private static final InMemoryLogRecordExporter testLogRecordExporter; @@ -61,7 +66,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner { .setInterval(Duration.ofNanos(Long.MAX_VALUE)) .build(); - openTelemetry = + openTelemetrySdk = OpenTelemetrySdk.builder() .setTracerProvider( SdkTracerProvider.builder() @@ -76,6 +81,7 @@ public final class LibraryTestRunner extends InstrumentationTestRunner { .build()) .setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance())) .buildAndRegisterGlobal(); + openTelemetry = wrap(openTelemetrySdk); } private static final LibraryTestRunner INSTANCE = new LibraryTestRunner(); @@ -91,10 +97,8 @@ private LibraryTestRunner() { @Override public void beforeTestClass() { // just in case: if there was any test that modified the global instance, reset it - if (GlobalOpenTelemetry.get() != openTelemetry) { - GlobalOpenTelemetry.resetForTest(); - GlobalOpenTelemetry.set(openTelemetry); - } + GlobalOpenTelemetry.resetForTest(); + GlobalOpenTelemetry.set(openTelemetrySdk); } @Override @@ -103,7 +107,7 @@ public void afterTestClass() {} @Override public void clearAllExportedData() { // Flush meter provider to remove any lingering measurements - openTelemetry.getSdkMeterProvider().forceFlush().join(10, TimeUnit.SECONDS); + openTelemetrySdk.getSdkMeterProvider().forceFlush().join(10, TimeUnit.SECONDS); testSpanExporter.reset(); testMetricExporter.reset(); testLogRecordExporter.reset(); @@ -116,7 +120,7 @@ public OpenTelemetry getOpenTelemetry() { } public OpenTelemetrySdk getOpenTelemetrySdk() { - return openTelemetry; + return openTelemetrySdk; } @Override @@ -140,6 +144,39 @@ public boolean forceFlushCalled() { return forceFlushCalled; } + // Wrap the OpenTelemetry instance similarly to what GlobalOpenTelemetry does. We do this to + // ensure that the OpenTelemetry instance can not be accidentally closed. This could happen for + // example when it is registered as spring bean and spring calls close on it, because + // OpenTelemetrySdk implements Closeable, when the application context is closed. + private static OpenTelemetry wrap(OpenTelemetry delegate) { + return new OpenTelemetry() { + @Override + public TracerProvider getTracerProvider() { + return delegate.getTracerProvider(); + } + + @Override + public MeterProvider getMeterProvider() { + return delegate.getMeterProvider(); + } + + @Override + public LoggerProvider getLogsBridge() { + return delegate.getLogsBridge(); + } + + @Override + public ContextPropagators getPropagators() { + return delegate.getPropagators(); + } + + @Override + public TracerBuilder tracerBuilder(String instrumentationScopeName) { + return delegate.tracerBuilder(instrumentationScopeName); + } + }; + } + private static class FlushTrackingSpanProcessor implements SpanProcessor { @Override public void onStart(Context parentContext, ReadWriteSpan span) {}