Skip to content

Use constant span name when using Spring AMQP AnonymousQueues #11141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading