Skip to content

Commit

Permalink
convert spring tests to java
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Aug 13, 2024
1 parent 4b86970 commit 975ad64
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 199 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefaultFullHttpRequest> {
Expand All @@ -49,7 +50,8 @@ class Netty40ClientTest extends AbstractHttpClientTest<DefaultFullHttpRequest> {

private final Bootstrap readTimeoutBootstrap = buildBootstrap(true);

void cleanupSpec() {
@AfterEach
void tearDown() {
if (eventLoopGroup != null) {
eventLoopGroup.shutdownGracefully();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassLoader> classLoaderOptimization() {
return hasClassesNamed("org.springframework.context.support.AbstractApplicationContext");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")))));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AbstractComplexPropagationTest(
}

@BeforeEach
void setupSpec() {
void setUp() {
List<Class<?>> contextClasses = new ArrayList<>();
contextClasses.add(ExternalQueueConfig.class);
if (additionalContextClass != null) {
Expand All @@ -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 =
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 975ad64

Please sign in to comment.