Skip to content

Commit

Permalink
Use constant span name when using Spring AMQP AnonymousQueues (#11141)
Browse files Browse the repository at this point in the history
  • Loading branch information
swar8080 committed Apr 22, 2024
1 parent bc5e032 commit 84d0511
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ String spanName() {
String queue = getQueue();
if (queue == null || queue.isEmpty()) {
return "<default> process";
} else if (queue.startsWith("amq.gen-")) {
} else if (queue.startsWith("amq.gen-") || queue.startsWith("spring.gen-")) {
// The spring.gen-<random uid> name comes from AnonymousQueue in the Spring AMQP library
return "<generated> process";
} else {
return queue + " process";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import org.assertj.core.api.AbstractStringAssert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.SpringApplication;
Expand All @@ -45,7 +47,7 @@
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;

public class ContextPropagationTest {
public class SpringRabbitMqTest {

@RegisterExtension
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
Expand Down Expand Up @@ -128,7 +130,7 @@ private static List<AttributeAssertion> getAssertions(

@ParameterizedTest
@ValueSource(booleans = {true, false})
public void test(boolean testHeaders) throws Exception {
public void testContextPropagation(boolean testHeaders) throws Exception {
try (Connection connection = connectionFactory.newConnection()) {
try (Channel ignored = connection.createChannel()) {
testing.runWithSpan(
Expand Down Expand Up @@ -218,6 +220,35 @@ public void test(boolean testHeaders) throws Exception {
}
}

@Test
public void testAnonymousQueueSpanName() throws Exception {
try (Connection connection = connectionFactory.newConnection()) {
try (Channel ignored = connection.createChannel()) {
String anonymousQueueName = applicationContext.getBean(AnonymousQueue.class).getName();
applicationContext.getBean(AmqpTemplate.class).convertAndSend(anonymousQueueName, "test");
applicationContext.getBean(AmqpTemplate.class).receive(anonymousQueueName, 5000);

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span -> span.hasName("<default> publish"),
// Verify that a constant span name is used instead of the randomly generated
// anonymous queue name
span ->
span.hasName("<generated> process")
.hasAttribute(
equalTo(
MessagingIncubatingAttributes
.MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY,
anonymousQueueName))),
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.qos")),
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.consume")),
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.cancel")),
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.ack")));
}
}
}

@SpringBootConfiguration
@EnableAutoConfiguration
static class ConsumerConfig {
Expand All @@ -229,6 +260,11 @@ Queue testQueue() {
return new Queue(TEST_QUEUE);
}

@Bean
AnonymousQueue anonymousQueue() {
return new AnonymousQueue();
}

@RabbitListener(queues = TEST_QUEUE)
void consume(String ignored) {
GlobalTraceUtil.runWithSpan("consumer", () -> {});
Expand Down

0 comments on commit 84d0511

Please sign in to comment.