Skip to content

Commit 8ea5cb4

Browse files
committed
add network span assertions to OpenSearchJavaAwsSdk2TransportTest
1 parent bde4f45 commit 8ea5cb4

File tree

5 files changed

+59
-60
lines changed

5 files changed

+59
-60
lines changed

instrumentation/opensearch/opensearch-java-3.0/javaagent/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ dependencies {
2323
testImplementation(project(":instrumentation:opensearch:opensearch-rest-common:testing"))
2424
testInstrumentation(project(":instrumentation:apache-httpclient:apache-httpclient-5.0:javaagent"))
2525

26+
// For testing AwsSdk2Transport
27+
testInstrumentation(project(":instrumentation:apache-httpclient:apache-httpclient-4.0:javaagent"))
28+
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
2629
testImplementation("software.amazon.awssdk:auth:2.22.0")
2730
testImplementation("software.amazon.awssdk:identity-spi:2.22.0")
2831
testImplementation("software.amazon.awssdk:apache-client:2.22.0")

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opensearch/java/v3_0/OpenSearchJavaInstrumenterFactory.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@
1111
import io.opentelemetry.instrumentation.api.incubator.semconv.db.DbClientSpanNameExtractor;
1212
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1313
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
14-
import io.opentelemetry.instrumentation.api.semconv.network.NetworkAttributesExtractor;
1514

1615
public final class OpenSearchJavaInstrumenterFactory {
1716

1817
public static Instrumenter<OpenSearchJavaRequest, OpenSearchJavaResponse> create(
1918
String instrumentationName) {
2019
OpenSearchJavaAttributesGetter dbClientAttributesGetter = new OpenSearchJavaAttributesGetter();
21-
OpenSearchJavaNetResponseAttributesGetter netAttributesGetter =
22-
new OpenSearchJavaNetResponseAttributesGetter();
2320

2421
return Instrumenter.<OpenSearchJavaRequest, OpenSearchJavaResponse>builder(
2522
GlobalOpenTelemetry.get(),
2623
instrumentationName,
2724
DbClientSpanNameExtractor.create(dbClientAttributesGetter))
2825
.addAttributesExtractor(DbClientAttributesExtractor.create(dbClientAttributesGetter))
29-
.addAttributesExtractor(NetworkAttributesExtractor.create(netAttributesGetter))
3026
.addOperationMetrics(DbClientMetrics.get())
3127
.buildInstrumenter(SpanKindExtractor.alwaysClient());
3228
}

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opensearch/java/v3_0/OpenSearchJavaNetResponseAttributesGetter.java

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,6 @@
66
package io.opentelemetry.javaagent.instrumentation.opensearch.java.v3_0;
77

88
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;
139

1410
final class OpenSearchJavaNetResponseAttributesGetter
15-
implements NetworkAttributesGetter<OpenSearchJavaRequest, OpenSearchJavaResponse> {
16-
17-
@Nullable
18-
@Override
19-
public String getNetworkType(
20-
OpenSearchJavaRequest request, @Nullable OpenSearchJavaResponse response) {
21-
if (response == null) {
22-
return null;
23-
}
24-
InetAddress address = response.getAddress();
25-
if (address instanceof Inet4Address) {
26-
return "ipv4";
27-
} else if (address instanceof Inet6Address) {
28-
return "ipv6";
29-
}
30-
return null;
31-
}
32-
33-
@Override
34-
@Nullable
35-
public String getNetworkPeerAddress(
36-
OpenSearchJavaRequest request, @Nullable OpenSearchJavaResponse response) {
37-
if (response != null && response.getAddress() != null) {
38-
return response.getAddress().getHostAddress();
39-
}
40-
return null;
41-
}
42-
}
11+
implements NetworkAttributesGetter<OpenSearchJavaRequest, OpenSearchJavaResponse> {}

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opensearch/java/v3_0/AbstractOpenSearchJavaTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ void setUp() throws Exception {
6262
opensearch =
6363
new OpensearchContainer(DockerImageName.parse("opensearchproject/opensearch:1.3.6"))
6464
.withSecurityEnabled();
65-
// limit memory usage
6665
opensearch.withEnv("OPENSEARCH_JAVA_OPTS", "-Xmx256m -Xms256m");
6766
opensearch.start();
6867
httpHost = URI.create(opensearch.getHttpHostAddress());

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opensearch/java/v3_0/OpenSearchJavaAwsSdk2TransportTest.java

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
99
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
10+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_REQUEST_METHOD;
11+
import static io.opentelemetry.semconv.HttpAttributes.HTTP_RESPONSE_STATUS_CODE;
12+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_ADDRESS;
13+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PEER_PORT;
14+
import static io.opentelemetry.semconv.NetworkAttributes.NETWORK_PROTOCOL_VERSION;
15+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_ADDRESS;
16+
import static io.opentelemetry.semconv.ServerAttributes.SERVER_PORT;
17+
import static io.opentelemetry.semconv.UrlAttributes.URL_FULL;
1018
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_OPERATION;
1119
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT;
1220
import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_SYSTEM;
@@ -19,8 +27,8 @@
1927
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
2028
import io.opentelemetry.testing.internal.armeria.common.MediaType;
2129
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension;
22-
import java.io.IOException;
2330
import java.util.concurrent.CompletableFuture;
31+
import java.util.concurrent.CountDownLatch;
2432
import java.util.concurrent.atomic.AtomicReference;
2533
import org.junit.jupiter.api.AfterAll;
2634
import org.junit.jupiter.api.BeforeAll;
@@ -42,7 +50,7 @@
4250
import software.amazon.awssdk.regions.Region;
4351
import software.amazon.awssdk.utils.AttributeMap;
4452

45-
@SuppressWarnings("deprecation")
53+
@SuppressWarnings("deprecation") // using deprecated semconv
4654
public class OpenSearchJavaAwsSdk2TransportTest extends AbstractOpenSearchJavaTest {
4755

4856
protected static final MockWebServerExtension server = new MockWebServerExtension();
@@ -60,6 +68,7 @@ void setUp() throws Exception {
6068
server.start();
6169
openSearchClient = buildOpenSearchClient();
6270
openSearchAsyncClient = buildOpenSearchAsyncClient();
71+
httpHost = server.httpsUri();
6372
}
6473

6574
@AfterAll
@@ -140,35 +149,35 @@ protected OpenSearchAsyncClient buildOpenSearchAsyncClient() throws Exception {
140149
return new OpenSearchAsyncClient(transport);
141150
}
142151

143-
@Test
144-
@Override
145-
void shouldGetStatusWithTraces() throws IOException {
146-
HealthResponse healthResponse = openSearchClient.cluster().health();
147-
assertThat(healthResponse).isNotNull();
148-
149-
getTesting()
150-
.waitAndAssertTraces(
151-
trace ->
152-
trace.hasSpansSatisfyingExactly(
153-
span ->
154-
span.hasName("GET")
155-
.hasKind(SpanKind.CLIENT)
156-
.hasAttributesSatisfyingExactly(
157-
equalTo(maybeStable(DB_SYSTEM), "opensearch"),
158-
equalTo(maybeStable(DB_OPERATION), "GET"),
159-
equalTo(maybeStable(DB_STATEMENT), "GET /_cluster/health"))));
160-
}
161-
162152
@Test
163153
@Override
164154
void shouldGetStatusAsyncWithTraces() throws Exception {
165155
AtomicReference<CompletableFuture<HealthResponse>> responseCompletableFuture =
166156
new AtomicReference<>();
157+
CountDownLatch countDownLatch = new CountDownLatch(1);
167158

168159
getTesting()
169160
.runWithSpan(
170161
"client",
171-
() -> responseCompletableFuture.set(openSearchAsyncClient.cluster().health()));
162+
() -> {
163+
CompletableFuture<HealthResponse> future = openSearchAsyncClient.cluster().health();
164+
responseCompletableFuture.set(future);
165+
166+
future.whenComplete(
167+
(response, throwable) -> {
168+
getTesting()
169+
.runWithSpan(
170+
"callback",
171+
() -> {
172+
if (throwable != null) {
173+
throw new RuntimeException(throwable);
174+
}
175+
countDownLatch.countDown();
176+
});
177+
});
178+
});
179+
180+
countDownLatch.await();
172181
HealthResponse healthResponse = responseCompletableFuture.get().get();
173182
assertThat(healthResponse).isNotNull();
174183

@@ -184,6 +193,29 @@ void shouldGetStatusAsyncWithTraces() throws Exception {
184193
.hasAttributesSatisfyingExactly(
185194
equalTo(maybeStable(DB_SYSTEM), "opensearch"),
186195
equalTo(maybeStable(DB_OPERATION), "GET"),
187-
equalTo(maybeStable(DB_STATEMENT), "GET /_cluster/health"))));
196+
equalTo(maybeStable(DB_STATEMENT), "GET /_cluster/health")),
197+
span ->
198+
span.hasName("GET")
199+
.hasKind(SpanKind.CLIENT)
200+
.hasParent(trace.getSpan(1))
201+
.hasAttributesSatisfyingExactly(
202+
equalTo(NETWORK_PROTOCOL_VERSION, "1.1"),
203+
equalTo(SERVER_ADDRESS, httpHost.getHost()),
204+
equalTo(SERVER_PORT, httpHost.getPort()),
205+
equalTo(HTTP_REQUEST_METHOD, "GET"),
206+
equalTo(URL_FULL, httpHost + "/_cluster/health"),
207+
equalTo(
208+
NETWORK_PEER_ADDRESS,
209+
httpHost.getHost()), // Netty 4.1 Instrumentation collects
210+
// NETWORK_PEER_ADDRESS
211+
equalTo(
212+
NETWORK_PEER_PORT,
213+
httpHost.getPort()), // Netty 4.1 Instrumentation collects
214+
// NETWORK_PEER_PORT
215+
equalTo(HTTP_RESPONSE_STATUS_CODE, 200L)),
216+
span ->
217+
span.hasName("callback")
218+
.hasKind(SpanKind.INTERNAL)
219+
.hasParent(trace.getSpan(1))));
188220
}
189221
}

0 commit comments

Comments
 (0)