- 重要な情報
- はじめに
- 用語集
- ガイド
- エージェント
- インテグレーション
- OpenTelemetry
- 開発者
- API
- CoScreen
- アプリ内
- Service Management
- インフラストラクチャー
- アプリケーションパフォーマンス
- 継続的インテグレーション
- ログ管理
- セキュリティ
- UX モニタリング
- 管理
サポート対象言語:
言語 | バージョン |
---|---|
Python 2 | >= 2.7 |
Python 3 | >= 3.6 |
サポートされているテストフレームワーク:
Test Framework | バージョン |
---|---|
pytest | >= 3.0.0 |
pytest-benchmark | >= 3.1.0 |
unittest | >= 3.7 |
Datadog にテスト結果を報告するには、Datadog Python ライブラリを構成する必要があります。
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.
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)false
DD_API_KEY
(Required)(empty)
Additionally, configure the Datadog site to which you want to send data.
DD_SITE
(Required)datadoghq.com
次のコマンドを実行して、Python トレーサーをインストールします。
pip install -U ddtrace
詳細については、Python トレーサーのインストールドキュメントを参照してください。
pytest
テストのインスツルメンテーションを有効にするには、pytest
の実行時に --ddtrace
オプションを追加し、DD_SERVICE
環境変数でテスト対象のサービスまたはライブラリの名前を、DD_ENV
環境変数でテストが実行されている環境を指定します (たとえば、開発者ワークステーションでテストを実行する場合は local
、CI プロバイダーでテストを実行する場合は ci
)。
DD_SERVICE=my-python-app DD_ENV=ci pytest --ddtrace
もし、残りの APM インテグレーションも有効にして flamegraph でより多くの情報を取得したい場合は、--ddtrace-patch-all
オプションを追加します。
DD_SERVICE=my-python-app DD_ENV=ci pytest --ddtrace --ddtrace-patch-all
pytest-benchmark
でベンチマークテストをインスツルメンテーションするには、pytest
実行時に —ddtrace
オプションを付けてベンチマークテストを実行すると、Datadog は自動的に pytest-benchmark
からメトリクスを検出します。
def square_value(value):
return value * value
def test_square_value(benchmark):
result = benchmark(square_value, 5)
assert result == 25
unittest
テストのインスツルメンテーションを有効にするには、unittest
コマンドの冒頭に ddtrace-run
を追加してテストを実行します。
DD_SERVICE
環境変数において、テスト対象のサービスまたはライブラリの名前を必ず指定してください。
また、DD_ENV
環境変数で、テストが実行されている環境を宣言することもできます。
DD_SERVICE=my-python-app DD_ENV=ci ddtrace-run python -m unittest
あるいは、unittest
インスツルメンテーションを手動で有効にしたい場合は、patch()
を使ってインテグレーションを有効にしてください。
from ddtrace import patch
import unittest
patch(unittest=True)
class MyTest(unittest.TestCase):
def test_will_pass(self):
assert True
unittest
テスト実行が並列で実行される場合、場合によっては、インスツルメンテーションが損なわれ、テストの可視性に影響を及ぼす可能性があります。
Datadog では、テストの可視性に影響が出ないよう、一度に使用するプロセスを 1 つに制限することを推奨しています。
テストの引数に ddspan
と宣言することで、テストにカスタムタグを追加することができます。
from ddtrace import tracer
# テストの引数として `ddspan` を宣言します
def test_simple_case(ddspan):
# タグを設定します
ddspan.set_tag("test_owner", "my_team")
# テストは正常に続きます
# ...
これらのタグに対して、フィルターや group by
フィールドを作成するには、まずファセットを作成する必要があります。タグの追加についての詳細は、Python カスタムインスツルメンテーションドキュメントのタグの追加セクションを参照してください。
タグと同様に、現在アクティブなスパンを使用して、テストにカスタムメトリクスを追加できます。
from ddtrace import tracer
# テストの引数として `ddspan` を宣言します
def test_simple_case(ddspan):
# タグを設定します
ddspan.set_tag("memory_allocations", 16)
# テストは正常に続きます
# ...
カスタムメトリクスについては、カスタムメトリクスの追加ガイドを参照してください。
以下は、コードか、または環境変数を使用した、トレーサーで使用できる最も重要なコンフィギュレーション設定のリストです。
ddtrace.config.service
DD_SERVICE
pytest
my-python-app
ddtrace.config.env
DD_ENV
none
local
、ci
次の環境変数を使用して、Datadog Agent の場所を構成できます。
DD_TRACE_AGENT_URL
http://hostname:port
の形式のトレース収集用の Datadog Agent URL。http://localhost:8126
他のすべての Datadog トレーサー構成オプションも使用できます。
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
git@github.com:MyCompany/MyApp.git
, https://github.com/MyCompany/MyApp.git
DD_GIT_BRANCH
develop
DD_GIT_TAG
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
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
2021-03-12T16:00:28Z
お役に立つドキュメント、リンクや記事: