Skip to content

Commit 6ce3bd8

Browse files
authored
Add network attributes to rabbitmq process spans (#10210)
1 parent 2aaa7b1 commit 6ce3bd8

File tree

9 files changed

+64
-12
lines changed

9 files changed

+64
-12
lines changed

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@
77

88
import com.google.auto.value.AutoValue;
99
import com.rabbitmq.client.AMQP;
10+
import com.rabbitmq.client.Connection;
1011
import com.rabbitmq.client.Envelope;
1112

1213
@AutoValue
1314
abstract class DeliveryRequest {
1415

1516
static DeliveryRequest create(
16-
String queue, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
17-
return new AutoValue_DeliveryRequest(queue, envelope, properties, body);
17+
String queue,
18+
Envelope envelope,
19+
Connection connection,
20+
AMQP.BasicProperties properties,
21+
byte[] body) {
22+
return new AutoValue_DeliveryRequest(queue, envelope, connection, properties, body);
1823
}
1924

2025
abstract String getQueue();
2126

2227
abstract Envelope getEnvelope();
2328

29+
abstract Connection getConnection();
30+
2431
abstract AMQP.BasicProperties getProperties();
2532

2633
@SuppressWarnings("mutable")

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,12 @@ public static class ChannelConsumeAdvice {
235235

236236
@Advice.OnMethodEnter(suppress = Throwable.class)
237237
public static void wrapConsumer(
238+
@Advice.This Channel channel,
238239
@Advice.Argument(0) String queue,
239240
@Advice.Argument(value = 6, readOnly = false) Consumer consumer) {
240241
// We have to save off the queue name here because it isn't available to the consumer later.
241242
if (consumer != null && !(consumer instanceof TracedDelegatingConsumer)) {
242-
consumer = new TracedDelegatingConsumer(queue, consumer);
243+
consumer = new TracedDelegatingConsumer(queue, consumer, channel.getConnection());
243244
}
244245
}
245246
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.rabbitmq;
7+
8+
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesGetter;
9+
import java.net.Inet4Address;
10+
import java.net.Inet6Address;
11+
import java.net.InetAddress;
12+
import javax.annotation.Nullable;
13+
14+
public class RabbitDeliveryNetAttributesGetter
15+
implements NetworkAttributesGetter<DeliveryRequest, Void> {
16+
17+
@Nullable
18+
@Override
19+
public String getNetworkType(DeliveryRequest request, @Nullable Void response) {
20+
InetAddress address = request.getConnection().getAddress();
21+
if (address instanceof Inet4Address) {
22+
return "ipv4";
23+
} else if (address instanceof Inet6Address) {
24+
return "ipv6";
25+
}
26+
return null;
27+
}
28+
29+
@Nullable
30+
@Override
31+
public String getNetworkPeerAddress(DeliveryRequest request, @Nullable Void response) {
32+
return request.getConnection().getAddress().getHostAddress();
33+
}
34+
35+
@Nullable
36+
@Override
37+
public Integer getNetworkPeerPort(DeliveryRequest request, @Nullable Void response) {
38+
return request.getConnection().getPort();
39+
}
40+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class RabbitReceiveNetAttributesGetter
2020
public String getNetworkType(ReceiveRequest request, @Nullable GetResponse response) {
2121
InetAddress address = request.getConnection().getAddress();
2222
if (address instanceof Inet4Address) {
23-
return "ipv6";
23+
return "ipv4";
2424
} else if (address instanceof Inet6Address) {
2525
return "ipv6";
2626
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private static Instrumenter<DeliveryRequest, Void> createDeliverInstrumenter() {
9696
extractors.add(
9797
buildMessagingAttributesExtractor(
9898
RabbitDeliveryAttributesGetter.INSTANCE, MessageOperation.PROCESS));
99+
extractors.add(NetworkAttributesExtractor.create(new RabbitDeliveryNetAttributesGetter()));
99100
extractors.add(new RabbitDeliveryExtraAttributesExtractor());
100101
if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
101102
extractors.add(new RabbitDeliveryExperimentalAttributesExtractor());

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.javaagent.instrumentation.rabbitmq.RabbitSingletons.deliverInstrumenter;
99

1010
import com.rabbitmq.client.AMQP;
11+
import com.rabbitmq.client.Connection;
1112
import com.rabbitmq.client.Consumer;
1213
import com.rabbitmq.client.Envelope;
1314
import com.rabbitmq.client.ShutdownSignalException;
@@ -23,10 +24,12 @@ public class TracedDelegatingConsumer implements Consumer {
2324

2425
private final String queue;
2526
private final Consumer delegate;
27+
private final Connection connection;
2628

27-
public TracedDelegatingConsumer(String queue, Consumer delegate) {
29+
public TracedDelegatingConsumer(String queue, Consumer delegate, Connection connection) {
2830
this.queue = queue;
2931
this.delegate = delegate;
32+
this.connection = connection;
3033
}
3134

3235
@Override
@@ -59,7 +62,7 @@ public void handleDelivery(
5962
String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
6063
throws IOException {
6164
Context parentContext = Context.current();
62-
DeliveryRequest request = DeliveryRequest.create(queue, envelope, properties, body);
65+
DeliveryRequest request = DeliveryRequest.create(queue, envelope, connection, properties, body);
6366

6467
if (!deliverInstrumenter().shouldStart(parentContext, request)) {
6568
delegate.handleDelivery(consumerTag, envelope, properties, body);

instrumentation/rabbitmq-2.7/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitMqTest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,7 @@ private static void verifySpan(
760760
verifyException(span, exception, errorMsg);
761761
}
762762

763-
// listener does not have access to net attributes
764-
if (!"basic.deliver".equals(rabbitCommand)) {
765-
verifyNetAttributes(span);
766-
}
767-
763+
verifyNetAttributes(span);
768764
verifyMessagingAttributes(span, exchange, routingKey, operation);
769765

770766
if (expectTimestamp) {

instrumentation/spring/spring-integration-4.1/javaagent/src/test/groovy/SpringIntegrationAndRabbitTest.groovy

+3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ class SpringIntegrationAndRabbitTest extends AgentInstrumentationSpecification i
7474
childOf span(3)
7575
kind CONSUMER
7676
attributes {
77+
"$NetworkAttributes.NETWORK_PEER_ADDRESS" { it == "127.0.0.1" || it == "0:0:0:0:0:0:0:1" || it == null }
78+
"$NetworkAttributes.NETWORK_PEER_PORT" Long
79+
"$SemanticAttributes.NETWORK_TYPE" { it == "ipv4" || it == "ipv6" || it == null }
7780
"$SemanticAttributes.MESSAGING_SYSTEM" "rabbitmq"
7881
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "testTopic"
7982
"$SemanticAttributes.MESSAGING_OPERATION" "process"

instrumentation/spring/spring-rabbit-1.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/spring/rabbit/v1_0/ContextPropagationTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public void test(boolean testHeaders) throws Exception {
172172
.hasKind(SpanKind.CONSUMER)
173173
.hasParent(trace.getSpan(1))
174174
.hasAttributesSatisfyingExactly(
175-
getAssertions("<default>", "process", null, true, testHeaders)),
175+
getAssertions(
176+
"<default>", "process", "127.0.0.1", true, testHeaders)),
176177
// created by spring-rabbit instrumentation
177178
span ->
178179
span.hasName("testQueue process")

0 commit comments

Comments
 (0)