94 lines
2.7 KiB
Groovy
94 lines
2.7 KiB
Groovy
import datadog.trace.agent.test.base.HttpClientTest
|
|
import datadog.trace.instrumentation.netty40.client.NettyHttpClientDecorator
|
|
import io.opentracing.tag.Tags
|
|
import org.asynchttpclient.AsyncHttpClient
|
|
import org.asynchttpclient.DefaultAsyncHttpClientConfig
|
|
import spock.lang.Shared
|
|
|
|
import java.util.concurrent.ExecutionException
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import static datadog.trace.agent.test.utils.PortUtils.UNUSABLE_PORT
|
|
import static datadog.trace.agent.test.utils.TraceUtils.runUnderTrace
|
|
import static org.asynchttpclient.Dsl.asyncHttpClient
|
|
|
|
class Netty40ClientTest extends HttpClientTest<NettyHttpClientDecorator> {
|
|
|
|
@Shared
|
|
def clientConfig = DefaultAsyncHttpClientConfig.Builder.newInstance().setRequestTimeout(TimeUnit.SECONDS.toMillis(10).toInteger())
|
|
@Shared
|
|
AsyncHttpClient asyncHttpClient = asyncHttpClient(clientConfig)
|
|
|
|
@Override
|
|
int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) {
|
|
def methodName = "prepare" + method.toLowerCase().capitalize()
|
|
def requestBuilder = asyncHttpClient."$methodName"(uri.toString())
|
|
headers.each { requestBuilder.setHeader(it.key, it.value) }
|
|
def response = requestBuilder.execute().get()
|
|
callback?.call()
|
|
return response.statusCode
|
|
}
|
|
|
|
@Override
|
|
NettyHttpClientDecorator decorator() {
|
|
return NettyHttpClientDecorator.DECORATE
|
|
}
|
|
|
|
@Override
|
|
String expectedOperationName() {
|
|
return "netty.client.request"
|
|
}
|
|
|
|
@Override
|
|
boolean testRedirects() {
|
|
false
|
|
}
|
|
|
|
@Override
|
|
boolean testConnectionFailure() {
|
|
false
|
|
}
|
|
|
|
def "connection error (unopened port)"() {
|
|
given:
|
|
def uri = new URI("http://localhost:$UNUSABLE_PORT/")
|
|
|
|
when:
|
|
runUnderTrace("parent") {
|
|
doRequest(method, uri)
|
|
}
|
|
|
|
then:
|
|
def ex = thrown(Exception)
|
|
def thrownException = ex instanceof ExecutionException ? ex.cause : ex
|
|
|
|
and:
|
|
assertTraces(1) {
|
|
trace(0, 2) {
|
|
parentSpan(it, 0, thrownException)
|
|
|
|
span(1) {
|
|
operationName "netty.connect"
|
|
resourceName "netty.connect"
|
|
childOf span(0)
|
|
errored true
|
|
tags {
|
|
"$Tags.COMPONENT.key" "netty"
|
|
Class errorClass = ConnectException
|
|
try {
|
|
errorClass = Class.forName('io.netty.channel.AbstractChannel$AnnotatedConnectException')
|
|
} catch (ClassNotFoundException e) {
|
|
// Older versions use 'java.net.ConnectException' and do not have 'io.netty.channel.AbstractChannel$AnnotatedConnectException'
|
|
}
|
|
errorTags errorClass, "Connection refused: localhost/127.0.0.1:$UNUSABLE_PORT"
|
|
defaultTags()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
where:
|
|
method = "GET"
|
|
}
|
|
}
|