Skip to content

Commit 2a68a47

Browse files
authored
Convert elasticsearch-rest-5.0 Test from groovy to java. (#8542)
1 parent 2d7bd70 commit 2a68a47

File tree

5 files changed

+200
-180
lines changed

5 files changed

+200
-180
lines changed

instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies {
2727

2828
testImplementation("org.apache.logging.log4j:log4j-core:2.11.0")
2929
testImplementation("org.apache.logging.log4j:log4j-api:2.11.0")
30+
testImplementation("com.fasterxml.jackson.core:jackson-databind")
3031

3132
testImplementation("org.testcontainers:elasticsearch")
3233
testLibrary("org.elasticsearch.client:rest:5.0.0")

instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/groovy/ElasticsearchRest5Test.groovy

Lines changed: 0 additions & 179 deletions
This file was deleted.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
7+
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import io.opentelemetry.api.common.AttributeKey;
10+
import io.opentelemetry.api.trace.SpanKind;
11+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
12+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
13+
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
14+
import java.io.IOException;
15+
import java.util.Map;
16+
import java.util.concurrent.CountDownLatch;
17+
import org.apache.http.HttpHost;
18+
import org.elasticsearch.client.Response;
19+
import org.elasticsearch.client.ResponseListener;
20+
import org.elasticsearch.client.RestClient;
21+
import org.junit.jupiter.api.AfterAll;
22+
import org.junit.jupiter.api.Assertions;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.RegisterExtension;
26+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
27+
28+
public class ElasticsearchRest5Test {
29+
30+
@RegisterExtension
31+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
32+
33+
static ElasticsearchContainer elasticsearch;
34+
35+
static HttpHost httpHost;
36+
37+
static RestClient client;
38+
39+
static ObjectMapper objectMapper;
40+
41+
@BeforeAll
42+
static void setup() {
43+
if (!Boolean.getBoolean("testLatestDeps")) {
44+
elasticsearch =
45+
new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:5.6.16")
46+
.withEnv("xpack.ml.enabled", "false")
47+
.withEnv("xpack.security.enabled", "false");
48+
} else {
49+
elasticsearch =
50+
new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.16");
51+
}
52+
// limit memory usage
53+
elasticsearch.withEnv("ES_JAVA_OPTS", "-Xmx256m -Xms256m");
54+
elasticsearch.start();
55+
56+
httpHost = HttpHost.create(elasticsearch.getHttpHostAddress());
57+
client =
58+
RestClient.builder(httpHost)
59+
.setMaxRetryTimeoutMillis(Integer.MAX_VALUE)
60+
.setRequestConfigCallback(
61+
builder ->
62+
builder
63+
.setConnectTimeout(Integer.MAX_VALUE)
64+
.setSocketTimeout(Integer.MAX_VALUE))
65+
.build();
66+
67+
objectMapper = new ObjectMapper();
68+
}
69+
70+
@AfterAll
71+
static void cleanUp() {
72+
elasticsearch.stop();
73+
}
74+
75+
@Test
76+
@SuppressWarnings("rawtypes")
77+
void elasticsearchStatus() throws IOException {
78+
Response response = client.performRequest("GET", "_cluster/health");
79+
80+
Map result = objectMapper.readValue(response.getEntity().getContent(), Map.class);
81+
82+
// usually this test reports green status, but sometimes it is yellow
83+
Assertions.assertTrue(
84+
"green".equals(result.get("status")) || "yellow".equals(result.get("status")));
85+
86+
testing.waitAndAssertTraces(
87+
trace -> {
88+
trace.hasSpansSatisfyingExactly(
89+
span -> {
90+
span.hasName("GET")
91+
.hasKind(SpanKind.CLIENT)
92+
.hasNoParent()
93+
.hasAttributesSatisfyingExactly(
94+
equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"),
95+
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
96+
equalTo(SemanticAttributes.DB_STATEMENT, "GET _cluster/health"));
97+
},
98+
span -> {
99+
span.hasName("GET")
100+
.hasKind(SpanKind.CLIENT)
101+
.hasParent(trace.getSpan(0))
102+
.hasAttributesSatisfyingExactly(
103+
equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()),
104+
equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()),
105+
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
106+
equalTo(AttributeKey.stringKey("net.protocol.name"), "http"),
107+
equalTo(AttributeKey.stringKey("net.protocol.version"), "1.1"),
108+
equalTo(SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health"),
109+
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
110+
equalTo(
111+
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
112+
response.getEntity().getContentLength()));
113+
});
114+
});
115+
}
116+
117+
@Test
118+
@SuppressWarnings("rawtypes")
119+
void elasticsearchStatusAsync() throws Exception {
120+
Response[] requestResponse = {null};
121+
Exception[] exception = {null};
122+
CountDownLatch countDownLatch = new CountDownLatch(1);
123+
ResponseListener responseListener =
124+
new ResponseListener() {
125+
@Override
126+
public void onSuccess(Response response) {
127+
testing.runWithSpan(
128+
"callback",
129+
() -> {
130+
requestResponse[0] = response;
131+
countDownLatch.countDown();
132+
});
133+
}
134+
135+
@Override
136+
public void onFailure(Exception e) {
137+
testing.runWithSpan(
138+
"callback",
139+
() -> {
140+
exception[0] = e;
141+
countDownLatch.countDown();
142+
});
143+
}
144+
};
145+
146+
testing.runWithSpan(
147+
"parent",
148+
() -> {
149+
client.performRequestAsync("GET", "_cluster/health", responseListener);
150+
});
151+
countDownLatch.await();
152+
if (exception[0] != null) {
153+
throw exception[0];
154+
}
155+
Map result = objectMapper.readValue(requestResponse[0].getEntity().getContent(), Map.class);
156+
157+
// usually this test reports green status, but sometimes it is yellow
158+
Assertions.assertTrue(
159+
"green".equals(result.get("status")) || "yellow".equals(result.get("status")));
160+
161+
testing.waitAndAssertTraces(
162+
trace -> {
163+
trace.hasSpansSatisfyingExactly(
164+
span -> {
165+
span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent();
166+
},
167+
span -> {
168+
span.hasName("GET")
169+
.hasKind(SpanKind.CLIENT)
170+
.hasParent(trace.getSpan(0))
171+
.hasAttributesSatisfyingExactly(
172+
equalTo(SemanticAttributes.DB_SYSTEM, "elasticsearch"),
173+
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
174+
equalTo(SemanticAttributes.DB_STATEMENT, "GET _cluster/health"));
175+
},
176+
span -> {
177+
span.hasName("GET")
178+
.hasKind(SpanKind.CLIENT)
179+
.hasParent(trace.getSpan(1))
180+
.hasAttributesSatisfyingExactly(
181+
equalTo(SemanticAttributes.NET_PEER_NAME, httpHost.getHostName()),
182+
equalTo(SemanticAttributes.NET_PEER_PORT, httpHost.getPort()),
183+
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
184+
equalTo(AttributeKey.stringKey("net.protocol.name"), "http"),
185+
equalTo(AttributeKey.stringKey("net.protocol.version"), "1.1"),
186+
equalTo(SemanticAttributes.HTTP_URL, httpHost.toURI() + "/_cluster/health"),
187+
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
188+
equalTo(
189+
SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH,
190+
requestResponse[0].getEntity().getContentLength()));
191+
},
192+
span -> {
193+
span.hasName("callback").hasKind(SpanKind.INTERNAL).hasParent(trace.getSpan(0));
194+
});
195+
});
196+
}
197+
}

instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030

3131
testImplementation("org.apache.logging.log4j:log4j-core:2.11.0")
3232
testImplementation("org.apache.logging.log4j:log4j-api:2.11.0")
33+
testImplementation("com.fasterxml.jackson.core:jackson-databind")
3334

3435
testImplementation("org.testcontainers:elasticsearch")
3536
testLibrary("org.elasticsearch.client:elasticsearch-rest-client:6.4.0")

0 commit comments

Comments
 (0)