From 975ad64f4c9508d81b9bec35f1c8e17936ba57eb Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 13 Aug 2024 13:36:01 +0200 Subject: [PATCH] convert spring tests to java --- .../netty/v4_0/client/Netty40ClientTest.java | 4 +- .../client/Netty40ConnectionSpanTest.java | 4 +- .../ApplicationContextInstrumentation.java | 2 +- .../java/SpringIntegrationAndRabbitTest.java | 142 ++++++++++-------- .../src/test/java/ComplexPropagationTest.java | 2 +- .../java/SpringCloudStreamProducerTest.java | 2 +- .../java/SpringCloudStreamRabbitTest.java | 2 +- .../java/SpringIntegrationTelemetryTest.java | 2 +- .../java/AbstractComplexPropagationTest.java | 20 +-- .../AbstractRabbitProducerConsumerTest.java | 45 ++++-- ...AbstractSpringCloudStreamProducerTest.java | 38 +---- .../AbstractSpringCloudStreamRabbitTest.java | 33 +--- .../AbstractSpringIntegrationTracingTest.java | 57 +++---- .../main/java/CapturingMessageHandler.java | 3 +- 14 files changed, 157 insertions(+), 199 deletions(-) diff --git a/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ClientTest.java b/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ClientTest.java index 2953186c8580..aea202663f0e 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ClientTest.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ClientTest.java @@ -36,6 +36,7 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.extension.RegisterExtension; class Netty40ClientTest extends AbstractHttpClientTest { @@ -49,7 +50,8 @@ class Netty40ClientTest extends AbstractHttpClientTest { private final Bootstrap readTimeoutBootstrap = buildBootstrap(true); - void cleanupSpec() { + @AfterEach + void tearDown() { if (eventLoopGroup != null) { eventLoopGroup.shutdownGracefully(); } diff --git a/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ConnectionSpanTest.java b/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ConnectionSpanTest.java index 45c36c980f2f..c3c4cdbc3049 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ConnectionSpanTest.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/Netty40ConnectionSpanTest.java @@ -56,13 +56,13 @@ class Netty40ConnectionSpanTest { private static final Bootstrap bootstrap = buildBootstrap(); @BeforeAll - static void setupSpec() { + static void setUp() { server = new HttpClientTestServer(testing.getOpenTelemetry()); server.start(); } @AfterAll - static void cleanupSpec() throws InterruptedException, ExecutionException, TimeoutException { + static void tearDown() throws InterruptedException, ExecutionException, TimeoutException { eventLoopGroup.shutdownGracefully(); server.stop().get(10, TimeUnit.SECONDS); } diff --git a/instrumentation/spring/spring-integration-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/integration/v4_1/ApplicationContextInstrumentation.java b/instrumentation/spring/spring-integration-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/integration/v4_1/ApplicationContextInstrumentation.java index 877b382bca55..341be5b2b207 100644 --- a/instrumentation/spring/spring-integration-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/integration/v4_1/ApplicationContextInstrumentation.java +++ b/instrumentation/spring/spring-integration-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/integration/v4_1/ApplicationContextInstrumentation.java @@ -22,7 +22,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.integration.channel.interceptor.GlobalChannelInterceptorWrapper; -public class ApplicationContextInstrumentation implements TypeInstrumentation { +class ApplicationContextInstrumentation implements TypeInstrumentation { @Override public ElementMatcher classLoaderOptimization() { return hasClassesNamed("org.springframework.context.support.AbstractApplicationContext"); diff --git a/instrumentation/spring/spring-integration-4.1/javaagent/src/test/java/SpringIntegrationAndRabbitTest.java b/instrumentation/spring/spring-integration-4.1/javaagent/src/test/java/SpringIntegrationAndRabbitTest.java index 296c9f55e751..0fac63a3f1a4 100644 --- a/instrumentation/spring/spring-integration-4.1/javaagent/src/test/java/SpringIntegrationAndRabbitTest.java +++ b/instrumentation/spring/spring-integration-4.1/javaagent/src/test/java/SpringIntegrationAndRabbitTest.java @@ -4,156 +4,170 @@ */ import static io.opentelemetry.instrumentation.testing.GlobalTraceUtil.runWithSpan; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions; import io.opentelemetry.semconv.NetworkAttributes; import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; -public class SpringIntegrationAndRabbitTest extends AbstractRabbitProducerConsumerTest { +class SpringIntegrationAndRabbitTest extends AbstractRabbitProducerConsumerTest { @RegisterExtension static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); - @BeforeEach - public void setupSpec() { - startRabbit(null); - } - - @AfterEach - public void cleanupSpec() { - stopRabbit(); + SpringIntegrationAndRabbitTest() { + super(null); } @Test - public void shouldCooperateWithExistingRabbitMQInstrumentation() { + void should_cooperate_with_existing_RabbitMq_instrumentation() { runWithSpan("parent", () -> producerContext.getBean("producer", Runnable.class).run()); testing.waitAndAssertTraces( + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore + trace -> {}, // ignore trace -> trace.hasSpansSatisfyingExactly( span -> span.hasName("parent"), span -> span.hasName("producer").hasParent(trace.getSpan(0)), + span -> span.hasName("exchange.declare"), span -> span.hasName("exchange.declare") .hasParent(trace.getSpan(1)) .hasKind(SpanKind.CLIENT) - .hasAttributesSatisfying( - OpenTelemetryAssertions.satisfies( + .hasAttributesSatisfyingExactly( + satisfies( NetworkAttributes.NETWORK_PEER_ADDRESS, s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), - OpenTelemetryAssertions.satisfies( - NetworkAttributes.NETWORK_PEER_PORT, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + l -> l.isInstanceOf(Long.class)), + satisfies( NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_SYSTEM, s -> s.isEqualTo("rabbitmq"))), + span -> span.hasName("queue.declare"), + span -> span.hasName("queue.bind"), span -> span.hasName("testTopic publish") .hasParent(trace.getSpan(1)) .hasKind(SpanKind.PRODUCER) - .hasAttributesSatisfying( - OpenTelemetryAssertions.satisfies( + .hasAttributesSatisfyingExactly( + satisfies( NetworkAttributes.NETWORK_PEER_ADDRESS, s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), - OpenTelemetryAssertions.satisfies( - NetworkAttributes.NETWORK_PEER_PORT, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + l -> l.isInstanceOf(Long.class)), + satisfies( NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_SYSTEM, s -> s.isEqualTo("rabbitmq")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, s -> s.isEqualTo("testTopic")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_OPERATION, s -> s.isEqualTo("publish")), - OpenTelemetryAssertions.satisfies( - MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, + l -> l.isInstanceOf(Long.class)), + satisfies( MessagingIncubatingAttributes .MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, - s -> {})), + s -> s.isInstanceOf(String.class))), // spring-cloud-stream-binder-rabbit listener puts all messages into a BlockingQueue // immediately after receiving // that's why the rabbitmq CONSUMER span will never have any child span (and // propagate context, actually) span -> span.satisfies( - spanData -> { - assertThat(spanData.getName()) - .matches("testTopic.anonymous.[-\\w]+ process/"); - }) - .hasParent(trace.getSpan(3)) + spanData -> + assertThat(spanData.getName()) + .matches("testTopic.anonymous.[-\\w]+ process")) + .hasParent(trace.getSpan(6)) .hasKind(SpanKind.CONSUMER) - .hasAttributesSatisfying( - OpenTelemetryAssertions.satisfies( + .hasAttributesSatisfyingExactly( + satisfies( NetworkAttributes.NETWORK_PEER_ADDRESS, s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), - OpenTelemetryAssertions.satisfies( - NetworkAttributes.NETWORK_PEER_PORT, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + l -> l.isInstanceOf(Long.class)), + satisfies( NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_SYSTEM, s -> s.isEqualTo("rabbitmq")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, s -> s.isEqualTo("testTopic")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_OPERATION, s -> s.isEqualTo("process")), - OpenTelemetryAssertions.satisfies( - MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, + l -> l.isInstanceOf(Long.class)), + satisfies( MessagingIncubatingAttributes .MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY, - s -> {})), + s -> s.isInstanceOf(String.class))), // spring-integration will detect that spring-rabbit has already created a consumer // span and back off span -> span.hasName("testTopic process") - .hasParent(trace.getSpan(3)) + .hasParent(trace.getSpan(6)) .hasKind(SpanKind.CONSUMER) - .hasAttributesSatisfying( - OpenTelemetryAssertions.satisfies( + .hasAttributesSatisfyingExactly( + satisfies( MessagingIncubatingAttributes.MESSAGING_SYSTEM, s -> s.isEqualTo("rabbitmq")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME, s -> s.isEqualTo("testTopic")), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_OPERATION, s -> s.isEqualTo("process")), - OpenTelemetryAssertions.satisfies( - MessagingIncubatingAttributes.MESSAGING_MESSAGE_ID, s -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + MessagingIncubatingAttributes.MESSAGING_MESSAGE_ID, + s -> s.isInstanceOf(String.class)), + satisfies( MessagingIncubatingAttributes.MESSAGING_MESSAGE_BODY_SIZE, - l -> {})), - span -> span.hasName("consumer").hasParent(trace.getSpan(5))), + l -> l.isInstanceOf(Long.class))), + span -> span.hasName("consumer").hasParent(trace.getSpan(8))), trace -> trace.hasSpansSatisfyingExactly( span -> span.hasName("basic.ack") .hasKind(SpanKind.CLIENT) - .hasAttributesSatisfying( - OpenTelemetryAssertions.satisfies( + .hasAttributesSatisfyingExactly( + satisfies( NetworkAttributes.NETWORK_PEER_ADDRESS, s -> s.isIn("127.0.0.1", "0:0:0:0:0:0:0:1", null)), - OpenTelemetryAssertions.satisfies( - NetworkAttributes.NETWORK_PEER_PORT, l -> {}), - OpenTelemetryAssertions.satisfies( + satisfies( + NetworkAttributes.NETWORK_PEER_PORT, + l -> l.isInstanceOf(Long.class)), + satisfies( NetworkAttributes.NETWORK_TYPE, s -> s.isIn("ipv4", "ipv6", null)), - OpenTelemetryAssertions.satisfies( + satisfies( MessagingIncubatingAttributes.MESSAGING_SYSTEM, s -> s.isEqualTo("rabbitmq"))))); } diff --git a/instrumentation/spring/spring-integration-4.1/library/src/test/java/ComplexPropagationTest.java b/instrumentation/spring/spring-integration-4.1/library/src/test/java/ComplexPropagationTest.java index f4c5e7cfe34b..74314e264e3d 100644 --- a/instrumentation/spring/spring-integration-4.1/library/src/test/java/ComplexPropagationTest.java +++ b/instrumentation/spring/spring-integration-4.1/library/src/test/java/ComplexPropagationTest.java @@ -7,7 +7,7 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import org.junit.jupiter.api.extension.RegisterExtension; -public class ComplexPropagationTest extends AbstractComplexPropagationTest { +class ComplexPropagationTest extends AbstractComplexPropagationTest { @RegisterExtension static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); diff --git a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamProducerTest.java b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamProducerTest.java index 9ab6bf4e4618..186cf5542bdc 100644 --- a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamProducerTest.java +++ b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamProducerTest.java @@ -7,7 +7,7 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import org.junit.jupiter.api.extension.RegisterExtension; -public class SpringCloudStreamProducerTest extends AbstractSpringCloudStreamProducerTest { +class SpringCloudStreamProducerTest extends AbstractSpringCloudStreamProducerTest { @RegisterExtension static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); diff --git a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamRabbitTest.java b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamRabbitTest.java index d5632848f6e4..ec49f62bad00 100644 --- a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamRabbitTest.java +++ b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringCloudStreamRabbitTest.java @@ -7,7 +7,7 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import org.junit.jupiter.api.extension.RegisterExtension; -public class SpringCloudStreamRabbitTest extends AbstractSpringCloudStreamRabbitTest { +class SpringCloudStreamRabbitTest extends AbstractSpringCloudStreamRabbitTest { @RegisterExtension static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); diff --git a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringIntegrationTelemetryTest.java b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringIntegrationTelemetryTest.java index 6fe1f9187725..cb38bd030297 100644 --- a/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringIntegrationTelemetryTest.java +++ b/instrumentation/spring/spring-integration-4.1/library/src/test/java/SpringIntegrationTelemetryTest.java @@ -7,7 +7,7 @@ import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; import org.junit.jupiter.api.extension.RegisterExtension; -public class SpringIntegrationTelemetryTest extends AbstractSpringIntegrationTracingTest { +class SpringIntegrationTelemetryTest extends AbstractSpringIntegrationTracingTest { @RegisterExtension static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractComplexPropagationTest.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractComplexPropagationTest.java index f912a73712f0..571708d0111d 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractComplexPropagationTest.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractComplexPropagationTest.java @@ -45,7 +45,7 @@ public AbstractComplexPropagationTest( } @BeforeEach - void setupSpec() { + void setUp() { List> contextClasses = new ArrayList<>(); contextClasses.add(ExternalQueueConfig.class); if (additionalContextClass != null) { @@ -59,12 +59,12 @@ void setupSpec() { } @AfterEach - void cleanupSpec() { + void tearDown() { applicationContext.close(); } @Test - void shouldPropagateContextThroughAComplexFlow() { + void should_propagate_context_through_acomplex_flow() { SubscribableChannel sendChannel = applicationContext.getBean("sendChannel", SubscribableChannel.class); SubscribableChannel receiveChannel = @@ -80,15 +80,11 @@ void shouldPropagateContextThroughAComplexFlow() { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> { - span.hasName("application.sendChannel process"); - span.hasKind(SpanKind.CONSUMER); - }, - span -> { - span.hasName("application.receiveChannel process"); - span.hasParent(trace.getSpan(0)); - span.hasKind(SpanKind.CONSUMER); - }, + span -> span.hasName("application.sendChannel process").hasKind(SpanKind.CONSUMER), + span -> + span.hasName("application.receiveChannel process") + .hasParent(trace.getSpan(0)) + .hasKind(SpanKind.CONSUMER), span -> span.hasName("handler").hasParent(trace.getSpan(1)))); receiveChannel.unsubscribe(messageHandler); diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractRabbitProducerConsumerTest.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractRabbitProducerConsumerTest.java index 53a6b043685f..a849e986e397 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractRabbitProducerConsumerTest.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractRabbitProducerConsumerTest.java @@ -10,6 +10,8 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; @@ -26,13 +28,29 @@ public abstract class AbstractRabbitProducerConsumerTest { - static GenericContainer rabbitMqContainer; - static ConfigurableApplicationContext producerContext; - static ConfigurableApplicationContext consumerContext; + private GenericContainer rabbitMqContainer; + protected ConfigurableApplicationContext producerContext; + private ConfigurableApplicationContext consumerContext; - public void startRabbit(Class additionalContext) { + private final Class additionalContextClass; + + public AbstractRabbitProducerConsumerTest(Class additionalContextClass) { + this.additionalContextClass = additionalContextClass; + } + + @BeforeEach + public void setUp() { + startRabbit(additionalContextClass); + } + + @AfterEach + public void tearDown() { + stopRabbit(); + } + + private void startRabbit(Class additionalContext) { rabbitMqContainer = - new GenericContainer("rabbitmq:latest") + new GenericContainer<>("rabbitmq:latest") .withExposedPorts(5672) .waitingFor(Wait.forLogMessage(".*Server startup complete.*", 1)) .withStartupTimeout(Duration.ofMinutes(2)); @@ -64,7 +82,7 @@ public void startRabbit(Class additionalContext) { consumerContext = consumerApp.run(); } - private Class[] getContextClasses(Class mainContext, Class additionalContext) { + private static Class[] getContextClasses(Class mainContext, Class additionalContext) { List> contextClasses = new ArrayList<>(); contextClasses.add(mainContext); if (additionalContext != null) { @@ -73,7 +91,7 @@ private Class[] getContextClasses(Class mainContext, Class additionalCo return contextClasses.toArray(new Class[0]); } - public void stopRabbit() { + private void stopRabbit() { rabbitMqContainer.stop(); producerContext.close(); consumerContext.close(); @@ -87,13 +105,12 @@ static class ProducerConfig { @Bean Runnable producer() { - return () -> { - runWithSpan( - "producer", - () -> { - source.output().send(MessageBuilder.withPayload("test").build()); - }); - }; + return () -> + runWithSpan( + "producer", + () -> { + source.output().send(MessageBuilder.withPayload("test").build()); + }); } @SpringBootConfiguration diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamProducerTest.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamProducerTest.java index d23b602e28fb..568aa2a75f05 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamProducerTest.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamProducerTest.java @@ -7,8 +7,6 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public abstract class AbstractSpringCloudStreamProducerTest @@ -19,26 +17,14 @@ public abstract class AbstractSpringCloudStreamProducerTest private static final boolean HAS_PRODUCER_SPAN = Boolean.getBoolean("otel.instrumentation.spring-integration.producer.enabled"); - private final Class additionalContextClass; - public AbstractSpringCloudStreamProducerTest( InstrumentationExtension testing, Class additionalContextClass) { + super(additionalContextClass); this.testing = testing; - this.additionalContextClass = additionalContextClass; - } - - @BeforeEach - public void setupSpec() { - startRabbit(additionalContextClass); - } - - @AfterEach - public void cleanupSpec() { - stopRabbit(); } @Test - public void hasProducerSpan() { + void has_producer_span() { assumeTrue(HAS_PRODUCER_SPAN); producerContext.getBean("producer", Runnable.class).run(); @@ -46,21 +32,9 @@ public void hasProducerSpan() { testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> { - span.hasName("producer"); - span.hasKind(SpanKind.INTERNAL); - }, - span -> { - span.hasName("testProducer.output publish"); - span.hasKind(SpanKind.PRODUCER); - }, - span -> { - span.hasName("testConsumer.input process"); - span.hasKind(SpanKind.CONSUMER); - }, - span -> { - span.hasName("consumer"); - span.hasKind(SpanKind.INTERNAL); - })); + span -> span.hasName("producer").hasKind(SpanKind.INTERNAL), + span -> span.hasName("testProducer.output publish").hasKind(SpanKind.PRODUCER), + span -> span.hasName("testConsumer.input process").hasKind(SpanKind.CONSUMER), + span -> span.hasName("consumer").hasKind(SpanKind.INTERNAL))); } } diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamRabbitTest.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamRabbitTest.java index 4a7a5319fae0..65dc13700b58 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamRabbitTest.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringCloudStreamRabbitTest.java @@ -5,8 +5,6 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public abstract class AbstractSpringCloudStreamRabbitTest @@ -14,42 +12,21 @@ public abstract class AbstractSpringCloudStreamRabbitTest protected final InstrumentationExtension testing; - private final Class additionalContextClass; - public AbstractSpringCloudStreamRabbitTest( InstrumentationExtension testing, Class additionalContextClass) { + super(additionalContextClass); this.testing = testing; - this.additionalContextClass = additionalContextClass; - } - - @BeforeEach - public void setupSpec() { - startRabbit(additionalContextClass); - } - - @AfterEach - public void cleanupSpec() { - stopRabbit(); } @Test - public void shouldPropagateContextThroughRabbitMQ() { + void should_propagate_context_through_rabbit_mq() { producerContext.getBean("producer", Runnable.class).run(); testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> { - span.hasName("producer"); - span.hasKind(SpanKind.INTERNAL); - }, - span -> { - span.hasName("testConsumer.input process"); - span.hasKind(SpanKind.CONSUMER); - }, - span -> { - span.hasName("consumer"); - span.hasKind(SpanKind.INTERNAL); - })); + span -> span.hasName("producer").hasKind(SpanKind.INTERNAL), + span -> span.hasName("testConsumer.input process").hasKind(SpanKind.CONSUMER), + span -> span.hasName("consumer").hasKind(SpanKind.INTERNAL))); } } diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringIntegrationTracingTest.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringIntegrationTracingTest.java index c5e0e7d9e2af..18e40b61fcc7 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringIntegrationTracingTest.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/AbstractSpringIntegrationTracingTest.java @@ -48,7 +48,7 @@ public AbstractSpringIntegrationTracingTest( } @BeforeEach - public void setupSpec() { + public void setUp() { List> contextClasses = new ArrayList<>(); contextClasses.add(MessageChannelsConfig.class); if (additionalContextClass != null) { @@ -62,7 +62,7 @@ public void setupSpec() { } @AfterEach - public void cleanupSpec() { + public void tearDown() { applicationContext.close(); } @@ -73,7 +73,7 @@ public void cleanupSpec() { "executorChannel,executorChannel process" }, delimiter = ',') - public void shouldPropagateContext(String channelName, String interceptorSpanName) { + public void should_propagate_context(String channelName, String interceptorSpanName) { SubscribableChannel channel = applicationContext.getBean(channelName, SubscribableChannel.class); @@ -88,20 +88,16 @@ public void shouldPropagateContext(String channelName, String interceptorSpanNam trace -> trace.hasSpansSatisfyingExactly( span -> { - span.hasName(interceptorSpanName); - span.hasKind(SpanKind.CONSUMER); + span.hasName(interceptorSpanName).hasKind(SpanKind.CONSUMER); verifyCorrectSpanWasPropagated(capturedMessage, trace.getSpan(0)); }, - span -> { - span.hasName("handler"); - span.hasParentSpanId(trace.getSpan(0).getSpanId()); - })); + span -> span.hasName("handler").hasParentSpanId(trace.getSpan(0).getSpanId()))); channel.unsubscribe(messageHandler); } @Test - public void shouldNotAddInterceptorTwice() { + void should_not_add_interceptor_twice() { SubscribableChannel channel = applicationContext.getBean("directChannel1", SubscribableChannel.class); @@ -116,20 +112,16 @@ public void shouldNotAddInterceptorTwice() { trace -> trace.hasSpansSatisfyingExactly( span -> { - span.hasName("application.directChannel2 process"); - span.hasKind(SpanKind.CONSUMER); + span.hasName("application.directChannel2 process").hasKind(SpanKind.CONSUMER); verifyCorrectSpanWasPropagated(capturedMessage, trace.getSpan(0)); }, - span -> { - span.hasName("handler"); - span.hasParentSpanId(trace.getSpan(0).getSpanId()); - })); + span -> span.hasName("handler").hasParentSpanId(trace.getSpan(0).getSpanId()))); channel.unsubscribe(messageHandler); } @Test - public void shouldNotCreateASpanWhenThereIsAlreadyASpanInTheContext() { + void should_not_create_aspan_when_there_is_already_aspan_in_the_context() { SubscribableChannel channel = applicationContext.getBean("directChannel", SubscribableChannel.class); @@ -142,24 +134,19 @@ public void shouldNotCreateASpanWhenThereIsAlreadyASpanInTheContext() { channel.send(MessageBuilder.withPayload("test").build()); }); - Message capturedMessage = messageHandler.join(); + messageHandler.join(); testing.waitAndAssertTraces( trace -> trace.hasSpansSatisfyingExactly( - span -> { - span.hasName("parent"); - }, - span -> { - span.hasName("handler"); - span.hasParentSpanId(trace.getSpan(0).getSpanId()); - })); + span -> span.hasName("parent"), + span -> span.hasName("handler").hasParentSpanId(trace.getSpan(0).getSpanId()))); channel.unsubscribe(messageHandler); } @Test - public void shouldHandleMultipleMessageChannelsInAChain() { + void should_handle_multiple_message_channels_in_achain() { SubscribableChannel channel1 = applicationContext.getBean("linkedChannel1", SubscribableChannel.class); SubscribableChannel channel2 = @@ -176,20 +163,16 @@ public void shouldHandleMultipleMessageChannelsInAChain() { trace -> trace.hasSpansSatisfyingExactly( span -> { - span.hasName("application.linkedChannel1 process"); - span.hasKind(SpanKind.CONSUMER); + span.hasName("application.linkedChannel1 process").hasKind(SpanKind.CONSUMER); verifyCorrectSpanWasPropagated(capturedMessage, trace.getSpan(0)); }, - span -> { - span.hasName("handler"); - span.hasParentSpanId(trace.getSpan(0).getSpanId()); - })); + span -> span.hasName("handler").hasParentSpanId(trace.getSpan(0).getSpanId()))); channel2.unsubscribe(messageHandler); } @Test - public void captureMessageHeader() { + void capture_message_header() { SubscribableChannel channel = applicationContext.getBean("directChannel", SubscribableChannel.class); @@ -205,14 +188,10 @@ public void captureMessageHeader() { trace -> trace.hasSpansSatisfyingExactly( span -> { - span.hasName("application.directChannel process"); - span.hasKind(SpanKind.CONSUMER); + span.hasName("application.directChannel process").hasKind(SpanKind.CONSUMER); verifyCorrectSpanWasPropagated(capturedMessage, trace.getSpan(0)); }, - span -> { - span.hasName("handler"); - span.hasParentSpanId(trace.getSpan(0).getSpanId()); - })); + span -> span.hasName("handler").hasParentSpanId(trace.getSpan(0).getSpanId()))); channel.unsubscribe(messageHandler); } diff --git a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/CapturingMessageHandler.java b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/CapturingMessageHandler.java index 5b3ac61fe85e..c2821ef56792 100644 --- a/instrumentation/spring/spring-integration-4.1/testing/src/main/java/CapturingMessageHandler.java +++ b/instrumentation/spring/spring-integration-4.1/testing/src/main/java/CapturingMessageHandler.java @@ -8,13 +8,12 @@ import java.util.concurrent.CompletableFuture; import org.springframework.messaging.Message; import org.springframework.messaging.MessageHandler; -import org.springframework.messaging.MessagingException; class CapturingMessageHandler implements MessageHandler { final CompletableFuture> captured = new CompletableFuture<>(); @Override - public void handleMessage(Message message) throws MessagingException { + public void handleMessage(Message message) { runWithSpan( "handler", () -> {