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:
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.
it('sum function can sum',()=>{consttestSpan=require('dd-trace').scope().active()testSpan.setTag('team_owner','my_team')// test continues normally
// ...
})
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加の詳細については、Node.js カスタムインスツルメンテーションドキュメントの タグの追加 セクションを参照してください。
テストへのカスタム測定値の追加
タグと同様に、現在アクティブなスパンを使用して、テストにカスタム測定値を追加できます。
it('sum function can sum',()=>{consttestSpan=require('dd-trace').scope().active()testSpan.setTag('memory_allocations',16)// test continues normally
// ...
})
When('the function is called',function(){conststepSpan=require('dd-trace').scope().active()testSpan.setTag('team_owner','my_team')// test continues normally
// ...
})
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加の詳細については、Node.js カスタムインスツルメンテーションドキュメントの タグの追加 セクションを参照してください。
テストへのカスタム測定値の追加
現在アクティブなスパンをつかんで、テストにカスタム測定値を追加することもできます。
When('the function is called',function(){conststepSpan=require('dd-trace').scope().active()testSpan.setTag('memory_allocations',16)// test continues normally
// ...
})
// Your code can be before this line
// require('./commands')
require('dd-trace/ci/cypress/support')// Also supported:
// import 'dd-trace/ci/cypress/support'
// Your code can also be after this line
// Cypress.Commands.add('login', (email, pw) => {})
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){// your previous code is before this line
returnrequire('dd-trace/ci/cypress/plugin')(on,config)}}})
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){require('dd-trace/ci/cypress/plugin')(on,config)// other plugins
on('after:run',(details)=>{// other 'after:run' handlers
// important that this function call is returned
returnrequire('dd-trace/ci/cypress/after-run')(details)})}}})
const{defineConfig}=require('cypress')module.exports=defineConfig({e2e:{setupNodeEvents(on,config){require('dd-trace/ci/cypress/plugin')(on,config)// other plugins
on('after:spec',(...args)=>{// other 'after:spec' handlers
// Important that this function call is returned
// Important that all the arguments are passed
returnrequire('dd-trace/ci/cypress/after-spec')(...args)})}}})
beforeEach(()=>{cy.task('dd:addTags',{'before.each':'certain.information'})})it('renders a hello world',()=>{cy.task('dd:addTags',{'team.owner':'ui'})cy.get('.hello-world').should('have.text','Hello World')})
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加の詳細については、Node.js カスタムインスツルメンテーションドキュメントの タグの追加 セクションを参照してください。
有効にすると、Test Optimization はテストが失敗したときに Cypress がキャプチャしたスクリーンショットをアップロードします。これらは Test Optimization テスト詳細サイドパネルの Media タブに表示されます。これらを使用して、失敗時のブラウザの状態を調査します。
importtracerfrom'dd-trace'import{expect,test}from'vitest'test('sum function can sum',()=>{consttestSpan=tracer.scope().active()testSpan.setTag('team_owner','my_team')expect(1+2).toBe(3)})
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加の詳細については、Node.js カスタムインスツルメンテーションドキュメントの タグの追加 セクションを参照してください。
現在アクティブなスパンを使用して、テストにカスタム測定値を追加することもできます。
importtracerfrom'dd-trace'import{expect,test}from'vitest'test('sum function can sum',()=>{consttestSpan=tracer.scope().active()testSpan.setTag('memory_allocations',16)expect(1+2).toBe(3)})
{"scripts":{"test:e2e":"NODE_OPTIONS=\"--max-old-space-size=12288 ${NODE_OPTIONS:-}\" wdio run ./wdio.conf.js"}}
テストへのカスタムタグまたは測定値の追加
現在アクティブなスパンを使用して、テストにカスタムタグを追加することができます。
importtracerfrom'dd-trace'describe('home page',()=>{it('displays the heading',async()=>{consttestSpan=tracer.scope().active()testSpan.setTag('team_owner','my_team')awaitbrowser.url('/')awaitexpect($('h1')).toBeDisplayed()})})
これらのタグに対してフィルターや group by フィールドを作成するには、まずファセットを作成する必要があります。タグの追加の詳細については、Node.js カスタムインスツルメンテーションドキュメントの タグの追加 セクションを参照してください。
現在アクティブなスパンを使用して、テストにカスタム測定値を追加することもできます。
importtracerfrom'dd-trace'describe('home page',()=>{it('displays the heading',async()=>{consttestSpan=tracer.scope().active()testSpan.setTag('memory_allocations',16)awaitbrowser.url('/')awaitexpect($('h1')).toBeDisplayed()})})
Datadog uses Git information for visualizing your test results and grouping them by repository, branch, and commit. Git metadata is automatically collected by the test instrumentation from CI provider environment variables and the local .git folder in the project path, if available.
If you are running tests in non-supported CI providers or with no .git folder, you can set the Git information manually using environment variables. These environment variables take precedence over any auto-detected information. Set the following environment variables to provide Git information:
DD_GIT_REPOSITORY_URL
URL of the repository where the code is stored. Both HTTP and SSH URLs are supported. Example: git@github.com:MyCompany/MyApp.git, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
Git branch being tested. Leave empty if providing tag information instead. Example: develop
DD_GIT_TAG
Git tag being tested (if applicable). Leave empty if providing branch information instead. Example: 1.0.1
DD_GIT_COMMIT_SHA
Full commit hash. Example: a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
Commit message. Example: Set release number
DD_GIT_COMMIT_AUTHOR_NAME
Commit author name. Example: John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
Commit author email. Example: john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
Commit author date in ISO 8601 format. Example: 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
Commit committer name. Example: Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
Commit committer email. Example: jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
Commit committer date in ISO 8601 format. Example: 2021-03-12T16:00:28Z
手動テスト API
注: 手動テスト API は、 dd-trace バージョン 5.23.0 および 4.47.0から利用可能です。
Jest、Mocha、Cypress、Playwright、Cucumber、Vitest、または WebdriverIO を使用している場合は、手動テスト API を使用しないでください。Test Optimization は、これらのフレームワークに対して自動的にインスツルメンテーションを行い、テスト結果を Datadog に送信します。手動テスト API は、サポートされているテストフレームワークと互換性がありません。
サポートされていないテストフレームワークを使用している場合や、別のテストメカニズムを持っている場合のみ、手動テスト API を使用してください。
手動テスト API は、Node.js の node:diagnostics_channel モジュールを活用し、以下に公開可能なチャンネルに基づいています。
const{channel}=require('node:diagnostics_channel')const{describe,test,beforeEach,afterEach,assert}=require('my-custom-test-framework')consttestStartCh=channel('dd-trace:ci:manual:test:start')consttestFinishCh=channel('dd-trace:ci:manual:test:finish')consttestSuite=__filenamedescribe('can run tests',()=>{beforeEach((testName)=>{testStartCh.publish({testName,testSuite})})afterEach((status,error)=>{testFinishCh.publish({status,error})})test('first test will pass',()=>{assert.equal(1,1)})})
テスト開始チャンネル
このチャンネルを ID dd-trace:ci:manual:test:start で取得して、テストが開始されることを公開します。これを行うのに適した場所は、beforeEach フックなどです。
const{channel}=require('node:diagnostics_channel')consttestStartCh=channel('dd-trace:ci:manual:test:start')// ... code for your testing framework goes here
beforeEach(()=>{consttestDefinition={testName:'a-string-that-identifies-this-test',testSuite:'what-suite-this-test-is-from.js'}testStartCh.publish(testDefinition)})// code for your testing framework continues here ...
このチャンネルを ID dd-trace:ci:manual:test:finish で取得して、テストが終了されることを公開します。これを行うのに適した場所は、afterEach フックなどです。
const{channel}=require('node:diagnostics_channel')consttestFinishCh=channel('dd-trace:ci:manual:test:finish')// ... code for your testing framework goes here
afterEach(()=>{consttestStatusPayload={status:'fail',error: newError('assertion error')}testStartCh.publish(testStatusPayload)})// code for your testing framework continues here ...
このチャンネルを ID dd-trace:ci:manual:test:addTags で取得して、テストにカスタムタグが必要であることを公開します。これはテスト関数内で行うことができます。
const{channel}=require('node:diagnostics_channel')consttestAddTagsCh=channel('dd-trace:ci:manual:test:addTags')// ... code for your testing framework goes here
test('can sum',()=>{testAddTagsCh.publish({'test.owner':'my-team','number.assertions':3})constresult=sum(2,1)assert.equal(result,3)})// code for your testing framework continues here ...
Cypress の テスト分離 は、
再試行ベースの Test Optimization 機能が動作するために有効 (デフォルト) になっている必要があります。testIsolation が
false に設定されている場合、を Cypress 設定に指定すると、dd-trace はすべてのテストの
再試行 Early Flake Detection、Auto Test Retries、および
attempt to fix を無効にします。これらの機能は各テストをその場で再実行するため、分離が必要となるからです。
分離が無効になっている場合、トレーサーは警告 Test isolation is disabled, retries will not be enabled を記録し、テスト実行に
@test.test_management.is_attempt_to_fix タグは付けられません。トレーサーはグローバル
testIsolation 値を読み取るため、スイートごとの describe オーバーライドでは再試行は再有効化されません。
Jest の --forceExit
Jest の –forceExit オプションはデータ損失を引き起こす可能性があります。Datadog はテスト終了直後にデータを送信しようとしますが、プロセスを突然シャットダウンすると一部のリクエストが失敗する可能性があります。--forceExit は注意して使用してください。