Skip to content

Commit 84d0511

Browse files
authored
Use constant span name when using Spring AMQP AnonymousQueues (#11141)
1 parent bc5e032 commit 84d0511

File tree

2 files changed

+40
-3
lines changed
  • instrumentation
    • rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq
    • spring/spring-rabbit-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/rabbit/v1_0

2 files changed

+40
-3
lines changed

instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/DeliveryRequest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ String spanName() {
3737
String queue = getQueue();
3838
if (queue == null || queue.isEmpty()) {
3939
return "<default> process";
40-
} else if (queue.startsWith("amq.gen-")) {
40+
} else if (queue.startsWith("amq.gen-") || queue.startsWith("spring.gen-")) {
41+
// The spring.gen-<random uid> name comes from AnonymousQueue in the Spring AMQP library
4142
return "<generated> process";
4243
} else {
4344
return queue + " process";
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
import org.assertj.core.api.AbstractStringAssert;
3232
import org.junit.jupiter.api.AfterAll;
3333
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
3435
import org.junit.jupiter.api.extension.RegisterExtension;
3536
import org.junit.jupiter.params.ParameterizedTest;
3637
import org.junit.jupiter.params.provider.ValueSource;
3738
import org.springframework.amqp.core.AmqpTemplate;
39+
import org.springframework.amqp.core.AnonymousQueue;
3840
import org.springframework.amqp.core.Queue;
3941
import org.springframework.amqp.rabbit.annotation.RabbitListener;
4042
import org.springframework.boot.SpringApplication;
@@ -45,7 +47,7 @@
4547
import org.testcontainers.containers.GenericContainer;
4648
import org.testcontainers.containers.wait.strategy.Wait;
4749

48-
public class ContextPropagationTest {
50+
public class SpringRabbitMqTest {
4951

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

129131
@ParameterizedTest
130132
@ValueSource(booleans = {true, false})
131-
public void test(boolean testHeaders) throws Exception {
133+
public void testContextPropagation(boolean testHeaders) throws Exception {
132134
try (Connection connection = connectionFactory.newConnection()) {
133135
try (Channel ignored = connection.createChannel()) {
134136
testing.runWithSpan(
@@ -218,6 +220,35 @@ public void test(boolean testHeaders) throws Exception {
218220
}
219221
}
220222

223+
@Test
224+
public void testAnonymousQueueSpanName() throws Exception {
225+
try (Connection connection = connectionFactory.newConnection()) {
226+
try (Channel ignored = connection.createChannel()) {
227+
String anonymousQueueName = applicationContext.getBean(AnonymousQueue.class).getName();
228+
applicationContext.getBean(AmqpTemplate.class).convertAndSend(anonymousQueueName, "test");
229+
applicationContext.getBean(AmqpTemplate.class).receive(anonymousQueueName, 5000);
230+
231+
testing.waitAndAssertTraces(
232+
trace ->
233+
trace.hasSpansSatisfyingExactly(
234+
span -> span.hasName("<default> publish"),
235+
// Verify that a constant span name is used instead of the randomly generated
236+
// anonymous queue name
237+
span ->
238+
span.hasName("<generated> process")
239+
.hasAttribute(
240+
equalTo(
241+
MessagingIncubatingAttributes
242+
.MESSAGING_RABBITMQ_DESTINATION_ROUTING_KEY,
243+
anonymousQueueName))),
244+
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.qos")),
245+
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.consume")),
246+
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.cancel")),
247+
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("basic.ack")));
248+
}
249+
}
250+
}
251+
221252
@SpringBootConfiguration
222253
@EnableAutoConfiguration
223254
static class ConsumerConfig {
@@ -229,6 +260,11 @@ Queue testQueue() {
229260
return new Queue(TEST_QUEUE);
230261
}
231262

263+
@Bean
264+
AnonymousQueue anonymousQueue() {
265+
return new AnonymousQueue();
266+
}
267+
232268
@RabbitListener(queues = TEST_QUEUE)
233269
void consume(String ignored) {
234270
GlobalTraceUtil.runWithSpan("consumer", () -> {});

0 commit comments

Comments
 (0)