Migrate ApacheHttpClientTest to use span assertion api
MODIFY
dd-java-agent/instrumentation/apache-httpclient-4.3/src/test/groovy/ApacheHttpClientTest.groovy
dd-java-agent/instrumentation/apache-httpclient-4.3/src/test/groovy/ApacheHttpClientTest.groovy
-import datadog.opentracing.DDSpan
import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.agent.test.RatpackUtils
import datadog.trace.api.DDSpanTypes
+import datadog.trace.api.DDTags
import io.opentracing.tag.Tags
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
@@ -10,6 +10,7 @@ import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.message.BasicHeader
import spock.lang.Shared
+import static datadog.trace.agent.test.ListWriterAssert.assertTraces
import static datadog.trace.agent.test.TestUtils.runUnderTrace
import static ratpack.groovy.test.embed.GroovyEmbeddedApp.ratpack
@@ -26,6 +27,8 @@ class ApacheHttpClientTest extends AgentTestRunner {
}
}
}
+ @Shared
+ int port = server.getAddress().port
def "trace request with propagation"() {
setup:
@@ -44,33 +47,60 @@ class ApacheHttpClientTest extends AgentTestRunner {
expect:
// one trace on the server, one trace on the client
- TEST_WRITER.size() == 2
- final List<DDSpan> serverTrace = TEST_WRITER.get(0)
- serverTrace.size() == 1
-
- final List<DDSpan> clientTrace = TEST_WRITER.get(1)
- clientTrace.size() == 3
- clientTrace.get(0).getOperationName() == "someTrace"
- // our instrumentation makes 2 spans for apache-httpclient
- final DDSpan localSpan = clientTrace.get(1)
- localSpan.getType() == null
- localSpan.getTags()[Tags.COMPONENT.getKey()] == "apache-httpclient"
- localSpan.getOperationName() == "apache.http"
-
- final DDSpan clientSpan = clientTrace.get(2)
- clientSpan.getOperationName() == "http.request"
- clientSpan.getType() == DDSpanTypes.HTTP_CLIENT
- clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
- clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
- clientSpan.getTags()[Tags.HTTP_URL.getKey()] == server.getAddress().toString()
- clientSpan.getTags()[Tags.PEER_HOSTNAME.getKey()] == "localhost"
- clientSpan.getTags()[Tags.PEER_PORT.getKey()] == server.getAddress().port
- clientSpan.getTags()[Tags.SPAN_KIND.getKey()] == Tags.SPAN_KIND_CLIENT
-
- // client trace propagates to server
- clientSpan.getTraceId() == serverTrace.get(0).getTraceId()
- // server span is parented under http client
- clientSpan.getSpanId() == serverTrace.get(0).getParentId()
+ assertTraces(TEST_WRITER, 2) {
+ trace(0, 1) {
+ span(0) {
+ childOf TEST_WRITER[1][2]
+ serviceName "unnamed-java-app"
+ operationName "test-http-server"
+ resourceName "test-http-server"
+ errored false
+ tags {
+ defaultTags()
+ }
+ }
+ }
+ trace(1, 3) {
+ span(0) {
+ parent()
+ serviceName "unnamed-java-app"
+ operationName "someTrace"
+ resourceName "someTrace"
+ errored false
+ tags {
+ defaultTags()
+ }
+ }
+ span(1) {
+ childOf span(0)
+ serviceName "unnamed-java-app"
+ operationName "apache.http"
+ resourceName "apache.http"
+ errored false
+ tags {
+ defaultTags()
+ "$Tags.COMPONENT.key" "apache-httpclient"
+ }
+ }
+ span(2) {
+ childOf span(1)
+ serviceName "unnamed-java-app"
+ operationName "http.request"
+ resourceName "GET /"
+ errored false
+ tags {
+ defaultTags()
+ "$Tags.HTTP_STATUS.key" 200
+ "$Tags.HTTP_URL.key" "http://localhost:$port/"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_PORT.key" server.getAddress().port
+ "$Tags.HTTP_METHOD.key" "GET"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
+ }
+ }
+ }
+ }
}
def "trace request without propagation"() {
@@ -91,22 +121,47 @@ class ApacheHttpClientTest extends AgentTestRunner {
}
expect:
// only one trace (client).
- TEST_WRITER.size() == 1
- final List<DDSpan> clientTrace = TEST_WRITER.get(0)
- clientTrace.size() == 3
- clientTrace.get(0).getOperationName() == "someTrace"
- // our instrumentation makes 2 spans for apache-httpclient
- final DDSpan localSpan = clientTrace.get(1)
- localSpan.getTags()[Tags.COMPONENT.getKey()] == "apache-httpclient"
- localSpan.getOperationName() == "apache.http"
-
- final DDSpan clientSpan = clientTrace.get(2)
- clientSpan.getOperationName() == "http.request"
- clientSpan.getTags()[Tags.HTTP_METHOD.getKey()] == "GET"
- clientSpan.getTags()[Tags.HTTP_STATUS.getKey()] == 200
- clientSpan.getTags()[Tags.HTTP_URL.getKey()] == server.getAddress().toString()
- clientSpan.getTags()[Tags.PEER_HOSTNAME.getKey()] == "localhost"
- clientSpan.getTags()[Tags.PEER_PORT.getKey()] == server.getAddress().port
- clientSpan.getTags()[Tags.SPAN_KIND.getKey()] == Tags.SPAN_KIND_CLIENT
+ assertTraces(TEST_WRITER, 1) {
+ trace(0, 3) {
+ span(0) {
+ parent()
+ serviceName "unnamed-java-app"
+ operationName "someTrace"
+ resourceName "someTrace"
+ errored false
+ tags {
+ defaultTags()
+ }
+ }
+ span(1) {
+ childOf span(0)
+ serviceName "unnamed-java-app"
+ operationName "apache.http"
+ resourceName "apache.http"
+ errored false
+ tags {
+ defaultTags()
+ "$Tags.COMPONENT.key" "apache-httpclient"
+ }
+ }
+ span(2) {
+ childOf span(1)
+ serviceName "unnamed-java-app"
+ operationName "http.request"
+ resourceName "GET /"
+ errored false
+ tags {
+ defaultTags()
+ "$Tags.HTTP_STATUS.key" 200
+ "$Tags.HTTP_URL.key" "http://localhost:$port/"
+ "$Tags.PEER_HOSTNAME.key" "localhost"
+ "$Tags.PEER_PORT.key" server.getAddress().port
+ "$Tags.HTTP_METHOD.key" "GET"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$DDTags.SPAN_TYPE" DDSpanTypes.HTTP_CLIENT
+ }
+ }
+ }
+ }
}
}

Migrate ApacheHttpClientTest to use span assertion api