Skip to content

Commit

Permalink
Set test session name on test/suite/module/session events (#7603)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog authored Sep 16, 2024
1 parent 57d9846 commit 7108ba0
Show file tree
Hide file tree
Showing 369 changed files with 4,347 additions and 10,684 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ private static BuildSystemSession.Factory buildSystemSessionFactory(

repoServices.gitDataUploader.startOrObserveGitDataUpload();

TestDecorator testDecorator = new TestDecoratorImpl(buildSystemName, repoServices.ciTags);
String sessionName = services.config.getCiVisibilitySessionName();
TestDecorator testDecorator =
new TestDecoratorImpl(buildSystemName, sessionName, startCommand, repoServices.ciTags);

String signalServerHost = services.config.getCiVisibilitySignalServerHost();
int signalServerPort = services.config.getCiVisibilitySignalServerPort();
Expand Down Expand Up @@ -223,7 +225,11 @@ private static TestFrameworkSession.Factory childTestFrameworkSessionFactory(
long parentProcessSessionId = ProcessHierarchyUtils.getParentSessionId();
long parentProcessModuleId = ProcessHierarchyUtils.getParentModuleId();

TestDecorator testDecorator = new TestDecoratorImpl(component, repoServices.ciTags);
String sessionName = services.config.getCiVisibilitySessionName();
String testCommand = services.config.getCiVisibilityTestCommand();
TestDecorator testDecorator =
new TestDecoratorImpl(component, sessionName, testCommand, repoServices.ciTags);

ExecutionStrategy executionStrategy =
new ExecutionStrategy(services.config, executionSettings);
return new ProxyTestSession(
Expand All @@ -250,7 +256,10 @@ private static TestFrameworkSession.Factory headlessTestFrameworkEssionFactory(
return (String projectName, String component, Long startTime) -> {
repoServices.gitDataUploader.startOrObserveGitDataUpload();

TestDecorator testDecorator = new TestDecoratorImpl(component, repoServices.ciTags);
String sessionName = services.config.getCiVisibilitySessionName();
TestDecorator testDecorator =
new TestDecoratorImpl(component, sessionName, projectName, repoServices.ciTags);

ExecutionStrategy executionStrategy =
new ExecutionStrategy(services.config, executionSettings);
return new HeadlessTestSession(
Expand All @@ -272,7 +281,11 @@ private static CIVisibility.SessionFactory manualApiSessionFactory(
CiVisibilityServices services) {
return (String projectName, Path projectRoot, String component, Long startTime) -> {
CiVisibilityRepoServices repoServices = services.repoServices(projectRoot);
TestDecorator testDecorator = new TestDecoratorImpl(component, repoServices.ciTags);

String sessionName = services.config.getCiVisibilitySessionName();
TestDecorator testDecorator =
new TestDecoratorImpl(component, sessionName, projectName, repoServices.ciTags);

ExecutionSettings executionSettings =
repoServices.executionSettingsFactory.create(JvmInfo.CURRENT_JVM, null);
CiVisibilityCoverageServices.Child coverageServices =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.trace.civisibility.config;

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CiVisibilityWellKnownTags;
import datadog.trace.api.civisibility.config.TestIdentifier;
import datadog.trace.api.civisibility.config.TestMetadata;
import datadog.trace.api.git.GitInfo;
Expand Down Expand Up @@ -79,19 +80,16 @@ private TracerEnvironment buildTracerEnvironment(
}
}

/*
* IMPORTANT: JVM and OS properties should match tags
* set in datadog.trace.civisibility.decorator.TestDecoratorImpl
*/
CiVisibilityWellKnownTags wellKnownTags = config.getCiVisibilityWellKnownTags();
return builder
.service(config.getServiceName())
.env(config.getEnv())
.repositoryUrl(gitInfo.getRepositoryURL())
.branch(gitInfo.getBranch())
.sha(gitInfo.getCommit().getSha())
.osPlatform(System.getProperty("os.name"))
.osArchitecture(System.getProperty("os.arch"))
.osVersion(System.getProperty("os.version"))
.osPlatform(wellKnownTags.getOsPlatform().toString())
.osArchitecture(wellKnownTags.getOsArch().toString())
.osVersion(wellKnownTags.getOsVersion().toString())
.runtimeName(jvmInfo.getName())
.runtimeVersion(jvmInfo.getVersion())
.runtimeVendor(jvmInfo.getVendor())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package datadog.trace.civisibility.config;

import datadog.trace.api.Config;
import datadog.trace.api.civisibility.CiVisibilityWellKnownTags;
import datadog.trace.civisibility.ipc.Serializer;
import java.nio.ByteBuffer;
import java.util.Objects;

public class JvmInfo {

public static final JvmInfo CURRENT_JVM =
new JvmInfo(
System.getProperty("java.runtime.name"),
System.getProperty("java.version"),
System.getProperty("java.vendor"));
public static final JvmInfo CURRENT_JVM;

static {
Config config = Config.get();
CiVisibilityWellKnownTags wellKnownTags = config.getCiVisibilityWellKnownTags();
CURRENT_JVM =
new JvmInfo(
wellKnownTags.getRuntimeName().toString(),
wellKnownTags.getRuntimeVersion().toString(),
wellKnownTags.getRuntimeVendor().toString());
}

private final String name;
private final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,34 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import datadog.trace.civisibility.config.JvmInfo;
import datadog.trace.util.Strings;
import java.util.Map;

public class TestDecoratorImpl implements TestDecorator {

private static final UTF8BytesString CIAPP_TEST_ORIGIN = UTF8BytesString.create("ciapp-test");

private final String component;
private final String sessionName;
private final Map<String, String> ciTags;

public TestDecoratorImpl(String component, Map<String, String> ciTags) {
public TestDecoratorImpl(
String component, String sessionName, String testCommand, Map<String, String> ciTags) {
this.component = component;
this.ciTags = ciTags;
if (Strings.isNotBlank(sessionName)) {
this.sessionName = sessionName;
} else {
String ciJobName = ciTags.get(Tags.CI_JOB_NAME);
this.sessionName =
Strings.isNotBlank(ciJobName) ? ciJobName + "-" + testCommand : testCommand;
}
}

protected String testType() {
return TEST_TYPE;
}

protected String runtimeName() {
return JvmInfo.CURRENT_JVM.getName();
}

protected String runtimeVendor() {
return JvmInfo.CURRENT_JVM.getVendor();
}

protected String runtimeVersion() {
return JvmInfo.CURRENT_JVM.getVersion();
}

protected String osArch() {
return System.getProperty("os.arch");
}

protected String osPlatform() {
return System.getProperty("os.name");
}

protected String osVersion() {
return System.getProperty("os.version");
}

protected UTF8BytesString origin() {
return CIAPP_TEST_ORIGIN;
}
Expand All @@ -59,24 +44,11 @@ public CharSequence component() {

@Override
public AgentSpan afterStart(final AgentSpan span) {
/*
* IMPORTANT: JVM and OS tags should match properties
* set in datadog.trace.civisibility.config.ExecutionSettingsFactory
*
* Moreover, logic that populates these tags cannot be changed,
* as they are used to establish correspondence between
* executions of the same test case
*/
span.setTag(Tags.TEST_TYPE, testType());
span.setSamplingPriority(PrioritySampling.SAMPLER_KEEP);
span.setTag(Tags.RUNTIME_NAME, runtimeName());
span.setTag(Tags.RUNTIME_VENDOR, runtimeVendor());
span.setTag(Tags.RUNTIME_VERSION, runtimeVersion());
span.setTag(Tags.OS_ARCHITECTURE, osArch());
span.setTag(Tags.OS_PLATFORM, osPlatform());
span.setTag(Tags.OS_VERSION, osVersion());
span.setTag(DDTags.ORIGIN_KEY, CIAPP_TEST_ORIGIN);
span.setTag(Tags.TEST_TYPE, testType());
span.setTag(Tags.COMPONENT, component());
span.setTag(Tags.TEST_SESSION_NAME, sessionName);

for (final Map.Entry<String, String> ciTag : ciTags.entrySet()) {
span.setTag(ciTag.getKey(), ciTag.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public <T extends CoverageCalculator> BuildSystemModuleImpl(
new BuildModuleSettings(
getPropertiesPropagatedToChildProcess(
moduleName,
startCommand,
classpath,
jacocoAgent,
signalServerAddress,
Expand All @@ -109,6 +110,7 @@ public <T extends CoverageCalculator> BuildSystemModuleImpl(

private Map<String, String> getPropertiesPropagatedToChildProcess(
String moduleName,
String startCommand,
@Nullable Collection<Path> classpath,
@Nullable JavaAgent jacocoAgent,
InetSocketAddress signalServerAddress,
Expand Down Expand Up @@ -172,6 +174,9 @@ private Map<String, String> getPropertiesPropagatedToChildProcess(
propagatedSystemProperties.put(
Strings.propertyNameToSystemPropertyName(CiVisibilityConfig.CIVISIBILITY_MODULE_NAME),
moduleName);
propagatedSystemProperties.put(
Strings.propertyNameToSystemPropertyName(CiVisibilityConfig.CIVISIBILITY_TEST_COMMAND),
startCommand);

propagatedSystemProperties.put(
Strings.propertyNameToSystemPropertyName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ class TestDecoratorImplTest extends Specification {

def "test afterStart"() {
setup:
def decorator = newDecorator()
def decorator = new TestDecoratorImpl("test-component", "session-name", "test-command", ["ci-tag-1": "value", "ci-tag-2": "another value"])

when:
decorator.afterStart(span)

then:
1 * span.setTag(Tags.TEST_SESSION_NAME, "session-name")
1 * span.setTag(Tags.COMPONENT, "test-component")
1 * span.setTag(Tags.TEST_TYPE, decorator.testType())
1 * span.setSamplingPriority(PrioritySampling.SAMPLER_KEEP)
1 * span.setTag(Tags.RUNTIME_NAME, decorator.runtimeName())
1 * span.setTag(Tags.RUNTIME_VENDOR, decorator.runtimeVendor())
1 * span.setTag(Tags.RUNTIME_VERSION, decorator.runtimeVersion())
1 * span.setTag(Tags.OS_ARCHITECTURE, decorator.osArch())
1 * span.setTag(Tags.OS_PLATFORM, decorator.osPlatform())
1 * span.setTag(Tags.OS_VERSION, decorator.osVersion())
1 * span.setTag(DDTags.ORIGIN_KEY, decorator.origin())
1 * span.setTag("ci-tag-1", "value")
1 * span.setTag("ci-tag-2", "another value")
Expand All @@ -40,7 +35,20 @@ class TestDecoratorImplTest extends Specification {
serviceName << ["test-service", "other-service", null]
}

static newDecorator() {
new TestDecoratorImpl("test-component", ["ci-tag-1": "value", "ci-tag-2": "another value"])
def "test session name: #sessionName, #ciJobName, #testCommand"() {
setup:
def decorator = new TestDecoratorImpl("test-component", sessionName, testCommand, [(Tags.CI_JOB_NAME): ciJobName])

when:
decorator.afterStart(span)

then:
1 * span.setTag(Tags.TEST_SESSION_NAME, expectedSessionName)

where:
sessionName | ciJobName | testCommand | expectedSessionName
"session-name" | "ci-job-name" | "test-command" | "session-name"
null | "ci-job-name" | "test-command" | "ci-job-name-test-command"
null | null | "test-command" | "test-command"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class TestImplTest extends DDSpecification {
def testFramework = TestFrameworkInstrumentation.OTHER
def config = Config.get()
def metricCollector = Stub(CiVisibilityMetricCollectorImpl)
def testDecorator = new TestDecoratorImpl("component", [:])
def testDecorator = new TestDecoratorImpl("component", "session-name", "test-command", [:])
def methodLinesResolver = { it -> MethodLinesResolver.MethodLines.EMPTY }
def codeowners = NoCodeowners.INSTANCE
new TestImpl(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ abstract class CiVisibilityInstrumentationTest extends AgentTestRunner {
TestFrameworkSession.Factory testFrameworkSessionFactory = (String projectName, String component, Long startTime) -> {
def config = Config.get()
def ciTags = [(DUMMY_CI_TAG): DUMMY_CI_TAG_VALUE]
TestDecorator testDecorator = new TestDecoratorImpl(component, ciTags)
TestDecorator testDecorator = new TestDecoratorImpl(component, "session-name", "test-command", ciTags)
return new HeadlessTestSession(
projectName,
startTime,
Expand All @@ -150,7 +150,7 @@ abstract class CiVisibilityInstrumentationTest extends AgentTestRunner {
BuildSystemSession.Factory buildSystemSessionFactory = (String projectName, Path projectRoot, String startCommand, String component, Long startTime) -> {
def config = Config.get()
def ciTags = [(DUMMY_CI_TAG): DUMMY_CI_TAG_VALUE]
TestDecorator testDecorator = new TestDecoratorImpl(component, ciTags)
TestDecorator testDecorator = new TestDecoratorImpl(component, "session-name", "test-command", ciTags)
ModuleSignalRouter moduleSignalRouter = new ModuleSignalRouter()
SignalServer signalServer = new SignalServer()
RepoIndexBuilder repoIndexBuilder = Stub(RepoIndexBuilder)
Expand Down Expand Up @@ -269,7 +269,7 @@ abstract class CiVisibilityInstrumentationTest extends AgentTestRunner {
}

def getEventsAsJson(List<List<DDSpan>> traces) {
return getSpansAsJson(new CiTestCycleMapperV1(Config.get().getWellKnownTags(), false), traces)
return getSpansAsJson(new CiTestCycleMapperV1(Config.get().getCiVisibilityWellKnownTags(), false), traces)
}

def getCoveragesAsJson(List<List<DDSpan>> traces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class CiVisibilityTestUtils {
path("content.meta.['error.message']"),
path("content.meta.library_version"),
path("content.meta.runtime-id"),
path("content.meta.['_dd.tracer_host']"),
// Different events might or might not have the same start or duration.
// Regardless, the values of these fields should be treated as different
path("content.start", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"files" : [ {
"filename" : "org/example/cucumber/calculator/basic_arithmetic.feature"
} ]
} ]
} ]
Loading

0 comments on commit 7108ba0

Please sign in to comment.