Use testcontainers for ES rest tests and some other ES dependency cleanup
MODIFY
instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/elasticsearch-rest-5.0-javaagent.gradle
instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/elasticsearch-rest-5.0-javaagent.gradle
@@ -16,10 +16,6 @@ muzzle {
}
}
-testSets {
- latestDepTest
-}
-
dependencies {
compileOnly "org.elasticsearch.client:rest:5.0.0"
@@ -31,15 +27,12 @@ dependencies {
testImplementation "org.apache.logging.log4j:log4j-core:2.11.0"
testImplementation "org.apache.logging.log4j:log4j-api:2.11.0"
- testImplementation "org.elasticsearch.client:rest:5.0.0"
- testImplementation "org.elasticsearch:elasticsearch:5.0.0"
- testImplementation "org.elasticsearch.plugin:transport-netty3-client:5.0.0"
+ testImplementation "org.testcontainers:elasticsearch:${versions["org.testcontainers"]}"
+ testLibrary "org.elasticsearch.client:rest:5.0.0"
- latestDepTestImplementation "org.elasticsearch.client:elasticsearch-rest-client:6.3.+"
- latestDepTestImplementation "org.elasticsearch:elasticsearch:6.3.+"
- latestDepTestImplementation "org.elasticsearch.plugin:transport-netty4-client:6.3.+"
+ latestDepTestLibrary "org.elasticsearch.client:elasticsearch-rest-client:6.3.+"
}
-configurations.latestDepTestImplementation {
- exclude group: "org.elasticsearch.client", module: "rest"
+tasks.withType(Test).configureEach {
+ systemProperty "testLatestDeps", testLatestDeps
}

DELETE
instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/latestDepTest/groovy/Elasticsearch6RestClientTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import static io.opentelemetry.api.trace.SpanKind.CLIENT
-
-import groovy.json.JsonSlurper
-import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
-import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
-import org.apache.http.HttpHost
-import org.apache.http.client.config.RequestConfig
-import org.apache.http.util.EntityUtils
-import org.elasticsearch.client.Response
-import org.elasticsearch.client.RestClient
-import org.elasticsearch.client.RestClientBuilder
-import org.elasticsearch.common.io.FileSystemUtils
-import org.elasticsearch.common.settings.Settings
-import org.elasticsearch.common.transport.TransportAddress
-import org.elasticsearch.http.HttpServerTransport
-import org.elasticsearch.node.InternalSettingsPreparer
-import org.elasticsearch.node.Node
-import org.elasticsearch.transport.Netty4Plugin
-import spock.lang.Shared
-
-class Elasticsearch6RestClientTest extends AgentInstrumentationSpecification {
- @Shared
- TransportAddress httpTransportAddress
- @Shared
- Node testNode
- @Shared
- File esWorkingDir
- @Shared
- String clusterName = UUID.randomUUID().toString()
-
- @Shared
- RestClient client
-
- def setupSpec() {
-
- esWorkingDir = File.createTempDir("test-es-working-dir-", "")
- esWorkingDir.deleteOnExit()
- println "ES work dir: $esWorkingDir"
-
- def settings = Settings.builder()
- .put("path.home", esWorkingDir.path)
- .put("cluster.name", clusterName)
- .build()
- testNode = new Node(InternalSettingsPreparer.prepareEnvironment(settings, null), [Netty4Plugin])
- testNode.start()
- httpTransportAddress = testNode.injector().getInstance(HttpServerTransport).boundAddress().publishAddress()
-
- client = RestClient.builder(new HttpHost(httpTransportAddress.address, httpTransportAddress.port))
- .setMaxRetryTimeoutMillis(Integer.MAX_VALUE)
- .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
- @Override
- RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
- return builder.setConnectTimeout(Integer.MAX_VALUE).setSocketTimeout(Integer.MAX_VALUE)
- }
- })
- .build()
-
- }
-
- def cleanupSpec() {
- testNode?.close()
- if (esWorkingDir != null) {
- FileSystemUtils.deleteSubDirectories(esWorkingDir.toPath())
- esWorkingDir.delete()
- }
- }
-
- def "test elasticsearch status"() {
- setup:
- Response response = client.performRequest("GET", "_cluster/health")
-
- Map result = new JsonSlurper().parseText(EntityUtils.toString(response.entity))
-
- expect:
- result.status == "green"
-
- assertTraces(1) {
- trace(0, 1) {
- span(0) {
- name "GET _cluster/health"
- kind CLIENT
- hasNoParent()
- attributes {
- "${SemanticAttributes.DB_SYSTEM.key}" "elasticsearch"
- "${SemanticAttributes.DB_OPERATION.key}" "GET _cluster/health"
- "${SemanticAttributes.NET_PEER_NAME.key}" httpTransportAddress.address
- "${SemanticAttributes.NET_PEER_PORT.key}" httpTransportAddress.port
- }
- }
- }
- }
- }
-
- String expectedOperationName(String method) {
- return method != null ? "HTTP $method" : "HTTP request"
- }
-}

