Use assertTraces api in slick tests
MODIFY
dd-java-agent/instrumentation/java-concurrent/src/slickTest/groovy/SlickTest.groovy
dd-java-agent/instrumentation/java-concurrent/src/slickTest/groovy/SlickTest.groovy
import datadog.trace.agent.test.AgentTestRunner
+import datadog.trace.api.DDSpanTypes
+import datadog.trace.api.DDTags
+import io.opentracing.tag.Tags
import spock.lang.Shared
+import static datadog.trace.agent.test.ListWriterAssert.assertTraces
+
class SlickTest extends AgentTestRunner {
@Shared
@@ -10,41 +15,44 @@ class SlickTest extends AgentTestRunner {
setup:
def future = database.startQuery(SlickUtils.TestQuery())
def result = database.getResults(future)
- TEST_WRITER.waitForTraces(1)
expect:
result == SlickUtils.TestValue()
- TEST_WRITER.size() == 1
-
- def trace = TEST_WRITER.firstTrace()
- trace.size() == 2
-
- def rootSpan = trace[0]
- rootSpan.serviceName == "unnamed-java-app"
- rootSpan.resourceName == "SlickUtils.startQuery"
-
- def dbSpan = trace[1]
- dbSpan.context().operationName == "${SlickUtils.Driver()}.query"
- dbSpan.serviceName == SlickUtils.Driver()
- dbSpan.resourceName == SlickUtils.TestQuery()
- dbSpan.type == "sql"
- !dbSpan.context().getErrorFlag()
- dbSpan.context().parentId == rootSpan.spanId
-
- def tags = dbSpan.context().tags
- tags["db.type"] == SlickUtils.Driver()
- tags["db.user"] == null
- tags["span.kind"] == "client"
- tags["span.type"] == "sql"
- tags["component"] == "java-jdbc-prepared_statement"
-
- tags["db.jdbc.url"].contains(SlickUtils.Driver())
- tags["span.origin.type"] != null
-
- tags["thread.name"] != null
- tags["thread.id"] != null
- tags.size() == 8
+ assertTraces(TEST_WRITER, 1) {
+ trace(0, 2) {
+ span(0) {
+ operationName "SlickUtils.startQuery"
+ serviceName "unnamed-java-app"
+ resourceName "SlickUtils.startQuery"
+ parent()
+ errored false
+ tags {
+ defaultTags()
+ }
+ }
+ span(1) {
+ operationName "${SlickUtils.Driver()}.query"
+ serviceName SlickUtils.Driver()
+ resourceName SlickUtils.TestQuery()
+ childOf span(0)
+ errored false
+ tags {
+ "$Tags.COMPONENT.key" "java-jdbc-prepared_statement"
+ "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
+ "$DDTags.SPAN_TYPE" DDSpanTypes.SQL
+
+ "$Tags.DB_TYPE.key" SlickUtils.Driver()
+ "$Tags.DB_USER.key" SlickUtils.Username()
+
+ "db.jdbc.url" SlickUtils.Url()
+ "span.origin.type" "org.h2.jdbc.JdbcPreparedStatement"
+
+ defaultTags()
+ }
+ }
+ }
+ }
}
def "Concurrent requests do not throw exception"() {
@@ -55,13 +63,20 @@ class SlickTest extends AgentTestRunner {
def result = database.getResults(future)
database.getResults(sleepFuture)
- TEST_WRITER.waitForTraces(2)
expect:
result == SlickUtils.TestValue()
- TEST_WRITER.size() == 2 // Since we have two DB queries
- def trace = TEST_WRITER.firstTrace()
- trace.size() == 2
+ // Expect two traces because two queries have been run
+ assertTraces(TEST_WRITER, 2) {
+ trace(0, 2, {
+ span(0) {}
+ span(1) {}
+ })
+ trace(1, 2, {
+ span(0) {}
+ span(1) {}
+ })
+ }
}
}

MODIFY
dd-java-agent/instrumentation/java-concurrent/src/slickTest/scala/SlickUtils.scala
dd-java-agent/instrumentation/java-concurrent/src/slickTest/scala/SlickUtils.scala
@@ -9,10 +9,11 @@ import scala.concurrent.duration.Duration
class SlickUtils {
import SlickUtils._
- val database = Database.forURL(s"jdbc:${Driver}:mem:test",
+ val database = Database.forURL(Url,
+ user=Username,
driver="org.h2.Driver",
keepAliveConnection=true,
- // Limit numbner of threads to hit Slick-specific case when we need to avoid re-wrapping
+ // Limit number of threads to hit Slick-specific case when we need to avoid re-wrapping
// wrapped runnables.
executor=AsyncExecutor("test", numThreads=1, queueSize=1000)
)
@@ -32,10 +33,12 @@ class SlickUtils {
object SlickUtils {
- var Driver = "h2"
+ val Driver = "h2"
+ val Username = "TESTUSER"
+ val Url = s"jdbc:${Driver}:mem:test"
val TestValue = 3
val TestQuery = "SELECT 3"
- var SleepQuery = "CALL SLEEP(3000)"
+ val SleepQuery = "CALL SLEEP(3000)"
}

Use assertTraces api in slick tests