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

View File

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

View File

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

View File

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

View File

@ -213,20 +213,6 @@ public class PendingTrace extends ConcurrentLinkedDeque<DDSpan> {
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() {
SPAN_CLEANER.close();
}

View File

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

View File

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