Jenkins 또는 자체 관리형 GitLab CI와 같은 온프레미스 CI 공급자에서 테스트를 실행하는 경우, 에이전트 설치 지침에 따라 각 작업자 노드에 Datadog 에이전트를 설치합니다.
자동으로 테스트 결과를 로그와 기본 호트스 메트릭과 연결할 수 있기 때문에 이 옵션을 추천합니다.
쿠버네티스 실행기를 사용하는 경우 Datadog에서는 Datadog 연산자를 사용할 것을 권고합니다.
연산자에는 Datadog 허용 제어기가 포함되어 있어 빌드 파드에 자동으로 추적기 라이브러리를 삽입합니다.
참고: Datadog 연산자를 사용할 경우 허용 제어기가 작업을 해주기 때문에 추적기 라이브러리를 다운로드 받고 삽입할 필요가 없습니다. 따라서 아래 해당 단계를 건너뛰어도 됩니다.
그러나 테스트 가시화 기능을 사용할 때 필요한 파드의 환경 변수나 명령줄 파라미터는 설정해야 합니다.
쿠버네티스를 사용하지 않거나 Datadog 허용 제어기를 사용할 수 없고 CI 공급자가 컨테이너 기반 실행기를 사용하는 경우, 추적기를 실행하는 빌드 컨테이너에서 환경 변수 DD_TRACE_AGENT_URL(기본값 http://localhost:8126)를 해당 컨테이너 내에서 액세스할 수 있는 엔드포인트로 설정합니다. 참고: 빌드 내에서 localhost를 사용하면 기본 작업자 노드나 에이전트를 실행하는 컨테이너를 참조하지 않고 컨테이너 자체를 참조합니다.
DD_TRACE_AGENT_URL 은 프로토콜과 포트(예: http://localhost:8126)를 포함하고 DD_AGENT_HOST과 DD_TRACE_AGENT_PORT보다 우선하며, CI Visibility를 위해 Datadog 에이전트의 URL을 설정하는 데 권장되는 설정 파라미터입니다.
Datdog 에이전트에 연결하는 데 아직 문제가 있다면 에이전트리스 모드를 사용해 보세요.
참고: 이 방법을 사용할 경우 테스트가 로그 및 인프라스트럭처 메트릭과 상관 관계를 수립하지 않습니다.
Follow these steps if your CI Provider is not supported for auto-instrumentation (see Configuring reporting method).
Set the following environment variables to configure the tracer:
DD_CIVISIBILITY_ENABLED=true (Required)
Enables the Test Optimization product.
DD_TEST_SESSION_NAME
Use this to identify a group of tests (for example: unit-tests or integration-tests).
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 (Optional)
Name of the service or library being tested.
Prepend your test command with this datadog-ci CLI wrapper:
bundle exec ddcirb exec bundle exec rake test
Alternatively, set RUBYOPT environment variable to "-rbundler/setup -rdatadog/ci/auto_instrument" and don’t modify your test command.
Adding custom tags to tests
You can add custom tags to your tests by using the current active test:
require"datadog/ci"# inside your testDatadog::CI.active_test&.set_tag("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 Ruby custom instrumentation documentation.
Adding custom measures to tests
Like tags, you can add custom measures to your tests by using the current active test:
require"datadog/ci"# inside your testDatadog::CI.active_test&.set_metric("memory_allocations",16)# test continues normally# ...
The following is a list of the most important configuration settings that can be used with the test optimization library, either in code by using a Datadog.configure block, or using environment variables:
service
Name of the service or library under test. Environment variable: DD_SERVICE Default: $PROGRAM_NAME Example: my-ruby-app
env
Name of the environment where tests are being run. Environment variable: DD_ENV Default: none Examples: local, ci
It can be useful to have rich tracing information about your tests that includes time spent performing database operations or other external calls, as seen in the following flame graph:
To achieve this, configure additional instrumentation in your configure block:
ifENV["DD_ENV"]=="ci"Datadog.configuredo|c|# ... ci configs and instrumentation here ...c.tracing.instrument:redisc.tracing.instrument:pg# ... any other instrumentations supported by datadog gem ...endend
Alternatively, you can enable automatic APM instrumentation in test_helper/spec_helper:
테스트 중인 Git 브랜치입니다. 대신 태그 정보를 제공하는 경우 비워 둡니다. 예시: develop
DD_GIT_TAG
테스트 중인 Git 태그입니다(해당되는 경우). 대신 브랜치 정보를 제공하는 경우 비워 둡니다. 예시: 1.0.1
DD_GIT_COMMIT_SHA
전체 커밋 해시입니다. 예시: a18ebf361cc831f5535e58ec4fae04ffd98d8152
DD_GIT_COMMIT_MESSAGE
커밋 메시지입니다. 예시: Set release number
DD_GIT_COMMIT_AUTHOR_NAME
커밋 작성자 이름입니다. 예시: John Smith
DD_GIT_COMMIT_AUTHOR_EMAIL
커밋 작성자 이메일입니다. 예시: john@example.com
DD_GIT_COMMIT_AUTHOR_DATE
ISO 8601 형식의 커밋 작성자 날짜입니다. 예시: 2021-03-12T16:00:28Z
DD_GIT_COMMIT_COMMITTER_NAME
커밋 커미터 이름입니다. 예시: Jane Smith
DD_GIT_COMMIT_COMMITTER_EMAIL
커밋 커미터 이메일입니다. 예시: jane@example.com
DD_GIT_COMMIT_COMMITTER_DATE
ISO 8601 형식의 커밋 커미터 날짜입니다. 예시: 2021-03-12T16:00:28Z
Manually instrumenting your tests
Attention: when using manual instrumentation, run your tests like you normally do:
don't change `RUBYOPT` env variable and don't prepend `bundle exec ddcirb exec` to your test command
Auto-instrumentation adds additional performance overhead at the code loading stage. It can be especially noticeable for
large repositories with a lot of dependencies. If your project takes 20+ seconds to start, you are likely
to benefit from manually instrumenting your tests.
The RSpec integration traces all executions of example groups and examples when using the rspec test framework.
To activate your integration, add this to the spec_helper.rb file:
require"rspec"require"datadog/ci"# Only activates test instrumentation on CIifENV["DD_ENV"]=="ci"Datadog.configuredo|c|# enables test optimizationc.ci.enabled=true# The name of the service or library under testc.service="my-ruby-app"# Enables the RSpec instrumentationc.ci.instrument:rspecendend
Run your tests as you normally do, specifying the environment where tests are being run in the DD_ENV environment variable.
You could use the following environments:
local when running tests on a developer workstation
ci when running them on a CI provider
For example:
DD_ENV=ci bundle exec rake spec
The Minitest integration traces all executions of tests when using the minitest framework.
To activate your integration, add this to the test_helper.rb file:
require"minitest"require"datadog/ci"# Only activates test instrumentation on CIifENV["DD_ENV"]=="ci"Datadog.configuredo|c|# enables test optimizationc.ci.enabled=true# The name of the service or library under testc.service="my-ruby-app"c.ci.instrument:minitestendend
Run your tests as you normally do, specifying the environment where tests are being run in the DD_ENV environment variable.
You could use the following environments:
local when running tests on a developer workstation
ci when running them on a CI provider
For example:
DD_ENV=ci bundle exec rake test
Note: When using `minitest/autorun`, ensure that `datadog/ci` is required before `minitest/autorun`.
The Cucumber integration traces executions of scenarios and steps when using the cucumber framework.
To activate your integration, add the following code to your application:
require"cucumber"require"datadog/ci"# Only activates test instrumentation on CIifENV["DD_ENV"]=="ci"Datadog.configuredo|c|# enables test optimizationc.ci.enabled=true# The name of the service or library under testc.service="my-ruby-app"# Enables the Cucumber instrumentationc.ci.instrument:cucumberendend
Run your tests as you normally do, specifying the environment where tests are being run in the DD_ENV environment variable.
You could use the following environments:
local when running tests on a developer workstation
ci when running them on a CI provider
For example:
DD_ENV=ci bundle exec rake cucumber
Using library’s public API for unsupported test frameworks
If you use RSpec, Minitest, or Cucumber, do not use the manual testing API, as Test Optimization automatically instruments them and sends the test results to Datadog. The manual testing API is incompatible with already supported testing frameworks.
Use the manual testing API only if you use an unsupported testing framework or have a different testing mechanism.
Full public API documentation is available on YARD site.
Domain model
The API is based around four concepts: test session, test module, test suite, and test.
Test session
A test session represents a test command run.
To start a test session, call Datadog::CI.start_test_session and pass the Datadog service and tags (such as the test framework
you are using).
When all your tests have finished, call Datadog::CI::TestSession#finish, which closes the session and sends the session
trace to the backend.
Test module
A test module represents a smaller unit of work within a session.
For supported test frameworks, test module is always same as test session.
For your use case, this could be a package in your componentized application.
To start a test module, call Datadog::CI.start_test_module and pass the name of the module.
When the module run has finished, call Datadog::CI::TestModule#finish.
Test suite
A test suite comprises a set of tests that test similar functionality.
A single suite usually corresponds to a single file where tests are defined.
Create test suites by calling Datadog::CI#start_test_suite and passing the name of the test suite.
Call Datadog::CI::TestSuite#finish 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::CI#start_test or Datadog::CI.trace_test and passing the name of the test and name of the test suite. Test suite name must be the same as name of the test suite started in previous step.
Call Datadog::CI::Test#finish when a test has finished execution.
Code example
The following code represents example usage of the API:
Use DD_TEST_SESSION_NAME to define the name of the test session and the related group of tests. Examples of values for this tag would be:
unit-tests
integration-tests
smoke-tests
flaky-tests
ui-tests
backend-tests
If DD_TEST_SESSION_NAME is not specified, the default value used is a combination of the:
CI job name
Command used to run the tests (such as yarn test)
The test session name needs to be unique within a repository to help you distinguish different groups of tests.
When to use DD_TEST_SESSION_NAME
There’s a set of parameters that Datadog checks to establish correspondence between test sessions. The test command used to execute the tests is one of them. If the test command contains a string that changes for every execution, such as a temporary folder, Datadog considers the sessions to be unrelated to each other. For example:
yarn test --temp-dir=/var/folders/t1/rs2htfh55mz9px2j4prmpg_c0000gq/T