Skip to content

Commit 98a8dfd

Browse files
committed
address PR comments - add the same logic to the sender side
1 parent e038247 commit 98a8dfd

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-plugin/src/main/java/co/elastic/apm/agent/rabbitmq/ChannelInstrumentation.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation;
2525
import co.elastic.apm.agent.tracer.ElasticContext;
2626
import co.elastic.apm.agent.tracer.Span;
27+
import co.elastic.apm.agent.tracer.configuration.MessagingConfiguration;
2728
import com.rabbitmq.client.AMQP;
2829
import com.rabbitmq.client.Channel;
2930
import com.rabbitmq.client.Connection;
@@ -137,10 +138,9 @@ public static Object[] onBasicPublish(@Advice.This Channel channel,
137138
if (exitSpan != null) {
138139

139140
exchange = normalizeExchangeName(exchange);
140-
141+
String transactionNameSuffix = normalizeExchangeName(resolveTransactionNameSuffix(exchange, routingKey));
141142
exitSpan.withAction("send")
142-
.withName("RabbitMQ SEND to ").appendToName(exchange);
143-
143+
.withName("RabbitMQ SEND to ").appendToName(transactionNameSuffix);
144144
}
145145

146146
properties = propagateTraceContext(tracer.currentContext(), properties);
@@ -174,6 +174,15 @@ private static AMQP.BasicProperties propagateTraceContext(ElasticContext<?> toPr
174174
return properties.builder().headers(headersWithContext).build();
175175
}
176176

177+
private static String resolveTransactionNameSuffix(String exchange, String routingKey) {
178+
179+
if (MessagingConfiguration.RabbitMQNamingMode.ROUTING_KEY == AbstractBaseInstrumentation.getRabbitMQNamingMode()) {
180+
return routingKey;
181+
} else {
182+
return exchange;
183+
}
184+
}
185+
177186
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)
178187
public static void afterBasicPublish(@Advice.Enter @Nullable Object[] enterArray,
179188
@Advice.Thrown @Nullable Throwable throwable) {

apm-agent-plugins/apm-rabbitmq/apm-rabbitmq-plugin/src/test/java/co/elastic/apm/agent/rabbitmq/RabbitMQIT.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProp
316316
checkTransaction(childTransaction, exchange, transactionNameSuffix, "RabbitMQ");
317317

318318
Span span = getReporter().getSpans().get(0);
319-
checkSendSpan(span, exchange);
319+
checkSendSpan(span, exchange, transactionNameSuffix);
320320

321321
// span should be child of the first transaction
322322
checkParentChild(rootTransaction, span);
@@ -706,14 +706,23 @@ private static HashMap<String, String> getHeadersMap(Message message) {
706706
}
707707

708708
private static void checkSendSpan(Span span, String exchange) {
709-
checkSendSpan(span, exchange, connection.getAddress().getHostAddress(), connection.getPort());
709+
checkSendSpan(span, exchange, exchange, connection.getAddress().getHostAddress(), connection.getPort());
710+
}
711+
712+
private static void checkSendSpan(Span span, String exchange, String spanNameSuffix) {
713+
checkSendSpan(span, exchange, spanNameSuffix, connection.getAddress().getHostAddress(), connection.getPort());
710714
}
711715

712716
static void checkSendSpan(Span span, String exchange, String host, int port) {
717+
checkSendSpan(span, exchange, exchange, host, port);
718+
}
719+
720+
static void checkSendSpan(Span span, String exchange, String spanNameSuffix, String host, int port) {
713721
String exchangeName = exchange.isEmpty() ? "<default>" : exchange;
722+
String spanName = spanNameSuffix.isEmpty() ? "<default>" : spanNameSuffix;
714723
checkSpanCommon(span,
715724
"send",
716-
String.format("RabbitMQ SEND to %s", exchangeName),
725+
String.format("RabbitMQ SEND to %s", spanName),
717726
exchangeName,
718727
true
719728
);

apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/configuration/MessagingConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public class MessagingConfiguration extends ConfigurationOptionProvider {
112112
.key("rabbitmq_naming_mode")
113113
.configurationCategory(MESSAGING_CATEGORY)
114114
.description("Defines whether the agent should use the exchanges, the routing key or the queue for the naming of RabbitMQ Transactions. Valid options are `QUEUE`, `ROUTING_KEY` and `EXCHANGE`.\n" +
115-
"Note that `QUEUE` only works when using RabbitMQ via spring-amqp."
115+
"Note that `QUEUE` only works when using RabbitMQ via spring-amqp and `ROUTING_KEY` only works for the non spring-client."
116116
)
117117
.dynamic(true)
118118
.tags("added[1.46.0]")

docs/configuration.asciidoc

+4-4
Original file line numberDiff line numberDiff line change
@@ -2514,7 +2514,7 @@ Starting from version 1.43.0, the classes that are part of the 'application_pack
25142514
==== `rabbitmq_naming_mode` (added[1.46.0])
25152515

25162516
Defines whether the agent should use the exchanges, the routing key or the queue for the naming of RabbitMQ Transactions. Valid options are `QUEUE`, `ROUTING_KEY` and `EXCHANGE`.
2517-
Note that `QUEUE` only works when using RabbitMQ via spring-amqp.
2517+
Note that `QUEUE` only works when using RabbitMQ via spring-amqp and `ROUTING_KEY` only works for the non spring-client.
25182518

25192519
<<configuration-dynamic, image:./images/dynamic-config.svg[] >>
25202520

@@ -4703,10 +4703,10 @@ Example: `5ms`.
47034703
#
47044704
# jms_listener_packages=
47054705
4706-
# Defines whether the agent should use the exchanges, the routing key or the queue for the naming of RabbitMQ Transactions. Valid options are `QUEUE` and `EXCHANGE`.
4707-
# Note that `QUEUE` only works when using RabbitMQ via spring-amqp.
4706+
# Defines whether the agent should use the exchanges, the routing key or the queue for the naming of RabbitMQ Transactions. Valid options are `QUEUE`, `ROUTING_KEY` and `EXCHANGE`.
4707+
# Note that `QUEUE` only works when using RabbitMQ via spring-amqp and `ROUTING_KEY` only works for the non spring-client.
47084708
#
4709-
# Valid options: EXCHANGE, QUEUE
4709+
# Valid options: EXCHANGE, QUEUE, ROUTING_KEY
47104710
# This setting can be changed at runtime
47114711
# Type: RabbitMQNamingMode
47124712
# Default value: EXCHANGE

0 commit comments

Comments
 (0)