This product is not supported for your selected Datadog site. ().

互換性

サポート対象テストフレームワーク:

  • testing パッケージ
Bazel を使用して Go テストを実行する場合は、Go テスト用の Datadog Bazel ルールを使用してください。

報告方法の構成

Datadog にテスト結果を報告するには、Datadog Go ライブラリを構成する必要があります。

We support auto-instrumentation for the following CI providers:

CI ProviderAuto-Instrumentation method
GitHub ActionsDatadog Test Visibility Github Action
JenkinsUI-based configuration with Datadog Jenkins plugin
GitLabDatadog Test Visibility GitLab Script
CircleCIDatadog Test Visibility CircleCI Orb

Auto-instrumentation runs on the CI executor and does not automatically apply to tests in a separate container. Before using it for containerized tests, see Tests in Containers.

If the auto-instrumentation step configures the process that runs your tests, you can skip the rest of the setup steps below.

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:

Set these variables before starting the test process. For parallel test runners, set them on the parent process so every worker inherits them.

DD_CIVISIBILITY_AGENTLESS_ENABLED=true selects Agentless mode. DD_API_KEY provides authentication but does not enable Agentless mode.

DD_CIVISIBILITY_AGENTLESS_ENABLED=true (Required for Agentless mode)
Enables Agentless mode to send test results directly to Datadog.
Default: false
DD_API_KEY (Required for Agentless mode)
The Datadog API key used to authenticate test result uploads.
Default: (empty)

If you use a Datadog site other than US1, set the following variable:

DD_SITE (Optional for Agentless mode)
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.

Orchestrion のインストール

Orchestrion は、コンパイル時に Go ソースコードを処理し、dd-trace-go を使用して自動的にインスツルメンテーションを挿入するためのツールです。

以下のコマンドを使用して、https://github.com/datadog/orchestrion から orchestrion をインストールします。

$ go install github.com/DataDog/orchestrion@latest

以下のコマンドを実行することで、orchestrion をプロジェクト依存関係に自動的に追加できます。

$ orchestrion pin

このコマンドには以下の効果があります。

  1. 以下のような内容の新しい orchestrion.tool.go ファイルを作成します。

    // 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. go get github.com/DataDog/orchstrion@<current-release> を実行して、プロジェクトのバージョンが 現在使用されているものと一致していることを確認します。

  3. go mod tidy を実行して、go.mod および go.sum ファイルが最新であることを確認します。

実行しない場合でも、このコマンドは必要なときに自動的に実行されます。完了すると、このプロジェクトで使用される orchestrion のバージョンは、 他の依存関係を管理する場合と同様に、go.mod ファイルを使用して直接管理できます。

Orchestrion は、Go の 公式リリースポリシー に準拠して、Go の最新の 2 つのリリースをサポートします。それより前の Go リリースでも正しく動作する場合がありますが、Datadog は動作しない古いリリースをサポートできません。

さらに、Orchestrion がサポートするのは Go modules を使用するプロジェクトのみです。

テストのインスツルメンテーション

ライブラリを構成するため、テストプロセスを開始する前に以下の環境変数を設定します。並列テストランナーの場合、すべてのワーカーが継承するように親プロセスで設定してください。

DD_CIVISIBILITY_ENABLED=true(必須)
Test Optimization を有効にします。
デフォルト: false
DD_SERVICE (オプション)
テスト対象のサービスまたはライブラリの名前。
デフォルト: リポジトリ名
DD_ENV (オプション)
テストが実行されている環境の名前。
デフォルト: (empty)
例: local、ci
DD_CIVISIBILITY_AGENTLESS_ENABLED=true (Agentless モードの場合、必須)
Agentless モードを有効にして、テスト結果を Datadog に直接送信します。
デフォルト: false
DD_API_KEY (Agentless モードの場合、必須)
テスト結果のアップロードを認証するために使用される Datadog API キー。この変数で Agentless モードが有効になることはありません。
デフォルト: (empty)
DD_SITE (Agentless モードの場合、オプション)
テスト結果のアップロード先の Datadog サイト。US1 以外のサイトを使用する場合は、この構成を設定します。
デフォルト: datadoghq.com
DD_TRACE_AGENT_URL (Datadog Agent を使用する場合のみ)
トレース収集用の Datadog Agent URL。http://hostname:port の形式にします。
デフォルト: http://localhost:8126
DD_TEST_SESSION_NAME (オプション)
unit-tests、integration-tests、smoke-tests などのテストグループを識別します。
デフォルト: CI ジョブ名とテストコマンド、または CI ジョブ名が利用できない場合はテストコマンド。
例: unit-tests、integration-tests、smoke-tests

go test コマンドの前に orchestrion を付けてください。

$ orchestrion go test -race ./...

orchestrion pin を実行していない場合、orchestrion pin が自動的に実行されるため、次のようなメッセージが表示されることがあります:

  ╭──────────────────────────────────────────────────────────────────────────────╮
  │                                                                              │
  │  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.    │
  │                                                                              │
  ╰──────────────────────────────────────────────────────────────────────────────╯

代替手段

Orchestrion の核となるのは、標準 Go ツールチェーン -toolexec プロキシです。orchestrion go を使用する代わりに、 -toolexec 引数を、それを受け入れる go コマンドに手動で指定することもできます:

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

参考資料