diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockTracingListener.java b/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockTracingListener.java index f88cf5c5852..d403e3002f2 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockTracingListener.java +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockTracingListener.java @@ -119,7 +119,8 @@ private void testCaseExecutionStarted(final TestDescriptor testDescriptor) { private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSource testSource) { TestDescriptor suiteDescriptor = SpockUtils.getSpecDescriptor(testDescriptor); String displayName = testDescriptor.getDisplayName(); - String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName); + String testParameters = + JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName); List tags = JUnitPlatformUtils.getTags(testDescriptor); TestSourceData testSourceData = SpockUtils.toTestSourceData(testDescriptor); @@ -221,7 +222,8 @@ private void testMethodExecutionSkipped( final TestDescriptor testDescriptor, final MethodSource methodSource, final String reason) { TestDescriptor suiteDescriptor = SpockUtils.getSpecDescriptor(testDescriptor); String displayName = testDescriptor.getDisplayName(); - String testParameters = JUnitPlatformUtils.getParameters(methodSource, displayName); + String testParameters = + JUnitPlatformUtils.getParameters(testDescriptor, methodSource, displayName); List tags = testDescriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList()); TestSourceData testSourceData = SpockUtils.toTestSourceData(testDescriptor); diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockUtils.java b/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockUtils.java index eaa4a14dcc3..8cd89350722 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockUtils.java +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5-spock-2.0/src/main/java/datadog/trace/instrumentation/junit5/SpockUtils.java @@ -89,7 +89,8 @@ public static TestIdentifier toTestIdentifier(TestDescriptor testDescriptor) { MethodSource methodSource = (MethodSource) testSource; String testSuiteName = methodSource.getClassName(); String displayName = spockNode.getDisplayName(); - String testParameters = JUnitPlatformUtils.getParameters(methodSource, displayName); + String testParameters = + JUnitPlatformUtils.getParameters(testDescriptor, methodSource, displayName); return new TestIdentifier(testSuiteName, displayName, testParameters); } else { diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/JUnitPlatformUtils.java b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/JUnitPlatformUtils.java index 14c12cbcc60..ac7849dce81 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/JUnitPlatformUtils.java +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/JUnitPlatformUtils.java @@ -148,12 +148,14 @@ private static Method getTestMethod(MethodSource methodSource) { } } - public static String getParameters(MethodSource methodSource, String displayName) { - if (methodSource.getMethodParameterTypes() == null - || methodSource.getMethodParameterTypes().isEmpty()) { - return null; + public static String getParameters( + TestDescriptor testDescriptor, MethodSource methodSource, String displayName) { + if (isDynamicTest(testDescriptor) + || (methodSource.getMethodParameterTypes() != null + && !methodSource.getMethodParameterTypes().isEmpty())) { + return "{\"metadata\":{\"test_name\":" + toJson(displayName) + "}}"; } - return "{\"metadata\":{\"test_name\":" + toJson(displayName) + "}}"; + return null; } @Nullable @@ -164,7 +166,7 @@ public static TestIdentifier toTestIdentifier(TestDescriptor testDescriptor) { String testSuiteName = methodSource.getClassName(); String testName = methodSource.getMethodName(); String displayName = testDescriptor.getDisplayName(); - String testParameters = getParameters(methodSource, displayName); + String testParameters = getParameters(testDescriptor, methodSource, displayName); return new TestIdentifier(testSuiteName, testName, testParameters); } else { @@ -240,6 +242,12 @@ public static boolean isParameterizedTest(TestDescriptor testDescriptor) { return "test-template".equals(lastSegment.getType()); } + public static boolean isDynamicTest(TestDescriptor testDescriptor) { + // retries add a "retry-attempt" segment at the end of the uniqueId, so the "dynamic-test" + // segment might not be found in position (segments.size() - 1) + return getIDSegmentValue(testDescriptor, "dynamic-test") != null; + } + public static boolean isRetry(TestDescriptor testDescriptor) { return getIDSegmentValue(testDescriptor, RETRY_DESCRIPTOR_ID_SUFFIX) != null; } diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TracingListener.java b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TracingListener.java index 8f4f693fc7e..ab38320ed48 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TracingListener.java +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/main/java/datadog/trace/instrumentation/junit5/TracingListener.java @@ -3,6 +3,9 @@ import datadog.trace.api.civisibility.config.TestSourceData; import datadog.trace.api.civisibility.execution.TestExecutionHistory; import datadog.trace.api.civisibility.telemetry.tag.TestFrameworkInstrumentation; +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; +import datadog.trace.bootstrap.instrumentation.api.Tags; import java.util.List; import java.util.stream.Collectors; import org.junit.platform.engine.EngineExecutionListener; @@ -119,7 +122,8 @@ private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSou String displayName = testDescriptor.getDisplayName(); String testName = testSource.getMethodName(); - String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName); + String testParameters = + JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName); List tags = JUnitPlatformUtils.getTags(testDescriptor); TestSourceData testSourceData = JUnitPlatformUtils.toTestSourceData(testDescriptor); @@ -136,6 +140,13 @@ private void testMethodExecutionStarted(TestDescriptor testDescriptor, MethodSou testSourceData, null, TestEventsHandlerHolder.getExecutionHistory(testDescriptor)); + + if (JUnitPlatformUtils.isDynamicTest(testDescriptor)) { + AgentSpan span = AgentTracer.activeSpan(); + if (span != null) { + span.setTag(Tags.TEST_JUNIT_IS_DYNAMIC, true); + } + } } private void testCaseExecutionFinished( @@ -224,7 +235,8 @@ private void testMethodExecutionSkipped( String displayName = testDescriptor.getDisplayName(); String testName = testSource.getMethodName(); - String testParameters = JUnitPlatformUtils.getParameters(testSource, displayName); + String testParameters = + JUnitPlatformUtils.getParameters(testDescriptor, testSource, displayName); List tags = testDescriptor.getTags().stream().map(TestTag::getName).collect(Collectors.toList()); TestSourceData testSourceData = JUnitPlatformUtils.toTestSourceData(testDescriptor); @@ -242,5 +254,12 @@ private void testMethodExecutionSkipped( testSourceData, reason, TestEventsHandlerHolder.getExecutionHistory(testDescriptor)); + + if (JUnitPlatformUtils.isDynamicTest(testDescriptor)) { + AgentSpan span = AgentTracer.activeSpan(); + if (span != null) { + span.setTag(Tags.TEST_JUNIT_IS_DYNAMIC, true); + } + } } } diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-factory/events.ftl b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-factory/events.ftl index 9f0dd405173..ccee0a8b4b5 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-factory/events.ftl +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-factory/events.ftl @@ -118,8 +118,10 @@ "test.final_status" : "pass", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", "test.status" : "pass", @@ -166,8 +168,10 @@ "test.final_status" : "pass", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", "test.status" : "pass", @@ -196,4 +200,4 @@ }, "type" : "test", "version" : 2 -} ] \ No newline at end of file +} ] diff --git a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-retry-factory/events.ftl b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-retry-factory/events.ftl index cb87ef28399..17c3604e3ae 100644 --- a/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-retry-factory/events.ftl +++ b/dd-java-agent/instrumentation/junit/junit-5/junit-5.3/src/test/resources/test-retry-factory/events.ftl @@ -118,8 +118,10 @@ "test.final_status" : "pass", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_succeed\"}}", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", "test.status" : "pass", @@ -169,8 +171,10 @@ "test.failure_suppressed" : "true", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", "test.status" : "fail", @@ -220,9 +224,11 @@ "test.failure_suppressed" : "true", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.is_retry" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}", "test.retry_reason" : "auto_test_retry", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", @@ -273,9 +279,11 @@ "test.failure_suppressed" : "true", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.is_retry" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}", "test.retry_reason" : "auto_test_retry", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", @@ -326,9 +334,11 @@ "test.failure_suppressed" : "true", "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, + "test.junit5.is_dynamic" : "true", "test.is_retry" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}", "test.retry_reason" : "auto_test_retry", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", @@ -380,9 +390,11 @@ "test.framework" : "junit5", "test.framework_version" : ${content_meta_test_framework_version}, "test.has_failed_all_retries" : "true", + "test.junit5.is_dynamic" : "true", "test.is_retry" : "true", "test.module" : "junit-5.3", "test.name" : "test_factory", + "test.parameters" : "{\"metadata\":{\"test_name\":\"dynamic_test_failed\"}}", "test.retry_reason" : "auto_test_retry", "test.source.file" : "dummy_source_path", "test.source.method" : "test_factory()Ljava/lang/Iterable;", @@ -412,4 +424,4 @@ }, "type" : "test", "version" : 2 -} ] \ No newline at end of file +} ] diff --git a/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java b/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java index d826704527c..35a07329a65 100644 --- a/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java +++ b/internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java @@ -98,6 +98,7 @@ public class Tags { public static final String TEST_IS_NEW = "test.is_new"; public static final String TEST_IS_RETRY = "test.is_retry"; public static final String TEST_RETRY_REASON = "test.retry_reason"; + public static final String TEST_JUNIT_IS_DYNAMIC = "test.junit5.is_dynamic"; public static final String TEST_IS_MODIFIED = "test.is_modified"; public static final String TEST_HAS_FAILED_ALL_RETRIES = "test.has_failed_all_retries"; public static final String TEST_FAILURE_SUPPRESSED = "test.failure_suppressed";