MODIFY
instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/groovy/Elasticsearch5RestClientTest.groovy
instrumentation/elasticsearch/elasticsearch-rest-5.0/javaagent/src/test/groovy/Elasticsearch5RestClientTest.groovy
*/
import static io.opentelemetry.api.trace.SpanKind.CLIENT
-import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
import groovy.json.JsonSlurper
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
@@ -15,46 +14,31 @@ import org.apache.http.util.EntityUtils
import org.elasticsearch.client.Response
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
-import org.elasticsearch.common.io.FileSystemUtils
-import org.elasticsearch.common.settings.Settings
-import org.elasticsearch.common.transport.TransportAddress
-import org.elasticsearch.env.Environment
-import org.elasticsearch.http.HttpServerTransport
-import org.elasticsearch.node.Node
-import org.elasticsearch.node.internal.InternalSettingsPreparer
-import org.elasticsearch.transport.Netty3Plugin
+import org.testcontainers.elasticsearch.ElasticsearchContainer
import spock.lang.Shared
class Elasticsearch5RestClientTest extends AgentInstrumentationSpecification {
@Shared
- TransportAddress httpTransportAddress
- @Shared
- Node testNode
- @Shared
- File esWorkingDir
+ ElasticsearchContainer elasticsearch
+
@Shared
- String clusterName = UUID.randomUUID().toString()
+ HttpHost httpHost
@Shared
static RestClient client
def setupSpec() {
+ if (!Boolean.getBoolean("testLatestDeps")) {
+ elasticsearch = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:5.6.16")
+ .withEnv("xpack.ml.enabled", "false")
+ .withEnv("xpack.security.enabled", "false")
+ } else {
+ elasticsearch = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.16")
+ }
+ elasticsearch.start()
- esWorkingDir = File.createTempDir("test-es-working-dir-", "")
- esWorkingDir.deleteOnExit()
- println "ES work dir: $esWorkingDir"
-
- def settings = Settings.builder()
- .put("path.home", esWorkingDir.path)
- .put("transport.type", "netty3")
- .put("http.type", "netty3")
- .put(CLUSTER_NAME_SETTING.getKey(), clusterName)
- .build()
- testNode = new Node(new Environment(InternalSettingsPreparer.prepareSettings(settings)), [Netty3Plugin])
- testNode.start()
- httpTransportAddress = testNode.injector().getInstance(HttpServerTransport).boundAddress().publishAddress()
-
- client = RestClient.builder(new HttpHost(httpTransportAddress.address, httpTransportAddress.port))
+ httpHost = HttpHost.create(elasticsearch.getHttpHostAddress())
+ client = RestClient.builder(httpHost)
.setMaxRetryTimeoutMillis(Integer.MAX_VALUE)
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
@@ -63,15 +47,10 @@ class Elasticsearch5RestClientTest extends AgentInstrumentationSpecification {
}
})
.build()
-
}
def cleanupSpec() {
- testNode?.close()
- if (esWorkingDir != null) {
- FileSystemUtils.deleteSubDirectories(esWorkingDir.toPath())
- esWorkingDir.delete()
- }
+ elasticsearch.stop()
}
def "test elasticsearch status"() {
@@ -92,8 +71,8 @@ class Elasticsearch5RestClientTest extends AgentInstrumentationSpecification {
attributes {
"${SemanticAttributes.DB_SYSTEM.key}" "elasticsearch"
"${SemanticAttributes.DB_OPERATION.key}" "GET _cluster/health"
- "${SemanticAttributes.NET_PEER_NAME.key}" httpTransportAddress.address
- "${SemanticAttributes.NET_PEER_PORT.key}" httpTransportAddress.port
+ "${SemanticAttributes.NET_PEER_NAME.key}" httpHost.hostName
+ "${SemanticAttributes.NET_PEER_PORT.key}" httpHost.port
}
}
}

