Skip to content

Commit

Permalink
adds tag to specify if the user is setting DD_SERVICE
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mohedano committed Jan 31, 2025
1 parent a41d151 commit cb8f3a1
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static datadog.trace.api.civisibility.CIConstants.CI_VISIBILITY_INSTRUMENTATION_NAME;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
import datadog.trace.api.civisibility.telemetry.CiVisibilityMetricCollector;
import datadog.trace.api.civisibility.telemetry.tag.EventType;
Expand Down Expand Up @@ -72,6 +73,8 @@ public AbstractTestModule(
span.setTag(Tags.TEST_MODULE_ID, span.getSpanId());
span.setTag(Tags.TEST_SESSION_ID, span.getTraceId());

span.setTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE, config.isServiceNameSetByUser());

// setting status to skip initially,
// as we do not know in advance whether the module will have any children
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static datadog.trace.api.civisibility.CIConstants.CI_VISIBILITY_INSTRUMENTATION_NAME;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.IdGenerationStrategy;
import datadog.trace.api.civisibility.CIConstants;
Expand Down Expand Up @@ -97,6 +98,8 @@ public AbstractTestSession(
span.setTag(Tags.SPAN_KIND, Tags.SPAN_KIND_TEST_SESSION);
span.setTag(Tags.TEST_SESSION_ID, span.getTraceId());

span.setTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE, config.isServiceNameSetByUser());

// setting status to skip initially,
// as we do not know in advance whether the session will have any children
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.civisibility.CIConstants;
import datadog.trace.api.civisibility.DDTest;
Expand Down Expand Up @@ -124,6 +125,8 @@ public TestImpl(
span.setTag(Tags.TEST_MODULE_ID, moduleSpanContext.getSpanId());
span.setTag(Tags.TEST_SESSION_ID, moduleSpanContext.getTraceId());

span.setTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE, config.isServiceNameSetByUser());

span.setTag(Tags.TEST_STATUS, TestStatus.pass);

