From 1431734d98c4cc35832e504da0a836553017affe Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Fri, 17 Jan 2025 11:15:54 +0100 Subject: [PATCH] feat(kotlin): Add failing tests --- .../src/test/kotlin/KotlinCoroutineTests.kt | 5 +++ .../KotlinCoroutineInstrumentationTest.groovy | 30 ++++++++++++++ .../src/test/kotlin/KotlinCoroutineTests.kt | 24 +++++++++++ ...tKotlinCoroutineInstrumentationTest.groovy | 41 +++++++++++++++++++ .../coroutines/CoreKotlinCoroutineTests.kt | 24 +++++++++++ 5 files changed, 124 insertions(+) diff --git a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.3/src/test/kotlin/KotlinCoroutineTests.kt b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.3/src/test/kotlin/KotlinCoroutineTests.kt index 305334620d0..6f2d86d1751 100644 --- a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.3/src/test/kotlin/KotlinCoroutineTests.kt +++ b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.3/src/test/kotlin/KotlinCoroutineTests.kt @@ -63,6 +63,11 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin return super.tracedWithLazyStarting() } + @Trace + override fun traceAfterTimeout(): Int { + return super.traceAfterTimeout() + } + @Trace override fun tracedChild(opName: String) { super.tracedChild(opName) diff --git a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/groovy/KotlinCoroutineInstrumentationTest.groovy b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/groovy/KotlinCoroutineInstrumentationTest.groovy index b0d794cbbcc..4dcecc53da4 100644 --- a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/groovy/KotlinCoroutineInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/groovy/KotlinCoroutineInstrumentationTest.groovy @@ -1,6 +1,7 @@ import datadog.trace.core.DDSpan import datadog.trace.instrumentation.kotlin.coroutines.AbstractKotlinCoroutineInstrumentationTest import kotlinx.coroutines.CoroutineDispatcher +import spock.lang.Ignore class KotlinCoroutineInstrumentationTest extends AbstractKotlinCoroutineInstrumentationTest { @@ -42,4 +43,33 @@ class KotlinCoroutineInstrumentationTest extends AbstractKotlinCoroutineInstrume where: [dispatcherName, dispatcher] << dispatchersToTest } + + @Ignore("Not working: disconnected trace") + def "kotlin trace consistent after flow"() { + setup: + KotlinCoroutineTests kotlinTest = new KotlinCoroutineTests(dispatcher) + int expectedNumberOfSpans = kotlinTest.traceAfterFlow() + TEST_WRITER.waitForTraces(1) + + expect: + assertTraces(1) { + trace(expectedNumberOfSpans, true) { + span(2) { + operationName "trace.annotation" + parent() + } + span(1) { + operationName "outside-flow" + childOf span(2) + } + span(0) { + operationName "inside-flow" + childOf span(2) + } + } + } + + where: + [dispatcherName, dispatcher] << dispatchersToTest + } } diff --git a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/kotlin/KotlinCoroutineTests.kt b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/kotlin/KotlinCoroutineTests.kt index 45f6c277844..19bfaf26db7 100644 --- a/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/kotlin/KotlinCoroutineTests.kt +++ b/dd-java-agent/instrumentation/kotlin-coroutines/coroutines-1.5/src/test/kotlin/KotlinCoroutineTests.kt @@ -2,9 +2,11 @@ import datadog.trace.api.Trace import datadog.trace.instrumentation.kotlin.coroutines.CoreKotlinCoroutineTests import edu.umd.cs.findbugs.annotations.SuppressFBWarnings import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.flow.single import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeout @@ -35,6 +37,23 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin 7 } + @Trace + fun traceAfterFlow(): Int = runTest { + val f = flow { + childSpan("inside-flow").activateAndUse { + println("insideFlowSpan") + } + emit(1) + }.flowOn(Dispatchers.IO) + val ff = f.single() + // FIXME: This span is detached + childSpan("outside-flow").activateAndUse { + println("hello $ff") + } + + 3 + } + @Trace override fun tracePreventedByCancellation(): Int { return super.tracePreventedByCancellation() @@ -65,6 +84,11 @@ class KotlinCoroutineTests(dispatcher: CoroutineDispatcher) : CoreKotlinCoroutin return super.tracedWithLazyStarting() } + @Trace + override fun traceAfterTimeout(): Int { + return super.traceAfterTimeout() + } + @Trace override fun tracedChild(opName: String) { super.tracedChild(opName) diff --git a/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/groovy/datadog/trace/instrumentation/kotlin/coroutines/AbstractKotlinCoroutineInstrumentationTest.groovy b/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/groovy/datadog/trace/instrumentation/kotlin/coroutines/AbstractKotlinCoroutineInstrumentationTest.groovy index b92c714a8aa..af01ad7441c 100644 --- a/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/groovy/datadog/trace/instrumentation/kotlin/coroutines/AbstractKotlinCoroutineInstrumentationTest.groovy +++ b/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/groovy/datadog/trace/instrumentation/kotlin/coroutines/AbstractKotlinCoroutineInstrumentationTest.groovy @@ -5,6 +5,7 @@ import datadog.trace.core.DDSpan import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ThreadPoolDispatcherKt +import spock.lang.Ignore import spock.lang.Shared abstract class AbstractKotlinCoroutineInstrumentationTest extends AgentTestRunner { @@ -365,6 +366,46 @@ abstract class AbstractKotlinCoroutineInstrumentationTest trace, String opName) { for (DDSpan span : trace) { if (span.getOperationName() == opName) { diff --git a/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/kotlin/datadog/trace/instrumentation/kotlin/coroutines/CoreKotlinCoroutineTests.kt b/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/kotlin/datadog/trace/instrumentation/kotlin/coroutines/CoreKotlinCoroutineTests.kt index d18cc07a1ed..8a9c3aa90d8 100644 --- a/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/kotlin/datadog/trace/instrumentation/kotlin/coroutines/CoreKotlinCoroutineTests.kt +++ b/dd-java-agent/instrumentation/kotlin-coroutines/src/testFixtures/kotlin/datadog/trace/instrumentation/kotlin/coroutines/CoreKotlinCoroutineTests.kt @@ -311,6 +311,30 @@ abstract class CoreKotlinCoroutineTests(private val dispatcher: CoroutineDispatc 1 } + @Trace + open fun traceAfterTimeout(): Int = runTest { + childSpan("1-before-timeout").activateAndUse { + delay(10) + } + withTimeout(50) { + childSpan("2-inside-timeout").activateAndUse { + delay(10) + } + } + // FIXME: This span is detached + childSpan("3-after-timeout").activateAndUse { + delay(10) + } + childSpan("4-after-timeout-2").activateAndUse { + delay(10) + } + childSpan("5-after-timeout-3").activateAndUse { + delay(10) + } + + 6 + } + @Trace protected open fun tracedChild(opName: String) { activeSpan().setSpanName(opName)