Test Optimization is not available in the selected site () at this time.

Join the Preview!

Test optimization for Go is in Preview.

Compatibility

Supported test frameworks:

  • testing package

Configuring reporting method

To report test results to Datadog, you need to configure the Datadog Go library:

If you are using a cloud CI provider without access to the underlying worker nodes, such as GitHub Actions or CircleCI, configure the library to use the Agentless mode. For this, set the following environment variables:

DD_CIVISIBILITY_AGENTLESS_ENABLED=true (Required)
Enables or disables Agentless mode.
Default: false
DD_API_KEY (Required)
The Datadog API key used to upload the test results.
Default: (empty)

Additionally, configure the Datadog site to which you want to send data.

DD_SITE (Required)
The Datadog site to upload results to.
Default: datadoghq.com

If you are running tests on an on-premises CI provider, such as Jenkins or self-managed GitLab CI, install the Datadog Agent on each worker node by following the Agent installation instructions. This is the recommended option as it allows you to automatically link test results to logs and underlying host metrics.

If you are using a Kubernetes executor, Datadog recommends using the Datadog Operator. The operator includes Datadog Admission Controller which can automatically inject the tracer library into the build pods. Note: If you use the Datadog Operator, there is no need to download and inject the tracer library since the Admission Controller can do this for you, so you can skip the corresponding step below. However, you still need to make sure that your pods set the environment variables or command-line parameters necessary to enable Test Visibility.

If you are not using Kubernetes or can’t use the Datadog Admission Controller and the CI provider is using a container-based executor, set the DD_TRACE_AGENT_URL environment variable (which defaults to http://localhost:8126) in the build container running the tracer to an endpoint that is accessible from within that container. Note: Using localhost inside the build references the container itself and not the underlying worker node or any container where the Agent might be running in.

DD_TRACE_AGENT_URL includes the protocol and port (for example, http://localhost:8126) and takes precedence over DD_AGENT_HOST and DD_TRACE_AGENT_PORT, and is the recommended configuration parameter to configure the Datadog Agent’s URL for CI Visibility.

If you still have issues connecting to the Datadog Agent, use the Agentless Mode. Note: When using this method, tests are not correlated with logs and infrastructure metrics.

Installing Orchestrion

Orchestrion is a tool to process Go source code at compilation time and automatically insert instrumentation using dd-trace-go.

Install orchestrion from https://github.com/datadog/orchestrion using the command:

$ go install github.com/DataDog/orchestrion@latest
  • Optional: project go.mod registration

    You can automatically add orchestrion to your project’s dependencies by running:

    $ orchestrion pin
    

    This command has the following effects:

    1. Create a new orchestrion.tool.go file containing content similar to:
      // Code generated by `orchestrion pin`; DO NOT EDIT.
      
      // This file is generated by `orchestrion pin`, and is used to include a blank import of the
      // orchestrion package(s) so that `go mod tidy` does not remove the requirements rom go.mod.
      // This file should be checked into source control.
      
      //go:build tools
      
      package tools
      
      import _ "github.com/DataDog/orchestrion"
      
    2. Run go get github.com/DataDog/orchstrion@<current-release> to make sure the project version corresponds to the one currently being used
    3. Run go mod tidy to make sure your go.mod and go.sum files are up-to-date

    If you do not run this command, it is done automatically when required. Once done, the version of orchestrion used by this project can be controlled directly using the go.mod file, as you would control any other dependency.

Orchestrion supports the two latest releases of Go, matching Go’s official release policy. It may function correctly with older Go releases, but Datadog cannot support older releases that do not work.

In addition to this, Orchestrion only supports projects using Go modules.

Instrumenting tests

Set the following environment variables to configure the library:

DD_CIVISIBILITY_ENABLED=true (Required)
Enables the Test Optimization product.
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).

Prefix your go test command with orchestrion:

$ orchestrion go test -race ./...

If you have not run orchestrion pin, you may see a message similar to the following appear, as orchestrion pin is automatically executed:

  ╭──────────────────────────────────────────────────────────────────────────────╮
                                                                                
    Warning: github.com/DataDog/orchestrion is not present in your go.mod       
    file.                                                                       
    In order to ensure build reliability and reproductibility, orchestrion      
    will now add itself in your go.mod file by:                                 
                                                                                
        1. creating a new file named orchestrion.tool.go                        
        2. running go get github.com/DataDog/orchestrion@v0.9.4                 
        3. running go mod tidy                                                  
                                                                                
    You should commit the resulting changes into your source control system.    
                                                                                
  ╰──────────────────────────────────────────────────────────────────────────────╯

Alternative

Orchestrion at the core is a standard Go toolchain -toolexec proxy. Instead of using orchestrion go, you can also manually provide the -toolexec argument to go commands that accept it:

$ go build -toolexec 'orchestrion toolexec' .
$ go test -toolexec 'orchestrion toolexec' -race .

Further reading