opentelemetry-java-instrume.../instrumentation/elasticsearch/elasticsearch-rest-6.4/src/test/groovy/Elasticsearch6RestClientTes...

127 lines
4.1 KiB
Groovy

/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import groovy.json.JsonSlurper
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpClientDecorator
import io.opentelemetry.auto.instrumentation.api.MoreTags
import io.opentelemetry.auto.instrumentation.api.Tags
import io.opentelemetry.auto.test.AgentTestRunner
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
import static io.opentelemetry.trace.Span.Kind.CLIENT
import static io.opentelemetry.trace.Span.Kind.INTERNAL
class Elasticsearch6RestClientTest extends AgentTestRunner {
@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, 2) {
span(0) {
operationName "GET _cluster/health"
spanKind INTERNAL
parent()
tags {
"$MoreTags.NET_PEER_NAME" httpTransportAddress.address
"$MoreTags.NET_PEER_PORT" httpTransportAddress.port
"$Tags.HTTP_URL" "_cluster/health"
"$Tags.HTTP_METHOD" "GET"
"$Tags.DB_TYPE" "elasticsearch"
}
}
span(1) {
operationName expectedOperationName("GET")
spanKind CLIENT
childOf span(0)
tags {
"$Tags.HTTP_URL" "_cluster/health"
"$Tags.HTTP_METHOD" "GET"
"$Tags.HTTP_STATUS" 200
}
}
}
}
}
String expectedOperationName(String method) {
return method != null ? "HTTP $method" : HttpClientDecorator.DEFAULT_SPAN_NAME
}
}