MODIFY
instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/elasticsearch-rest-6.4-javaagent.gradle
instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/elasticsearch-rest-6.4-javaagent.gradle
@@ -29,11 +29,8 @@ dependencies {
testImplementation "org.apache.logging.log4j:log4j-core:2.11.0"
testImplementation "org.apache.logging.log4j:log4j-api:2.11.0"
+ testImplementation "org.testcontainers:elasticsearch:${versions["org.testcontainers"]}"
testLibrary "org.elasticsearch.client:elasticsearch-rest-client:6.4.0"
- testLibrary "org.elasticsearch:elasticsearch:6.4.0"
- testLibrary "org.elasticsearch.plugin:transport-netty4-client:6.4.0"
latestDepTestLibrary "org.elasticsearch.client:elasticsearch-rest-client:6.+"
- latestDepTestLibrary "org.elasticsearch:elasticsearch:6.+"
- latestDepTestLibrary "org.elasticsearch.plugin:transport-netty4-client:6.+"
}

MODIFY
instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/groovy/Elasticsearch6RestClientTest.groovy
instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/groovy/Elasticsearch6RestClientTest.groovy
@@ -14,41 +14,25 @@ import org.apache.http.util.EntityUtils
import org.elasticsearch.client.Response
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
-import org.elasticsearch.common.io.FileSystemUtils
-import org.elasticsearch.common.settings.Settings
-import org.elasticsearch.common.transport.TransportAddress
-import org.elasticsearch.http.HttpServerTransport
-import org.elasticsearch.node.Node
+import org.testcontainers.elasticsearch.ElasticsearchContainer
import spock.lang.Shared
class Elasticsearch6RestClientTest extends AgentInstrumentationSpecification {
@Shared
- TransportAddress httpTransportAddress
- @Shared
- Node testNode
- @Shared
- File esWorkingDir
+ ElasticsearchContainer elasticsearch
+
@Shared
- String clusterName = UUID.randomUUID().toString()
+ HttpHost httpHost
@Shared
RestClient client
def setupSpec() {
+ elasticsearch = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.16")
+ elasticsearch.start()
- esWorkingDir = File.createTempDir("test-es-working-dir-", "")
- esWorkingDir.deleteOnExit()
- println "ES work dir: $esWorkingDir"
-
- def settings = Settings.builder()
- .put("path.home", esWorkingDir.path)
- .put("cluster.name", clusterName)
- .build()
- testNode = NodeFactory.newNode(settings)
- testNode.start()
- httpTransportAddress = testNode.injector().getInstance(HttpServerTransport).boundAddress().publishAddress()
-
- client = RestClient.builder(new HttpHost(httpTransportAddress.address, httpTransportAddress.port))
+ httpHost = HttpHost.create(elasticsearch.getHttpHostAddress())
+ client = RestClient.builder(httpHost)
.setMaxRetryTimeoutMillis(Integer.MAX_VALUE)
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
@@ -61,11 +45,7 @@ class Elasticsearch6RestClientTest extends AgentInstrumentationSpecification {
}
def cleanupSpec() {
- testNode?.close()
- if (esWorkingDir != null) {
- FileSystemUtils.deleteSubDirectories(esWorkingDir.toPath())
- esWorkingDir.delete()
- }
+ elasticsearch.stop()
}
def "test elasticsearch status"() {
@@ -86,8 +66,8 @@ class Elasticsearch6RestClientTest extends AgentInstrumentationSpecification {
attributes {
"${SemanticAttributes.DB_SYSTEM.key}" "elasticsearch"
"${SemanticAttributes.DB_OPERATION.key}" "GET _cluster/health"
- "${SemanticAttributes.NET_PEER_NAME.key}" httpTransportAddress.address
- "${SemanticAttributes.NET_PEER_PORT.key}" httpTransportAddress.port
+ "${SemanticAttributes.NET_PEER_NAME.key}" httpHost.hostName
+ "${SemanticAttributes.NET_PEER_PORT.key}" httpHost.port
}
}
}

DELETE
instrumentation/elasticsearch/elasticsearch-rest-6.4/javaagent/src/test/groovy/NodeFactory.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import org.elasticsearch.common.settings.Settings
-import org.elasticsearch.node.InternalSettingsPreparer
-import org.elasticsearch.node.Node
-import org.elasticsearch.transport.Netty4Plugin
-
-class NodeFactory {
- static Node newNode(Settings settings) {
- def version = org.elasticsearch.Version.CURRENT
- if (version.major == 6 && version.minor >= 5) {
- return new NodeV65(settings)
- }
- return new NodeV6(settings)
- }
-
- static class NodeV6 extends Node {
- NodeV6(Settings settings) {
- super(InternalSettingsPreparer.prepareEnvironment(settings, null), [Netty4Plugin])
- }
-
- protected void registerDerivedNodeNameWithLogger(String s) {
- }
- }
-
- static class NodeV65 extends Node {
- NodeV65(Settings settings) {
- super(InternalSettingsPreparer.prepareEnvironment(settings, null), [Netty4Plugin], true)
- }
-
- protected void registerDerivedNodeNameWithLogger(String s) {
- }
- }
-}

MODIFY
instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/elasticsearch-rest-7.0-javaagent.gradle
instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/elasticsearch-rest-7.0-javaagent.gradle
@@ -29,6 +29,5 @@ dependencies {
testImplementation "org.apache.logging.log4j:log4j-core:2.11.0"
testImplementation "org.apache.logging.log4j:log4j-api:2.11.0"
- testLibrary "org.elasticsearch:elasticsearch:7.0.0"
- testLibrary "org.elasticsearch.plugin:transport-netty4-client:7.0.0"
+ testImplementation "org.testcontainers:elasticsearch:${versions["org.testcontainers"]}"
}

MODIFY
instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/groovy/Elasticsearch7RestClientTest.groovy
instrumentation/elasticsearch/elasticsearch-rest-7.0/javaagent/src/test/groovy/Elasticsearch7RestClientTest.groovy
@@ -17,43 +17,25 @@ import org.elasticsearch.client.Response
import org.elasticsearch.client.ResponseListener
import org.elasticsearch.client.RestClient
import org.elasticsearch.client.RestClientBuilder
-import org.elasticsearch.common.io.FileSystemUtils
-import org.elasticsearch.common.settings.Settings
-import org.elasticsearch.common.transport.TransportAddress
-import org.elasticsearch.http.HttpServerTransport
-import org.elasticsearch.node.InternalSettingsPreparer
-import org.elasticsearch.node.Node
-import org.elasticsearch.transport.Netty4Plugin
+import org.testcontainers.elasticsearch.ElasticsearchContainer
import spock.lang.Shared
class Elasticsearch7RestClientTest extends AgentInstrumentationSpecification {
@Shared
- TransportAddress httpTransportAddress
- @Shared
- Node testNode
- @Shared
- File esWorkingDir
+ ElasticsearchContainer elasticsearch
+
@Shared
- String clusterName = UUID.randomUUID().toString()
+ HttpHost httpHost
@Shared
RestClient client
def setupSpec() {
+ elasticsearch = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:7.10.2")
+ elasticsearch.start()
- esWorkingDir = File.createTempDir("test-es-working-dir-", "")
- esWorkingDir.deleteOnExit()
- println "ES work dir: $esWorkingDir"
-
- def settings = Settings.builder()
- .put("path.home", esWorkingDir.path)
- .put("cluster.name", clusterName)
- .build()
- testNode = new Node(InternalSettingsPreparer.prepareEnvironment(settings, Collections.emptyMap(), null, { "default node name" }), [Netty4Plugin], true)
- testNode.start()
- httpTransportAddress = testNode.injector().getInstance(HttpServerTransport).boundAddress().publishAddress()
-
- client = RestClient.builder(new HttpHost(httpTransportAddress.address, httpTransportAddress.port))
+ httpHost = HttpHost.create(elasticsearch.getHttpHostAddress())
+ client = RestClient.builder(httpHost)
.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
@@ -61,15 +43,10 @@ class Elasticsearch7RestClientTest extends AgentInstrumentationSpecification {
}
})
.build()
-
}
def cleanupSpec() {
- testNode?.close()
- if (esWorkingDir != null) {
- FileSystemUtils.deleteSubDirectories(esWorkingDir.toPath())
- esWorkingDir.delete()
- }
+ elasticsearch.stop()
}
def "test elasticsearch status"() {
@@ -90,8 +67,8 @@ class Elasticsearch7RestClientTest extends AgentInstrumentationSpecification {
attributes {
"${SemanticAttributes.DB_SYSTEM.key}" "elasticsearch"
"${SemanticAttributes.DB_OPERATION.key}" "GET _cluster/health"
- "${SemanticAttributes.NET_PEER_NAME.key}" httpTransportAddress.address
- "${SemanticAttributes.NET_PEER_PORT.key}" httpTransportAddress.port
+ "${SemanticAttributes.NET_PEER_NAME.key}" httpHost.hostName
+ "${SemanticAttributes.NET_PEER_PORT.key}" httpHost.port
}
}
}
@@ -136,8 +113,8 @@ class Elasticsearch7RestClientTest extends AgentInstrumentationSpecification {
attributes {
"${SemanticAttributes.DB_SYSTEM.key}" "elasticsearch"
"${SemanticAttributes.DB_OPERATION.key}" "GET _cluster/health"
- "${SemanticAttributes.NET_PEER_NAME.key}" httpTransportAddress.address
- "${SemanticAttributes.NET_PEER_PORT.key}" httpTransportAddress.port
+ "${SemanticAttributes.NET_PEER_NAME.key}" httpHost.hostName
+ "${SemanticAttributes.NET_PEER_PORT.key}" httpHost.port
}
}
}

