Skip to content

Commit

Permalink
suggestions from review
Browse files Browse the repository at this point in the history
I will squash with the previous commit once everything is approved
  • Loading branch information
arn-ivu committed Jan 9, 2025
1 parent b0445dd commit dc05142
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 77 deletions.
5 changes: 5 additions & 0 deletions extensions/opentelemetry/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@
<artifactId>vertx-web-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes-deployment</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,67 +1,81 @@
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;
import io.smallrye.mutiny.helpers.test.AssertSubscriber;
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<String> subscriber = Uni.createFrom()
.item("Hello")
.emitOn(r -> contextRunner.runOnContext(r, vertx))
Expand All @@ -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<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(2);
final List<SpanData> 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";
Expand Down Expand Up @@ -119,18 +132,16 @@ void testSpanWithExplicitParent(final ContextProviderRunner contextRunner, final
parentSpan.end();

//ensure there are 3 spans with proper parent-child relationships
final List<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(3);
final List<SpanData> 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";
Expand All @@ -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<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(2);
final List<SpanData> 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";
Expand All @@ -185,8 +194,7 @@ void testNestedPipeline_explicitNoParent(final ContextProviderRunner contextRunn
subscriber.awaitItem();

//ensure there are 2 spans but without parent-child relationship
final List<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(2);
final List<SpanData> spans = spanExporter.getFinishedSpanItems(2);
assertThat(spans.stream().map(SpanData::getName)).containsExactlyInAnyOrder(parentSpanName, childSpanName);
assertThat(spans.stream()
.filter(span -> span.getName().equals(childSpanName))
Expand All @@ -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<String> subscriber = Multi.createFrom()
.items("test1", "test2", "test3")
Expand All @@ -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<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(6);
final List<SpanData> 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();
Expand All @@ -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<String> subscriber = Multi.createFrom()
.items("test1", "test2", "test3")
Expand All @@ -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<SpanData> spans = spanExporter.getFinishedSpanItems();
assertThat(spans).hasSize(6);
final List<SpanData> 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();
Expand All @@ -272,13 +278,6 @@ private static void assertChildSpan(final List<SpanData> spans, final String par
spans.stream().filter(span -> span.getName().equals(parentSpanName)).findAny().get().getSpanId());
}

private static Stream<Arguments> 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);
}
Expand Down
11 changes: 0 additions & 11 deletions extensions/opentelemetry/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,6 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand All @@ -216,12 +211,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<version>1.42.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit dc05142

Please sign in to comment.