From dc05142fb50d6871186c94b01d833f415027e8ff Mon Sep 17 00:00:00 2001 From: Hannah Arndt Date: Thu, 9 Jan 2025 18:01:18 +0100 Subject: [PATCH] suggestions from review I will squash with the previous commit once everything is approved --- extensions/opentelemetry/deployment/pom.xml | 5 + .../traces}/MutinyTracingHelperTest.java | 131 +++++++++--------- extensions/opentelemetry/runtime/pom.xml | 11 -- 3 files changed, 70 insertions(+), 77 deletions(-) rename extensions/opentelemetry/{runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/mutiny => deployment/src/test/java/io/quarkus/opentelemetry/deployment/traces}/MutinyTracingHelperTest.java (77%) diff --git a/extensions/opentelemetry/deployment/pom.xml b/extensions/opentelemetry/deployment/pom.xml index cac0090542ced..f0b9e0ef6d2a4 100644 --- a/extensions/opentelemetry/deployment/pom.xml +++ b/extensions/opentelemetry/deployment/pom.xml @@ -90,6 +90,11 @@ vertx-web-client test + + io.smallrye.reactive + smallrye-mutiny-vertx-junit5 + test + io.quarkus quarkus-reactive-routes-deployment diff --git a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/mutiny/MutinyTracingHelperTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/traces/MutinyTracingHelperTest.java similarity index 77% rename from extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/mutiny/MutinyTracingHelperTest.java rename to extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/traces/MutinyTracingHelperTest.java index 23ad478cee61d..6e10982a59b2e 100644 --- a/extensions/opentelemetry/runtime/src/test/java/io/quarkus/opentelemetry/runtime/tracing/mutiny/MutinyTracingHelperTest.java +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/traces/MutinyTracingHelperTest.java @@ -1,28 +1,26 @@ -package io.quarkus.opentelemetry.runtime.tracing.mutiny; +package io.quarkus.opentelemetry.deployment.traces; import static io.quarkus.opentelemetry.runtime.tracing.mutiny.MutinyTracingHelper.wrapWithSpan; import static org.assertj.core.api.Assertions.assertThat; import java.util.List; import java.util.Optional; -import java.util.stream.Stream; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; -import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.context.Scope; -import io.opentelemetry.sdk.OpenTelemetrySdk; -import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; -import io.opentelemetry.sdk.trace.SdkTracerProvider; import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; +import io.quarkus.opentelemetry.deployment.common.exporter.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.exporter.TestSpanExporterProvider; import io.quarkus.opentelemetry.runtime.QuarkusContextStorage; +import io.quarkus.test.QuarkusUnitTest; import io.smallrye.common.vertx.VertxContext; import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Uni; @@ -30,38 +28,54 @@ import io.smallrye.mutiny.helpers.test.UniAssertSubscriber; import io.vertx.core.Context; import io.vertx.core.Vertx; -import io.vertx.junit5.VertxExtension; +import jakarta.inject.Inject; -@ExtendWith(VertxExtension.class) class MutinyTracingHelperTest { - private InMemorySpanExporter spanExporter; + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest() + .setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClasses(TestSpanExporter.class, TestSpanExporterProvider.class) + .addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()), + "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")); + + @Inject + private TestSpanExporter spanExporter; + + @Inject private Tracer tracer; + @Inject private Vertx vertx; - @BeforeEach - void setVertx(final Vertx vertx) { - this.vertx = vertx; + @AfterEach + void tearDown() { + spanExporter.reset(); } - @BeforeEach - public void setup() { - GlobalOpenTelemetry.resetForTest(); + @Test + void testSimpleUniPipeline_noContext() { + final ContextProviderRunner contextRunner = new WithoutContextRunner(); + + simpleuniPipelineTest(contextRunner); + } - spanExporter = InMemorySpanExporter.create(); - final SdkTracerProvider tracerProvider = SdkTracerProvider.builder() - .addSpanProcessor(SimpleSpanProcessor.create(spanExporter)) - .build(); - OpenTelemetrySdk.builder().setTracerProvider(tracerProvider).buildAndRegisterGlobal(); + @Test + void testSimpleUniPipeline_onRootContext() { + final ContextProviderRunner contextRunner = new RootContextRunner(); - tracer = GlobalOpenTelemetry.getTracer(MutinyTracingHelperTest.class.getName()); + simpleuniPipelineTest(contextRunner); } - @ParameterizedTest(name = "{index}: Simple uni pipeline {1}") - @MethodSource("generateContextRunners") - void testSimpleUniPipeline(final ContextProviderRunner contextRunner, final String contextName) { + @Test + void testSimpleUniPipeline_onDuplicatedContext() { + final ContextProviderRunner contextRunner = new DuplicatedContextRunner(); + simpleuniPipelineTest(contextRunner); + } + + private void simpleuniPipelineTest(final ContextProviderRunner contextRunner) { final UniAssertSubscriber subscriber = Uni.createFrom() .item("Hello") .emitOn(r -> contextRunner.runOnContext(r, vertx)) @@ -81,15 +95,14 @@ void testSimpleUniPipeline(final ContextProviderRunner contextRunner, final Stri subscriber.awaitItem().assertItem("Hello world"); //ensure there are two spans with subspan as child of testSpan - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(2); + final List spans = spanExporter.getFinishedSpanItems(2); assertThat(spans.stream().map(SpanData::getName)).containsExactlyInAnyOrder("testSpan", "subspan"); assertChildSpan(spans, "testSpan", "subspan"); } - @ParameterizedTest(name = "{index}: Explicit parent {1}") - @MethodSource("generateContextRunners") - void testSpanWithExplicitParent(final ContextProviderRunner contextRunner, final String contextName) { + @Test + void testSpanWithExplicitParent() { + final ContextProviderRunner contextRunner = new RootContextRunner(); final String parentSpanName = "parentSpan"; final String pipelineSpanName = "pipelineSpan"; @@ -119,18 +132,16 @@ void testSpanWithExplicitParent(final ContextProviderRunner contextRunner, final parentSpan.end(); //ensure there are 3 spans with proper parent-child relationships - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(3); + final List spans = spanExporter.getFinishedSpanItems(3); assertThat(spans.stream().map(SpanData::getName)).containsExactlyInAnyOrder(parentSpanName, pipelineSpanName, subspanName); assertChildSpan(spans, parentSpanName, pipelineSpanName); assertChildSpan(spans, pipelineSpanName, subspanName); } - @ParameterizedTest(name = "{index}: Nested uni pipeline with implicit parent {1}") - @MethodSource("generateContextRunners") - void testNestedPipeline_implicitParent(final ContextProviderRunner contextRunner, - final String contextName) { + @Test + void testNestedPipeline_implicitParent() { + final ContextProviderRunner contextRunner = new RootContextRunner(); final String parentSpanName = "parentSpan"; final String childSpanName = "childSpan"; @@ -153,16 +164,14 @@ void testNestedPipeline_implicitParent(final ContextProviderRunner contextRunner subscriber.awaitItem(); //ensure there are 2 spans with doSomething and doSomethingAsync as children of testSpan - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(2); + final List spans = spanExporter.getFinishedSpanItems(2); assertThat(spans.stream().map(SpanData::getName)).containsExactlyInAnyOrder(parentSpanName, childSpanName); assertChildSpan(spans, parentSpanName, childSpanName); } - @ParameterizedTest(name = "{index}: Nested uni pipeline with explicit no parent {1}") - @MethodSource("generateContextRunners") - void testNestedPipeline_explicitNoParent(final ContextProviderRunner contextRunner, - final String contextName) { + @Test + void testNestedPipeline_explicitNoParent() { + final ContextProviderRunner contextRunner = new RootContextRunner(); final String parentSpanName = "parentSpan"; final String childSpanName = "childSpan"; @@ -185,8 +194,7 @@ void testNestedPipeline_explicitNoParent(final ContextProviderRunner contextRunn subscriber.awaitItem(); //ensure there are 2 spans but without parent-child relationship - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(2); + final List spans = spanExporter.getFinishedSpanItems(2); assertThat(spans.stream().map(SpanData::getName)).containsExactlyInAnyOrder(parentSpanName, childSpanName); assertThat(spans.stream() .filter(span -> span.getName().equals(childSpanName)) @@ -195,9 +203,9 @@ void testNestedPipeline_explicitNoParent(final ContextProviderRunner contextRunn .getParentSpanId()).isEqualTo("0000000000000000");//signifies no parent } - @ParameterizedTest(name = "{index}: Concatenating multi pipeline {1}") - @MethodSource("generateContextRunners") - void testSimpleMultiPipeline_Concatenate(final ContextProviderRunner contextRunner, final String contextName) { + @Test + void testSimpleMultiPipeline_Concatenate() { + final ContextProviderRunner contextRunner = new RootContextRunner(); final AssertSubscriber subscriber = Multi.createFrom() .items("test1", "test2", "test3") @@ -219,8 +227,7 @@ void testSimpleMultiPipeline_Concatenate(final ContextProviderRunner contextRunn subscriber.awaitCompletion().assertItems("test1 transformed", "test2 transformed", "test3 transformed"); //ensure there are six spans with three pairs of subspan as child of testSpan - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(6); + final List spans = spanExporter.getFinishedSpanItems(6); for (int i = 1; i <= 3; i++) { final int currentI = i; assertThat(spans.stream().anyMatch(span -> span.getName().equals("testSpan test" + currentI))).isTrue(); @@ -229,9 +236,9 @@ void testSimpleMultiPipeline_Concatenate(final ContextProviderRunner contextRunn } } - @ParameterizedTest(name = "{index}: Merging multi pipeline {1}") - @MethodSource("generateContextRunners") - void testSimpleMultiPipeline_Merge(final ContextProviderRunner contextRunner, final String contextName) { + @Test + void testSimpleMultiPipeline_Merge() { + final ContextProviderRunner contextRunner = new RootContextRunner(); final AssertSubscriber subscriber = Multi.createFrom() .items("test1", "test2", "test3") @@ -252,8 +259,7 @@ void testSimpleMultiPipeline_Merge(final ContextProviderRunner contextRunner, fi subscriber.awaitCompletion(); //ensure there are six spans with three pairs of subspan as child of testSpan - final List spans = spanExporter.getFinishedSpanItems(); - assertThat(spans).hasSize(6); + final List spans = spanExporter.getFinishedSpanItems(6); for (int i = 1; i <= 3; i++) { final int currentI = i; assertThat(spans.stream().anyMatch(span -> span.getName().equals("testSpan test" + currentI))).isTrue(); @@ -272,13 +278,6 @@ private static void assertChildSpan(final List spans, final String par spans.stream().filter(span -> span.getName().equals(parentSpanName)).findAny().get().getSpanId()); } - private static Stream generateContextRunners() { - return Stream.of( - Arguments.of(new WithoutContextRunner(), "Without Context"), - Arguments.of(new RootContextRunner(), "On Root Context"), - Arguments.of(new DuplicatedContextRunner(), "On Duplicated Context")); - } - private interface ContextProviderRunner { void runOnContext(Runnable runnable, Vertx vertx); } diff --git a/extensions/opentelemetry/runtime/pom.xml b/extensions/opentelemetry/runtime/pom.xml index bb5b46124de3b..e262526d857a3 100644 --- a/extensions/opentelemetry/runtime/pom.xml +++ b/extensions/opentelemetry/runtime/pom.xml @@ -201,11 +201,6 @@ quarkus-junit5-internal test - - io.smallrye.reactive - smallrye-mutiny-vertx-junit5 - test - org.assertj assertj-core @@ -216,12 +211,6 @@ mockito-core test - - io.opentelemetry - opentelemetry-sdk-testing - 1.42.1 - test -