MODIFY
instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/elasticsearch-transport-5.3-javaagent.gradle
instrumentation/elasticsearch/elasticsearch-transport-5.3/javaagent/elasticsearch-transport-5.3-javaagent.gradle
@@ -22,7 +22,13 @@ muzzle {
}
dependencies {
- library "org.elasticsearch.client:transport:5.3.0"
+ compileOnly("org.elasticsearch.client:transport:5.3.0") {
+ transitive = false
+ }
+ compileOnly("org.elasticsearch:elasticsearch:5.3.0") {
+ // We don't need all its transitive dependencies when compiling and run tests against 5.5.0
+ transitive = false
+ }
implementation project(':instrumentation:elasticsearch:elasticsearch-transport-common:library')
@@ -33,9 +39,10 @@ dependencies {
testImplementation "org.apache.logging.log4j:log4j-core:2.11.0"
testImplementation "org.apache.logging.log4j:log4j-api:2.11.0"
+ // Unfortunately spring-data-elasticsearch requires 5.5.0
+ testLibrary "org.elasticsearch.client:transport:5.5.0"
testLibrary "org.elasticsearch.plugin:transport-netty3-client:5.3.0"
- // Unfortunately this will bump the transport version up to 5.5.0.
testLibrary "org.springframework.data:spring-data-elasticsearch:3.0.0.RELEASE"
latestDepTestLibrary "org.elasticsearch.plugin:transport-netty3-client:5.+"

MODIFY
instrumentation/elasticsearch/elasticsearch-transport-common/library/elasticsearch-transport-common-library.gradle
instrumentation/elasticsearch/elasticsearch-transport-common/library/elasticsearch-transport-common-library.gradle
apply from: "$rootDir/gradle/instrumentation-library.gradle"
-dependencies {
- compileOnly "org.elasticsearch:elasticsearch:2.0.0"
-}
+// No dependencies, elasticsearch library not actually used here.
\ No newline at end of file

Use testcontainers for ES rest tests and some other ES dependency cleanup (#3119)