Merge pull request #449 from DataDog/mar-kolya/add-distributed-tracing-tests

Add some tests for distributed tracing in Tomcat and Jetty
This commit is contained in:
Nikolay Martynov 2018-08-20 20:30:27 -04:00 committed by GitHub
commit 520676538c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 35 deletions

View File

@ -31,7 +31,7 @@ class HttpUrlConnectionTest extends AgentTestRunner {
} }
} }
def "trace request with propagation"() { def "trace request with propagation (useCaches: #useCaches)"() {
setup: setup:
runUnderTrace("someTrace") { runUnderTrace("someTrace") {
HttpURLConnection connection = server.address.toURL().openConnection() HttpURLConnection connection = server.address.toURL().openConnection()
@ -107,7 +107,7 @@ class HttpUrlConnectionTest extends AgentTestRunner {
useCaches << [false, true] useCaches << [false, true]
} }
def "trace request without propagation"() { def "trace request without propagation (useCaches: #useCaches)"() {
setup: setup:
runUnderTrace("someTrace") { runUnderTrace("someTrace") {
HttpURLConnection connection = server.address.toURL().openConnection() HttpURLConnection connection = server.address.toURL().openConnection()

View File

@ -60,11 +60,15 @@ class JettyServlet3Test extends AgentTestRunner {
jettyServer.destroy() jettyServer.destroy()
} }
def "test #path servlet call"() { def "test #path servlet call (auth: #auth, distributed tracing: #distributedTracing)"() {
setup: setup:
def requestBuilder = new Request.Builder() def requestBuilder = new Request.Builder()
.url("http://localhost:$port/$path") .url("http://localhost:$port/$path")
.get() .get()
if (distributedTracing) {
requestBuilder.header("x-datadog-trace-id", "123")
requestBuilder.header("x-datadog-parent-id", "456")
}
if (auth) { if (auth) {
requestBuilder.header(HttpHeaders.AUTHORIZATION, Credentials.basic("user", "password")) requestBuilder.header(HttpHeaders.AUTHORIZATION, Credentials.basic("user", "password"))
} }
@ -76,12 +80,17 @@ class JettyServlet3Test extends AgentTestRunner {
assertTraces(TEST_WRITER, 1) { assertTraces(TEST_WRITER, 1) {
trace(0, 1) { trace(0, 1) {
span(0) { span(0) {
if (distributedTracing) {
traceId "123"
parentId "456"
} else {
parent()
}
serviceName "unnamed-java-app" serviceName "unnamed-java-app"
operationName "servlet.request" operationName "servlet.request"
resourceName "GET /$path" resourceName "GET /$path"
spanType DDSpanTypes.WEB_SERVLET spanType DDSpanTypes.WEB_SERVLET
errored false errored false
parent()
tags { tags {
"http.url" "http://localhost:$port/$path" "http.url" "http://localhost:$port/$path"
"http.method" "GET" "http.method" "GET"
@ -100,11 +109,15 @@ class JettyServlet3Test extends AgentTestRunner {
} }
where: where:
path | expectedResponse | auth | origin path | expectedResponse | auth | origin | distributedTracing
"async" | "Hello Async" | false | "Async" "async" | "Hello Async" | false | "Async" | false
"sync" | "Hello Sync" | false | "Sync" "sync" | "Hello Sync" | false | "Sync" | false
"auth/async" | "Hello Async" | true | "Async" "auth/async" | "Hello Async" | true | "Async" | false
"auth/sync" | "Hello Sync" | true | "Sync" "auth/sync" | "Hello Sync" | true | "Sync" | false
"async" | "Hello Async" | false | "Async" | true
"sync" | "Hello Sync" | false | "Sync" | true
"auth/async" | "Hello Async" | true | "Async" | true
"auth/sync" | "Hello Sync" | true | "Sync" | true
} }
def "servlet instrumentation clears state after async request"() { def "servlet instrumentation clears state after async request"() {

View File

@ -59,13 +59,16 @@ class TomcatServlet3Test extends AgentTestRunner {
tomcatServer.destroy() tomcatServer.destroy()
} }
def "test #path servlet call"() { def "test #path servlet call (distributed tracing: #distributedTracing)"() {
setup: setup:
def request = new Request.Builder() def requestBuilder = new Request.Builder()
.url("http://localhost:$port/my-context/$path") .url("http://localhost:$port/my-context/$path")
.get() .get()
.build() if (distributedTracing) {
def response = client.newCall(request).execute() requestBuilder.header("x-datadog-trace-id", "123")
requestBuilder.header("x-datadog-parent-id", "456")
}
def response = client.newCall(requestBuilder.build()).execute()
expect: expect:
response.body().string().trim() == expectedResponse response.body().string().trim() == expectedResponse
@ -73,12 +76,17 @@ class TomcatServlet3Test extends AgentTestRunner {
assertTraces(TEST_WRITER, 1) { assertTraces(TEST_WRITER, 1) {
trace(0, 1) { trace(0, 1) {
span(0) { span(0) {
if (distributedTracing) {
traceId "123"
parentId "456"
} else {
parent()
}
serviceName "my-context" serviceName "my-context"
operationName "servlet.request" operationName "servlet.request"
resourceName "GET /my-context/$path" resourceName "GET /my-context/$path"
spanType DDSpanTypes.WEB_SERVLET spanType DDSpanTypes.WEB_SERVLET
errored false errored false
parent()
tags { tags {
"http.url" "http://localhost:$port/my-context/$path" "http.url" "http://localhost:$port/my-context/$path"
"http.method" "GET" "http.method" "GET"
@ -95,9 +103,11 @@ class TomcatServlet3Test extends AgentTestRunner {
} }
where: where:
path | expectedResponse path | expectedResponse | distributedTracing
"async" | "Hello Async" "async" | "Hello Async" | false
"sync" | "Hello Sync" "sync" | "Hello Sync" | false
"async" | "Hello Async" | true
"sync" | "Hello Sync" | true
} }
def "test #path error servlet call"() { def "test #path error servlet call"() {

View File

@ -212,6 +212,7 @@ public class TestUtils {
obj = null; obj = null;
while (ref.get() != null) { while (ref.get() != null) {
System.gc(); System.gc();
System.runFinalization();
} }
} }
} }

View File

@ -213,20 +213,6 @@ public class PendingTrace extends ConcurrentLinkedDeque<DDSpan> {
return count > 0; return count > 0;
} }
/**
* This method ensures that garbage collection takes place, unlike <code>{@link System#gc()}
* </code>. Useful for testing.
*/
public static void awaitGC() {
System.gc(); // For good measure.
Object obj = new Object();
final WeakReference ref = new WeakReference<>(obj);
obj = null;
while (ref.get() != null) {
System.gc();
}
}
static void close() { static void close() {
SPAN_CLEANER.close(); SPAN_CLEANER.close();
} }

View File

@ -1,5 +1,6 @@
package datadog.opentracing package datadog.opentracing
import datadog.trace.agent.test.TestUtils
import datadog.trace.common.writer.ListWriter import datadog.trace.common.writer.ListWriter
import spock.lang.Specification import spock.lang.Specification
import spock.lang.Subject import spock.lang.Subject
@ -106,7 +107,7 @@ class PendingTraceTest extends Specification {
when: when:
child = null child = null
while (!trace.clean()) { while (!trace.clean()) {
trace.awaitGC() TestUtils.awaitGC()
} }
then: then:

View File

@ -3,7 +3,7 @@ package datadog.opentracing.scopemanager
import datadog.opentracing.DDSpan import datadog.opentracing.DDSpan
import datadog.opentracing.DDSpanContext import datadog.opentracing.DDSpanContext
import datadog.opentracing.DDTracer import datadog.opentracing.DDTracer
import datadog.opentracing.PendingTrace import datadog.trace.agent.test.TestUtils
import datadog.trace.common.writer.ListWriter import datadog.trace.common.writer.ListWriter
import io.opentracing.Scope import io.opentracing.Scope
import io.opentracing.Span import io.opentracing.Span
@ -149,7 +149,7 @@ class ScopeManagerTest extends Specification {
continuation.activate() continuation.activate()
if (forceGC) { if (forceGC) {
continuation = null // Continuation references also hold up traces. continuation = null // Continuation references also hold up traces.
PendingTrace.awaitGC() TestUtils.awaitGC()
((DDSpanContext) scope.span().context()).trace.clean() ((DDSpanContext) scope.span().context()).trace.clean()
} }
if (autoClose) { if (autoClose) {
@ -196,7 +196,7 @@ class ScopeManagerTest extends Specification {
if (forceGC) { if (forceGC) {
continuation = null // Continuation references also hold up traces. continuation = null // Continuation references also hold up traces.
while (!((DDSpanContext) span.context()).trace.clean()) { while (!((DDSpanContext) span.context()).trace.clean()) {
PendingTrace.awaitGC() TestUtils.awaitGC()
} }
writer.waitForTraces(1) writer.waitForTraces(1)
} }