Run your tests as you normally do (for example: mvn test or mvn verify).
Make sure to set the DD_TRACER_FOLDER variable to the path where you have downloaded the tracer.
Run your tests using the org.gradle.jvmargs system property to specify the path to the Datadog Java Tracer JAR.
When specifying tracer arguments, include the following:
Enable CI visibility by setting the dd.civisibility.enabled property to true.
Define the environment where the tests are being run using the dd.env property (for example: local when running tests on a developer workstation or ci when running them on a CI provider).
Define the name of the service or library being tested in the dd.service property.
For example:
./gradlew cleanTest test -Dorg.gradle.jvmargs=\
-javaagent:$DD_TRACER_FOLDER/dd-java-agent.jar=\
dd.civisibility.enabled=true,\
dd.env=ci,\
dd.service=my-java-app
Specifying org.gradle.jvmargs in the command line overrides the value specified elsewhere. If you have this property specified in a gradle.properties file, be sure to replicate the necessary settings in the command line invocation.
Set the following environment variables to configure the tracer:
DD_CIVISIBILITY_ENABLED=true (Required)
Enables Test Optimization.
DD_ENV (Required)
Environment where the tests are being run (for example: local when running tests on a developer workstation or ci when running them on a CI provider).
DD_SERVICE (Required)
Name of the service or library being tested.
DD_TRACER_FOLDER (Required)
Path to the folder where the downloaded Java Tracer is located.
ISO 8601 形式のコミットのコミッターの日付。 例: 2021-03-12T16:00:28Z
Extensions
The tracer exposes a set of APIs that can be used to extend its functionality programmatically.
Adding custom tags to tests
To add custom tags include opentracing-util library as a compile-time dependency to your project.
You can then add custom tags to your tests by using the active span:
importio.opentracing.Span;importio.opentracing.util.GlobalTracer;// ...// inside your testfinalSpanspan=GlobalTracer.get().activeSpan();if(span!=null){span.setTag("test_owner","my_team");}// test continues normally// ...
To create filters or group by fields for these tags, you must first create facets.
For more information about adding tags, see the Adding Tags section of the Java custom instrumentation documentation.
Adding custom measures to tests
Just like tags, you can add custom measures to your tests by using the current active span:
importio.opentracing.Span;importio.opentracing.util.GlobalTracer;// ...// inside your testfinalSpanspan=GlobalTracer.get().activeSpan();if(span!=null){span.setTag("test.memory.usage",1e8);}// test continues normally// ...
If you use one of the supported testing frameworks, the Java Tracer automatically instruments your tests and sends the results to the Datadog backend.
If you are using a framework that is not supported, or an ad-hoc testing solution, you can harness the manual testing API, which also reports test results to the backend.
To use the manual testing API, add the dd-trace-api library as a compile-time dependency to your project.
Domain model
The API is based around four concepts: test session, test module, test suite, and test.
Test session
A test session represents a project build, which typically corresponds to execution of a test command issued by a user or by a CI script.
To start a test session, call datadog.trace.api.civisibility.CIVisibility#startSession and pass the name of the project and the name of the testing framework you used.
When all your tests have finished, call datadog.trace.api.civisibility.DDTestSession#end, which forces the library to send all remaining test results to the backend.
Test module
A test module represents a smaller unit of work within a project build, typically corresponding to a project module. For example, a Maven submodule or Gradle subproject.
To start a test mode, call datadog.trace.api.civisibility.DDTestSession#testModuleStart and pass the name of the module.
When the module has finished building and testing, call datadog.trace.api.civisibility.DDTestModule#end.
Test Suite
A test suite comprises a set of tests that share common functionality.
They can share a common initialization and teardown, and can also share some variables.
A single suite usually corresponds to a Java class that contains test cases.
Create test suites in a test module by calling datadog.trace.api.civisibility.DDTestModule#testSuiteStart and passing the name of the test suite.
Call datadog.trace.api.civisibility.DDTestSuite#end when all the related tests in the suite have finished their execution.
Test
A test represents a single test case that is executed as part of a test suite.
Usually it corresponds to a method that contains testing logic.
Create tests in a suite by calling datadog.trace.api.civisibility.DDTestSuite#testStart and passing the name of the test.
Call datadog.trace.api.civisibility.DDTest#end when a test has finished execution.
Code Example
The following code represents a simple usage of the API:
packagecom.datadog.civisibility.example;importdatadog.trace.api.civisibility.CIVisibility;importdatadog.trace.api.civisibility.DDTest;importdatadog.trace.api.civisibility.DDTestModule;importdatadog.trace.api.civisibility.DDTestSession;importdatadog.trace.api.civisibility.DDTestSuite;importjava.lang.reflect.Method;// the null arguments in the calls below are optional startTime/endTime values:// when they are not specified, current time is usedpublicclassManualTest{publicstaticvoidmain(String[]args)throwsException{DDTestSessiontestSession=CIVisibility.startSession("my-project-name","my-test-framework",null);testSession.setTag("my-tag","additional-session-metadata");try{runTestModule(testSession);}finally{testSession.end(null);}}privatestaticvoidrunTestModule(DDTestSessiontestSession)throwsException{DDTestModuletestModule=testSession.testModuleStart("my-module",null);testModule.setTag("my-module-tag","additional-module-metadata");try{runFirstTestSuite(testModule);runSecondTestSuite(testModule);}finally{testModule.end(null);}}privatestaticvoidrunFirstTestSuite(DDTestModuletestModule)throwsException{DDTestSuitetestSuite=testModule.testSuiteStart("my-suite",ManualTest.class,null);testSuite.setTag("my-suite-tag","additional-suite-metadata");try{runTestCase(testSuite);}finally{testSuite.end(null);}}privatestaticvoidrunTestCase(DDTestSuitetestSuite)throwsException{MethodmyTestCaseMethod=ManualTest.class.getDeclaredMethod("myTestCase");DDTestddTest=testSuite.testStart("myTestCase",myTestCaseMethod,null);ddTest.setTag("my-test-case-tag","additional-test-case-metadata");ddTest.setTag("my-test-case-tag","more-test-case-metadata");try{myTestCase();}catch(Exceptione){ddTest.setErrorInfo(e);// pass error info to mark test case as failed}finally{ddTest.end(null);}}privatestaticvoidmyTestCase()throwsException{// run some test logic}privatestaticvoidrunSecondTestSuite(DDTestModuletestModule){DDTestSuitesecondTestSuite=testModule.testSuiteStart("my-second-suite",ManualTest.class,null);secondTestSuite.setSkipReason("this test suite is skipped");// pass skip reason to mark test suite as skippedsecondTestSuite.end(null);}}
Always call datadog.trace.api.civisibility.DDTestSession#end at the end so that all the test info is flushed to Datadog.
Best practices
Deterministic test parameters representation
Test Optimization works best when the test parameters are deterministic and stay the same between test runs.
If a test case has a parameter that varies between test executions (such as a current date, a random number, or an instance of a class whose toString() method is not overridden), some of the product features may not work as expected.
For example, the history of executions may not be available, or the test case may not be classified as flaky even if it exhibits flakiness.
The best way to fix this is to make sure that the test parameters are the same between test runs.
In JUnit 5, this can also be addressed by customizing the string representation of the test parameters without changing their values.
To do so, use org.junit.jupiter.api.Named interface or change the name parameter of the org.junit.jupiter.params.ParameterizedTest annotation:
@ParameterizedTest@MethodSource("namedArguments")voidparameterizedTest(Strings,Dated){// The second parameter in this test case is non-deterministic.// In the argument provider method it is wrapped with Named to ensure it has a deterministic name.}staticStream<Arguments>namedArguments(){returnStream.of(Arguments.of("a string",Named.of("current date",newDate())),Arguments.of("another string",Named.of("a date in the future",newDate(System.currentTimeMillis()+TimeUnit.DAYS.toMillis(1)))));}
@ParameterizedTest(name="[{index}] {0}, a random number from one to ten")@MethodSource("randomArguments")voidanotherParameterizedTest(Strings,inti){// The second parameter in this test case is non-deterministic.// The name of the parameterized test is customized to ensure it has a deterministic name.}staticStream<Arguments>randomArguments(){returnStream.of(Arguments.of("a string",ThreadLocalRandom.current().nextInt(10)+1),Arguments.of("another string",ThreadLocalRandom.current().nextInt(10)+1));}
Troubleshooting
The tests are not appearing in Datadog after enabling CI Visibility in the tracer
Verify that the tracer is injected into your build process by examining your build’s logs.
If the injection is successful, you can see a line containing DATADOG TRACER CONFIGURATION.
If the line is not there, make sure that the environment variables used to inject and configure the tracer are available to the build process.
A common mistake is to set the variables in a build step and run the tests in another build step. This approach may not work if the variables are not propagated between build steps.
Ensure that you are using the latest version of the tracer.
Ensure that the dd.civisibility.enabled property (or DD_CIVISIBILITY_ENABLED environment variable) is set to true in the tracer arguments.
Try running your build with tracer debug logging enabled by setting the DD_TRACE_DEBUG environment variable to true.
Check the build output for any errors that indicate tracer misconfiguration, such as an unset DD_API_KEY environment variable.
Tests or source code compilation fails when building a project with the tracer attached
By default, CI Visibility runs Java code compilation with a compiler plugin attached.
The plugin is optional, as it only serves to reduce the performance overhead.
Depending on the build configuration, adding the plugin can sometimes disrupt the compilation process.
If the plugin interferes with the build, disable it by adding dd.civisibility.compiler.plugin.auto.configuration.enabled=false to the list of -javaagent arguments
(or by setting DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED=false environment variable).
Builds fails because dd-javac-plugin-client artifact cannot be found
It is possible that the Java compiler plugin injected into the build is not available if the build uses a custom artifactory storage or if it is run in offline mode.
If this is the case, you can disable plugin injection by adding dd.civisibility.compiler.plugin.auto.configuration.enabled=false to the list of -javaagent arguments
(or by setting the DD_CIVISIBILITY_COMPILER_PLUGIN_AUTO_CONFIGURATION_ENABLED environment variable to false).
The plugin is optional, as it only serves to reduce the performance overhead.
Tests fail when building a project with the tracer attached
In some cases attaching the tracer can break tests, especially if they run asserts on the internal state of the JVM or instances of third-party libraries’ classes.
While the best approach is such cases is to update the tests, there is also a quicker option of disabling the tracer’s third-party library integrations.
The integrations provide additional insights into what happens in the tested code and are especially useful in integration tests, to monitor things like HTTP requests or database calls.
They are enabled by default.
To disable a specific integration, refer to the Datadog Tracer Compatibility table for the relevant configuration property names.
For example, to disable OkHttp3 client request integration, add dd.integration.okhttp-3.enabled=false to the list of -javaagent arguments.
To disable all integrations, augment the list of -javaagent arguments with dd.trace.enabled=false (or set DD_TRACE_ENABLED=false environment variable).