Skip to content

Commit e1a9b73

Browse files
test(rabbitmq): add coverage for new network semantic conventions
1 parent 56b179f commit e1a9b73

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 static org.assertj.core.api.Assertions.assertThat;
9+
10+
import com.rabbitmq.client.Channel;
11+
import com.rabbitmq.client.Connection;
12+
import com.rabbitmq.client.ConnectionFactory;
13+
import com.rabbitmq.client.GetResponse;
14+
import io.opentelemetry.api.common.AttributeKey;
15+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
16+
import java.nio.charset.StandardCharsets;
17+
import java.util.List;
18+
import java.util.UUID;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.RegisterExtension;
21+
import org.testcontainers.containers.RabbitMQContainer;
22+
import org.testcontainers.utility.DockerImageName;
23+
24+
/**
25+
* Verifies that publish/consume spans have at least one of the new network semantic-convention
26+
* keys: - network.peer.address / network.peer.port - OR server.address / server.port
27+
*
28+
* <p>Test-only; no production code changed.
29+
*/
30+
public class RabbitMqNewNetAttributesTest {
31+
32+
@RegisterExtension
33+
static final InstrumentationExtension testing = InstrumentationExtension.create();
34+
35+
static final DockerImageName IMAGE = DockerImageName.parse("rabbitmq:3.13-alpine");
36+
37+
@Test
38+
void publish_and_consume_contains_new_network_attributes() throws Exception {
39+
try (RabbitMQContainer rabbit = new RabbitMQContainer(IMAGE)) {
40+
rabbit.start();
41+
42+
ConnectionFactory factory = new ConnectionFactory();
43+
factory.setHost(rabbit.getHost());
44+
factory.setPort(rabbit.getAmqpPort());
45+
46+
try (Connection connection = factory.newConnection();
47+
Channel channel = connection.createChannel()) {
48+
49+
String queue = "q-" + UUID.randomUUID();
50+
channel.queueDeclare(queue, false, false, true, null);
51+
52+
// produce + consume
53+
channel.basicPublish("", queue, null, "hello".getBytes(StandardCharsets.UTF_8));
54+
GetResponse res = null;
55+
for (int i = 0; i < 30 && res == null; i++) {
56+
res = channel.basicGet(queue, true);
57+
Thread.sleep(100);
58+
}
59+
60+
// Wait for spans, then assert at least one has new network attributes
61+
testing.waitAndAssertTraces(
62+
traces -> traces.anySatisfy(trace -> trace.spans().size() >= 1));
63+
64+
List<io.opentelemetry.sdk.trace.data.SpanData> spans = testing.getExportedSpans();
65+
66+
AttributeKey<String> NET_PEER_ADDR = AttributeKey.stringKey("network.peer.address");
67+
AttributeKey<Long> NET_PEER_PORT = AttributeKey.longKey("network.peer.port");
68+
AttributeKey<String> SERVER_ADDR = AttributeKey.stringKey("server.address");
69+
AttributeKey<Long> SERVER_PORT = AttributeKey.longKey("server.port");
70+
71+
boolean foundNewNetAttr =
72+
spans.stream()
73+
.anyMatch(
74+
span -> {
75+
var attrs = span.getAttributes();
76+
boolean hasPeer =
77+
attrs.get(NET_PEER_ADDR) != null || attrs.get(NET_PEER_PORT) != null;
78+
boolean hasServer =
79+
attrs.get(SERVER_ADDR) != null || attrs.get(SERVER_PORT) != null;
80+
return hasPeer || hasServer;
81+
});
82+
83+
assertThat(foundNewNetAttr)
84+
.as("at least one span should carry new network attributes")
85+
.isTrue();
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)