opentelemetry-java-instrume.../dd-java-agent/instrumentation/elasticsearch/rest-5/src/test/groovy/Elasticsearch5RestClientTes...

123 lines
3.8 KiB
Groovy

import com.anotherchrisberry.spock.extensions.retry.RetryOnFailure
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.utils.PortUtils
import datadog.trace.api.DDSpanTypes
import datadog.trace.instrumentation.api.Tags
import groovy.json.JsonSlurper
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.env.Environment
import org.elasticsearch.node.Node
import org.elasticsearch.node.internal.InternalSettingsPreparer
import org.elasticsearch.transport.Netty3Plugin
import spock.lang.Shared
import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
@RetryOnFailure(times = 3, delaySeconds = 1)
class Elasticsearch5RestClientTest extends AgentTestRunner {
@Shared
int httpPort
@Shared
int tcpPort
@Shared
Node testNode
@Shared
File esWorkingDir
@Shared
static RestClient client
def setupSpec() {
httpPort = PortUtils.randomOpenPort()
tcpPort = PortUtils.randomOpenPort()
esWorkingDir = File.createTempDir("test-es-working-dir-", "")
esWorkingDir.deleteOnExit()
println "ES work dir: $esWorkingDir"
def settings = Settings.builder()
.put("path.home", esWorkingDir.path)
.put("http.port", httpPort)
.put("transport.tcp.port", tcpPort)
.put("transport.type", "netty3")
.put("http.type", "netty3")
.put(CLUSTER_NAME_SETTING.getKey(), "test-cluster")
.build()
testNode = new Node(new Environment(InternalSettingsPreparer.prepareSettings(settings)), [Netty3Plugin])
testNode.start()
client = RestClient.builder(new HttpHost("localhost", httpPort))
.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, 2) {
span(0) {
serviceName "elasticsearch"
resourceName "GET _cluster/health"
operationName "elasticsearch.rest.query"
spanType DDSpanTypes.ELASTICSEARCH
parent()
tags {
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_METHOD" "GET"
"$Tags.HTTP_URL" "_cluster/health"
"$Tags.PEER_HOSTNAME" "localhost"
"$Tags.PEER_PORT" httpPort
defaultTags()
}
}
span(1) {
serviceName "elasticsearch"
resourceName "GET _cluster/health"
operationName "http.request"
spanType DDSpanTypes.HTTP_CLIENT
childOf span(0)
tags {
"$Tags.COMPONENT" "apache-httpasyncclient"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_METHOD" "GET"
"$Tags.HTTP_URL" "_cluster/health"
"$Tags.HTTP_STATUS" 200
defaultTags()
}
}
}
}
}
}