if (testClass != null && !testClass.getName().equals(testSuiteName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.civisibility.DDTestSuite;
import datadog.trace.api.civisibility.coverage.CoverageStore;
import datadog.trace.api.civisibility.telemetry.CiVisibilityCountMetric;
Expand Down Expand Up @@ -113,6 +114,8 @@ public TestSuiteImpl(
span.setTag(Tags.TEST_MODULE_ID, moduleSpanContext.getSpanId());
span.setTag(Tags.TEST_SESSION_ID, moduleSpanContext.getTraceId());

span.setTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE, config.isServiceNameSetByUser());

// setting status to skip initially,
// as we do not know in advance whether the suite will have any children
span.setTag(Tags.TEST_STATUS, TestStatus.skip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datadog.trace.civisibility.domain
import datadog.trace.agent.test.asserts.ListWriterAssert
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import datadog.trace.api.DDTraceId
import datadog.trace.api.civisibility.config.TestIdentifier
import datadog.trace.api.civisibility.coverage.CoverageProbes
Expand All @@ -20,7 +21,7 @@ import datadog.trace.civisibility.test.ExecutionResults
import datadog.trace.civisibility.utils.SpanUtils

class TestImplTest extends SpanWriterTest {
def "test span is generated"() {
def "test span is generated and tags populated"() {
setup:
def test = givenATest()

Expand All @@ -33,6 +34,9 @@ class TestImplTest extends SpanWriterTest {
span(0) {
parent()
spanType DDSpanTypes.TEST
tags(false) {
"$DDTags.TEST_IS_USER_PROVIDED_SERVICE" true
}
}
}
})
Expand Down Expand Up @@ -84,8 +88,7 @@ class TestImplTest extends SpanWriterTest {
0 * coverageStore.report(_, _, _)
}

private TestImpl givenATest(
CoverageStore.Factory coverageStoreFactory = new NoOpCoverageStore.Factory()) {
private TestImpl givenATest(CoverageStore.Factory coverageStoreFactory = new NoOpCoverageStore.Factory()) {

def traceId = Stub(DDTraceId)
traceId.toLong() >> 123
Expand All @@ -95,8 +98,10 @@ class TestImplTest extends SpanWriterTest {
moduleSpanContext.getTraceId() >> traceId
def suiteId = 789

def config = Spy(Config.get())
config.isServiceNameSetByUser() >> true

def testFramework = TestFrameworkInstrumentation.OTHER
def config = Config.get()
def metricCollector = Stub(CiVisibilityMetricCollectorImpl)
def executionResults = Stub(ExecutionResults)
def testDecorator = new TestDecoratorImpl("component", "session-name", "test-command", [:])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datadog.trace.civisibility.domain
import datadog.trace.agent.test.asserts.ListWriterAssert
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import datadog.trace.api.DDTraceId
import datadog.trace.api.civisibility.coverage.CoverageStore
import datadog.trace.api.civisibility.coverage.NoOpCoverageStore
Expand Down Expand Up @@ -34,6 +35,7 @@ class TestSuiteImplTest extends SpanWriterTest {
"$Tags.TEST_CODEOWNERS" "[\"@global-owner1\",\"@global-owner2\"]"
"$Tags.TEST_SOURCE_START" 10
"$Tags.TEST_SOURCE_END" 20
"$DDTags.TEST_IS_USER_PROVIDED_SERVICE" true
}
}
}
Expand All @@ -42,17 +44,18 @@ class TestSuiteImplTest extends SpanWriterTest {

private static final class MyClass {}

private TestSuiteImpl givenATestSuite(
CoverageStore.Factory coverageStoreFactory = new NoOpCoverageStore.Factory()) {
private TestSuiteImpl givenATestSuite(CoverageStore.Factory coverageStoreFactory = new NoOpCoverageStore.Factory()) {
def traceId = Stub(DDTraceId)
traceId.toLong() >> 123

def moduleSpanContext = Stub(AgentSpanContext)
moduleSpanContext.getSpanId() >> 456
moduleSpanContext.getTraceId() >> traceId

def config = Spy(Config.get())
config.isServiceNameSetByUser() >> true

def testFramework = TestFrameworkInstrumentation.OTHER
def config = Config.get()
def metricCollector = Stub(CiVisibilityMetricCollectorImpl)
def executionResults = Stub(ExecutionResults)
def testDecorator = new TestDecoratorImpl("component", "session-name", "test-command", [:])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package datadog.trace.civisibility.domain.headless

import datadog.trace.agent.test.asserts.ListWriterAssert
import datadog.trace.api.Config
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import datadog.trace.api.civisibility.config.TestIdentifier
import datadog.trace.api.civisibility.config.TestSourceData
import datadog.trace.api.civisibility.coverage.CoverageStore
Expand All @@ -10,14 +13,58 @@ import datadog.trace.civisibility.codeowners.Codeowners
import datadog.trace.civisibility.config.EarlyFlakeDetectionSettings
import datadog.trace.civisibility.config.ExecutionSettings
import datadog.trace.civisibility.decorator.TestDecorator
import datadog.trace.civisibility.domain.SpanWriterTest
import datadog.trace.civisibility.source.LinesResolver
import datadog.trace.civisibility.source.SourcePathResolver
import datadog.trace.civisibility.test.ExecutionStrategy
import datadog.trace.test.util.DDSpecification

class HeadlessTestModuleTest extends DDSpecification {
class HeadlessTestModuleTest extends SpanWriterTest {
def "test module span is created and tags populated"() {
setup:
def headlessTestModule = givenAHeadlessTestModule()

when:
headlessTestModule.end(null)

then:
ListWriterAssert.assertTraces(TEST_WRITER, 1, false, {
trace(1) {
span(0) {
spanType DDSpanTypes.TEST_MODULE_END
tags(false) {
"$DDTags.TEST_IS_USER_PROVIDED_SERVICE" true
}
}
}
})
}

def "test total retries limit is applied across test cases"() {
given:
def headlessTestModule = givenAHeadlessTestModule()

when:
def retryPolicy1 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-1", null), TestSourceData.UNKNOWN)

then:
retryPolicy1.retry(false, 1L) // 2nd test execution, 1st retry globally
!retryPolicy1.retry(false, 1L) // asking for 3rd test execution - local limit reached

when:
def retryPolicy2 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-2", null), TestSourceData.UNKNOWN)

then:
retryPolicy2.retry(false, 1L) // 2nd test execution, 2nd retry globally (since previous test was retried too)
!retryPolicy2.retry(false, 1L) // asking for 3rd test execution - local limit reached

when:
def retryPolicy3 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-3", null), TestSourceData.UNKNOWN)

then:
!retryPolicy3.retry(false, 1L) // asking for 3rd retry globally - global limit reached
}

private HeadlessTestModule givenAHeadlessTestModule() {
def executionSettings = Stub(ExecutionSettings)
executionSettings.getEarlyFlakeDetectionSettings() >> EarlyFlakeDetectionSettings.DEFAULT
executionSettings.isFlakyTestRetriesEnabled() >> true
Expand All @@ -28,11 +75,11 @@ class HeadlessTestModuleTest extends DDSpecification {
// this counts all executions of a test case (first attempt is counted too)
config.getCiVisibilityTotalFlakyRetryCount() >> 2
// this counts retries across all tests (first attempt is not a retry, so it is not counted)
config.isServiceNameSetByUser() >> true

def executionStrategy = new ExecutionStrategy(config, executionSettings, Stub(SourcePathResolver), Stub(LinesResolver))

given:
def headlessTestModule = new HeadlessTestModule(
new HeadlessTestModule(
Stub(AgentSpanContext),
"test-module",
null,
Expand All @@ -46,27 +93,5 @@ class HeadlessTestModuleTest extends DDSpecification {
executionStrategy,
(span) -> { }
)

when:
def retryPolicy1 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-1", null), TestSourceData.UNKNOWN)

then:
retryPolicy1.retry(false, 1L) // 2nd test execution, 1st retry globally
!retryPolicy1.retry(false, 1L) // asking for 3rd test execution - local limit reached

when:
def retryPolicy2 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-2", null), TestSourceData.UNKNOWN)

then:
retryPolicy2.retry(false, 1L) // 2nd test execution, 2nd retry globally (since previous test was retried too)
!retryPolicy2.retry(false, 1L) // asking for 3rd test execution - local limit reached

when:
def retryPolicy3 = headlessTestModule.retryPolicy(new TestIdentifier("suite", "test-3", null), TestSourceData.UNKNOWN)

then:
!retryPolicy3.retry(false, 1L) // asking for 3rd retry globally - global limit reached
}


}
1 change: 1 addition & 0 deletions dd-trace-api/src/main/java/datadog/trace/api/DDTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class DDTags {
public static final String LIBRARY_VERSION_TAG_KEY = "library_version";
public static final String CI_ENV_VARS = "_dd.ci.env_vars";
public static final String CI_ITR_TESTS_SKIPPED = "_dd.ci.itr.tests_skipped";
public static final String TEST_IS_USER_PROVIDED_SERVICE = "_dd.test.is_user_provided_service";
public static final String MEASURED = "_dd.measured";
public static final String PID_TAG = "process_id";
public static final String SCHEMA_VERSION_TAG_KEY = "_dd.trace_span_attribute_schema";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datadog.communication.serialization.GrowableBuffer;
import datadog.communication.serialization.Writable;
import datadog.communication.serialization.msgpack.MsgPackWriter;
import datadog.trace.api.DDTags;
import datadog.trace.api.DDTraceId;
import datadog.trace.api.civisibility.CiVisibilityWellKnownTags;
import datadog.trace.api.civisibility.InstrumentationBridge;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class CiTestCycleMapperV1 implements RemoteMapper {
Tags.TEST_SESSION_ID.getBytes(StandardCharsets.UTF_8);
private static final byte[] TEST_MODULE_ID = Tags.TEST_MODULE_ID.getBytes(StandardCharsets.UTF_8);
private static final byte[] TEST_SUITE_ID = Tags.TEST_SUITE_ID.getBytes(StandardCharsets.UTF_8);
private static final byte[] TEST_IS_USER_PROVIDED_SERVICE =
DDTags.TEST_IS_USER_PROVIDED_SERVICE.getBytes(StandardCharsets.UTF_8);
private static final byte[] ITR_CORRELATION_ID =
Tags.ITR_CORRELATION_ID.getBytes(StandardCharsets.UTF_8);

Expand Down Expand Up @@ -89,6 +92,9 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
Number testSuiteId = span.getTag(Tags.TEST_SUITE_ID);
span.removeTag(Tags.TEST_SUITE_ID);

Boolean testIsUserProvidedService = span.getTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE);
span.removeTag(DDTags.TEST_IS_USER_PROVIDED_SERVICE);

String itrCorrelationId = span.getTag(Tags.ITR_CORRELATION_ID);
span.removeTag(Tags.ITR_CORRELATION_ID);

Expand All @@ -102,6 +108,9 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
if (testSuiteId != null) {
topLevelTagsCount++;
}
if (testIsUserProvidedService != null) {
topLevelTagsCount++;
}
if (itrCorrelationId != null) {
topLevelTagsCount++;
}
Expand Down Expand Up @@ -193,6 +202,10 @@ public void map(List<? extends CoreSpan<?>> trace, Writable writable) {
writable.writeUTF8(TEST_SUITE_ID);
writable.writeObject(testSuiteId, null);
}
if (testIsUserProvidedService != null) {
writable.writeUTF8(TEST_IS_USER_PROVIDED_SERVICE);
writable.writeObject(testIsUserProvidedService, null);
}
if (itrCorrelationId != null) {
writable.writeUTF8(ITR_CORRELATION_ID);
writable.writeObjectString(itrCorrelationId, null);
Expand Down
Loading

0 comments on commit cb8f3a1

Please sign in to comment.