opentelemetry-java-instrume.../instrumentation/elasticsearch/transport-6.0/src/test/groovy/Elasticsearch6NodeClientTes...

279 lines
8.7 KiB
Groovy

import io.opentelemetry.auto.instrumentation.api.MoreTags
import io.opentelemetry.auto.instrumentation.api.SpanTypes
import io.opentelemetry.auto.instrumentation.api.Tags
import io.opentelemetry.auto.test.AgentTestRunner
import io.opentelemetry.auto.test.utils.PortUtils
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest
import org.elasticsearch.common.io.FileSystemUtils
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.index.IndexNotFoundException
import org.elasticsearch.node.InternalSettingsPreparer
import org.elasticsearch.node.Node
import org.elasticsearch.transport.Netty4Plugin
import spock.lang.Shared
import static io.opentelemetry.auto.test.utils.TraceUtils.runUnderTrace
import static io.opentelemetry.trace.Span.Kind.CLIENT
import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING
class Elasticsearch6NodeClientTest extends AgentTestRunner {
public static final long TIMEOUT = 10000; // 10 seconds
@Shared
int httpPort
@Shared
int tcpPort
@Shared
Node testNode
@Shared
File esWorkingDir
def client = testNode.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)
// Since we use listeners to close spans this should make our span closing deterministic which is good for tests
.put("thread_pool.listener.size", 1)
.put("http.port", httpPort)
.put("transport.tcp.port", tcpPort)
.put(CLUSTER_NAME_SETTING.getKey(), "test-cluster")
.build()
testNode = new Node(InternalSettingsPreparer.prepareEnvironment(settings, null), [Netty4Plugin])
testNode.start()
runUnderTrace("setup") {
// this may potentially create multiple requests and therefore multiple spans, so we wrap this call
// into a top level trace to get exactly one trace in the result.
testNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT)
}
TEST_WRITER.waitForTraces(1)
}
def cleanupSpec() {
testNode?.close()
if (esWorkingDir != null) {
FileSystemUtils.deleteSubDirectories(esWorkingDir.toPath())
esWorkingDir.delete()
}
}
def "test elasticsearch status"() {
setup:
def result = client.admin().cluster().health(new ClusterHealthRequest()).get()
def status = result.status
expect:
status.name() == "GREEN"
assertTraces(1) {
trace(0, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "ClusterHealthAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "ClusterHealthAction"
"elasticsearch.request" "ClusterHealthRequest"
}
}
}
}
}
def "test elasticsearch error"() {
when:
client.prepareGet(indexName, indexType, id).get()
then:
thrown IndexNotFoundException
and:
assertTraces(1) {
trace(0, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
errored true
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "GetAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "GetAction"
"elasticsearch.request" "GetRequest"
"elasticsearch.request.indices" indexName
errorTags IndexNotFoundException, "no such index"
}
}
}
}
where:
indexName = "invalid-index"
indexType = "test-type"
id = "1"
}
def "test elasticsearch get"() {
setup:
assert TEST_WRITER.traces == []
def indexResult = client.admin().indices().prepareCreate(indexName).get()
TEST_WRITER.waitForTraces(1)
expect:
indexResult.index() == indexName
TEST_WRITER.traces.size() == 1
when:
def emptyResult = client.prepareGet(indexName, indexType, id).get()
then:
!emptyResult.isExists()
emptyResult.id == id
emptyResult.type == indexType
emptyResult.index == indexName
when:
def createResult = client.prepareIndex(indexName, indexType, id).setSource([:]).get()
then:
createResult.id == id
createResult.type == indexType
createResult.index == indexName
createResult.status().status == 201
when:
def result = client.prepareGet(indexName, indexType, id).get()
then:
result.isExists()
result.id == id
result.type == indexType
result.index == indexName
and:
assertTraces(5) {
sortTraces {
// IndexAction and PutMappingAction run in separate threads and so their order is not always the same
if (traces[2][0].attributes[MoreTags.RESOURCE_NAME].stringValue == "IndexAction") {
def tmp = traces[2]
traces[2] = traces[3]
traces[3] = tmp
}
}
trace(0, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "CreateIndexAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "CreateIndexAction"
"elasticsearch.request" "CreateIndexRequest"
"elasticsearch.request.indices" indexName
}
}
}
trace(1, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "GetAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "GetAction"
"elasticsearch.request" "GetRequest"
"elasticsearch.request.indices" indexName
"elasticsearch.type" indexType
"elasticsearch.id" "1"
"elasticsearch.version"(-1)
}
}
}
trace(2, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "PutMappingAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "PutMappingAction"
"elasticsearch.request" "PutMappingRequest"
}
}
}
trace(3, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "IndexAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "IndexAction"
"elasticsearch.request" "IndexRequest"
"elasticsearch.request.indices" indexName
"elasticsearch.request.write.type" indexType
"elasticsearch.request.write.version"(-3)
"elasticsearch.response.status" 201
"elasticsearch.shard.replication.total" 2
"elasticsearch.shard.replication.successful" 1
"elasticsearch.shard.replication.failed" 0
}
}
}
trace(4, 1) {
span(0) {
operationName "elasticsearch.query"
spanKind CLIENT
tags {
"$MoreTags.SERVICE_NAME" "elasticsearch"
"$MoreTags.RESOURCE_NAME" "GetAction"
"$MoreTags.SPAN_TYPE" SpanTypes.ELASTICSEARCH
"$Tags.COMPONENT" "elasticsearch-java"
"$Tags.DB_TYPE" "elasticsearch"
"elasticsearch.action" "GetAction"
"elasticsearch.request" "GetRequest"
"elasticsearch.request.indices" indexName
"elasticsearch.type" indexType
"elasticsearch.id" "1"
"elasticsearch.version" 1
}
}
}
}
cleanup:
client.admin().indices().prepareDelete(indexName).get()
where:
indexName = "test-index"
indexType = "test-type"
id = "1"
}
}