概要

Multistep API テストは、一度に複数の HTTP リクエストまたは gRPC リクエストを連鎖させ、主要サービスにおける複雑な操作フローがいつでもどこでも利用可能であることを、プロアクティブに監視・確認できるようにするものです。サービスに対して単一のリクエストを実行したい場合は、API テストを使用してください。

以下を実現できます。

  • 認証を必要とする API エンドポイントで HTTP リクエストを実行します (たとえば、トークンを介して)
  • API レベルで主要なビジネストランザクションを監視します
  • エンドツーエンドのモバイルアプリケーションのジャーニーをシミュレートします
マルチステップ API テストの複数のテストステップ

サービスの 1 つが応答遅延を起こしたり、予期しない方法 (たとえば、予期しない応答本文やステータスコード) で応答を開始した場合、テストはチームに警告するCI パイプラインをブロックする、または障害のあるデプロイをロールバックすることができます。

Multistep API テストは、Datadog 管理ロケーションおよびプライベートロケーションから実行できるため、外部と内部の両方でシステムを完全にカバーできます。

構成

テストに名前を付けてタグを付ける

  1. Multistep API テストに名前を付けます。
  2. Multistep API テストに env タグおよび他のタグを追加します。これらのタグを使用して、Synthetic Monitoring & Continuous Testing ページで Synthetic テストをフィルタリングできます。

ロケーションを選択する

Multistep API テストのロケーションを選択します。Multistep API テストは、ネットワークの外部または内部のどちらからテストを実行するかの好みによって、管理ロケーションとプライベートロケーションの両方から実行できます。

Datadog’s out-of-the-box managed locations allow you to test public-facing websites and endpoints from regions where your customers are located.

AWS:

AmericasAsia PacificEMEA
Canada CentralHong KongBahrain
Northern CaliforniaJakartaCape Town
Northern VirginiaMumbaiFrankfurt
OhioOsakaIreland
OregonSeoulLondon
São PauloSingaporeMilan
SydneyParis
TokyoStockholm

GCP:

AmericasAsia PacificEMEA
DallasTokyoFrankfurt
Los Angeles
Oregon
Virginia

Azure:

RegionLocation
AmericasVirginia

The Datadog for Government site (US1-FED) uses the following managed location:

RegionLocation
AmericasUS-West

ステップを定義する

API リクエストのステップを作成するには、Create Your First Step をクリックします。

Multistep API テストリクエストを作成する

デフォルトでは、最大 10 個のテストステップを作成することができます。この制限を増やすには、Datadog サポートに連絡してください。

リクエストを定義する

  1. ステップに名前を付けます。

  2. リクエストタイプとして HTTP または gRPC を選択します。

    HTTP リクエストを作成し、アサーションを追加するには、 HTTP テストのドキュメントを参照してください。アサーションはマルチステップ API テストでは任意です。

    gRPC リクエストを作成し、動作チェックまたはヘルスチェックのためのアサーションを追加するには、 gRPC テストのドキュメントを参照してください。アサーションはマルチステップ API テストでは任意です。

実行設定を追加

Execution Settings の下で、以下のオプションが利用できます。

ステップの成功:

ステップが成功した後に後続のステップを続行するには、 If step succeeds, continue to next step をクリックします。

ステップの成功オプション 'continue to next step' を示す実行設定のスクリーンショット

ステップが成功した後にテストを終了するには、 If step succeeds, exit test and mark it as passed をクリックします。これにより、不要なステップの実行を防ぎ、テストが失敗としてマークされるのを回避します。

ステップの成功で 'exit and mark as passed' を示す実行設定のスクリーンショット

ステップの失敗

ステップが失敗した場合に後続のステップを実行するには、If step fails, continue to next step をクリックします。これは、後続のステップを実行したい場合のクリーンアップタスクに役立ちます。例えば、テストがリソースを作成し、そのリソースに対していくつかの操作を行い、最後にそのリソースを削除する場合などです。

中間ステップの 1 つが失敗した場合、テスト終了時にリソースが削除され、誤検出が発生しないようにするため、すべての中間ステップでこの設定を有効にしたいと思います。

このテストでは、エンドポイントが期待通りに応答しない場合、アラートが生成されます。テストが失敗した場合、Y ミリ秒後に X 回再試行することができます。再試行の間隔は、警告の感性に合うようにカスタマイズしてください。

ステップの失敗を示す実行設定のスクリーンショット

応答から変数を抽出する

必要に応じて、API リクエストの応答ヘッダーまたは本文をパースして変数を抽出できます。変数の値は、API リクエストステップが実行されるたびに更新されます。

変数のパースを開始するには、Extract a variable from response content をクリックします。

  1. Variable Name を入力します。変数名に使用できるのは大文字、数字、アンダースコアのみです。また、3 文字以上にする必要があります。

  2. 変数をレスポンスのヘッダーから抽出するか、本文から抽出するか決定します。

    • 応答ヘッダーから値を抽出: API リクエストの応答ヘッダー全体を変数値として使用するか、regex を用いてパースします。
    • 応答本文から値を抽出: API リクエストの応答本文全体を変数値として使用するか、regexJSONPath、または XPath を用いてパースします。
Multistep API テストで API リクエストから変数を抽出する

1 つのテストステップにつき最大 10 個の変数を抽出することができます。一度作成すると、この変数はマルチステップ API テストの次のステップで使用することができます。詳しくは、変数の使用を参照してください。

テストの頻度を指定する

Multistep API テストは次の頻度で実行できます。

  • On a schedule: 最も重要なエンドポイントにユーザーが常にアクセスできるようにします。Datadog でマルチステップ API テストを実行する頻度を選択します。
  • Within your CI/CD pipelines: 欠陥のあるコードがカスタマーエクスペリエンスに影響を与える可能性があることを恐れずに出荷を開始します。
  • On-demand: チームにとって最も意味のあるときにいつでもテストを実行します。

Define alert conditions

Set alert conditions to determine the circumstances under which you want a test to fail and trigger an alert.

Alerting rule

When you set the alert conditions to: An alert is triggered if any assertion fails for X minutes from any n of N locations, an alert is triggered only if these two conditions are true:

  • At least one location was in failure (at least one assertion failed) during the last X minutes;
  • At one moment during the last X minutes, at least n locations were in failure.

Fast retry

Your test can trigger retries X times after Y ms in case of a failed test result. Customize the retry interval to suit your alerting sensibility.

Location uptime is computed on a per-evaluation basis (whether the last test result before evaluation was up or down). The total uptime is computed based on the configured alert conditions. Notifications sent are based on the total uptime.

Configure the test monitor

A notification is sent by your test based on the alerting conditions previously defined. Use this section to define how and what to message your team.

  1. Similar to how you configure monitors, select users and/or services that should receive notifications either by adding an @notification to the message or by searching for team members and connected integrations with the dropdown menu.

  2. Enter the notification message for your test or use pre-filled monitor messages. This field allows standard Markdown formatting and supports the following conditional variables:

Conditional VariableDescription
{{#is_alert}}Show when the test alerts.
{{^is_alert}}Show unless the test alerts.
{{#is_recovery}}Show when the test recovers from alert.
{{^is_recovery}}Show unless the test recovers from alert.
{{#is_renotify}}Show when the monitor renotifies.
{{^is_renotify}}Show unless the monitor renotifies.
{{#is_priority}}Show when the monitor matches priority (P1 to P5).
{{^is_priority}}Show unless the monitor matches priority (P1 to P5).

Notification messages include the message defined in this section and information about the failing locations. Pre-filled monitor messages are included in the message body section:

Synthetic Monitoring monitor section for API tests, highlighting the pre-filled monitor messages
  1. Specify how often you want your test to re-send the notification message in case of test failure. To prevent renotification on failing tests, check the option Stop re-notifying on X occurrences.

  2. Click Save & Start Recording to save your test configuration and monitor.

For more information, see Synthetic Monitoring notifications.

Create local variables

To create a local variable, click + All steps > Variables. You can select one of the following available builtins to add to your variable string:

{{ numeric(n) }}
Generates a numeric string with n digits.
{{ alphabetic(n) }}
Generates an alphabetic string with n letters.
{{ alphanumeric(n) }}
Generates an alphanumeric string with n characters.
{{ date(n unit, format) }}
Generates a date in one of Datadog’s accepted formats with a value corresponding to the UTC date the test is initiated at + or - n units.
{{ timestamp(n, unit) }}
Generates a timestamp in one of Datadog’s accepted units with a value corresponding to the UTC timestamp the test is initiated at +/- n units.
{{ uuid }}
Generates a version 4 universally unique identifier (UUID).
{{ public-id }}
Injects the Public ID of your test.
{{ result-id }}
Injects the Result ID of your test run.

To obfuscate local variable values in test results, select Hide and obfuscate variable value. Once you have defined the variable string, click Add Variable.

変数の抽出

ローカル変数の作成に加えて、マルチステップ API テストの任意のステップから変数を抽出し、後続のステップで値を再挿入することが可能です。

変数を使用する

API テストの URL、高度なオプション、およびアサーションで、Settings で定義されたグローバル変数ローカルで定義された変数を使用できます。

変数のリストを表示するには、目的のフィールドに {{ と入力します。

テストの失敗

ステップが 1 つまたは複数のアサーションを満たさない場合、またはステップのリクエストが時期尚早に失敗した場合、テストは FAILED と見なされます。場合によっては、エンドポイントに対してアサーションをテストできずにテストが実際に失敗することがあります。これらの理由には次のものがあります。

CONNREFUSED
ターゲットマシーンが積極的に拒否したため、接続できませんでした。
CONNRESET
接続がリモートサーバーによって突然閉じられました。Web サーバーにエラーが発生した、応答中にシステムが停止した、Web サーバーへの接続が失われた、などの原因が考えられます。
DNS
テスト URL に対応する DNS エントリが見つかりませんでした。テスト URL の構成の誤りまたは DNS エントリの構成の誤りの原因が考えられます。
INVALID_REQUEST
テストのコンフィギュレーションが無効です (URL に入力ミスがあるなど)。
SSL
SSL 接続を実行できませんでした。詳細については、個別のエラーページを参照してください
TIMEOUT
リクエストを一定時間内に完了できなかったことを示します。TIMEOUT には 2 種類あります。
  • TIMEOUT: The request couldn't be completed in a reasonable time. は、リクエストの持続時間がテスト定義のタイムアウト (デフォルトは 60 秒に設定されています) に当たったことを示します。 各リクエストについて、ネットワークウォーターフォールに表示されるのは、リクエストの完了したステージのみです。例えば、Total response time だけが表示されている場合、DNS の解決中にタイムアウトが発生したことになります。
  • TIMEOUT: Overall test execution couldn't be completed in a reasonable time. は、リクエストおよびアサーションの処理時間が最大許容時間 (30 分) に達したことを示しています。

HTTP ステップについては、 一般的な HTTP ステップの失敗を参照してください。gRPC ステップについては、 一般的な gRPC ステップの失敗を参照してください。

権限

デフォルトでは、Datadog 管理者および Datadog 標準ロールを持つユーザーのみが、Synthetic Multistep API テストを作成、編集、削除できます。Synthetic Multistep API テストの作成、編集、削除アクセスを取得するには、ユーザーをこれら 2 つのデフォルトのロールのいずれかにアップグレードします。

カスタムロール機能を使用している場合は、Synthetic Monitoring の synthetics_read および synthetics_write 権限を含むカスタムロールにユーザーを追加します。

アクセス制限

アカウントにカスタムロールを使用しているお客様は、アクセス制限が利用可能です。

組織内の役割に基づいて、Multistep API テストへのアクセスを制限することができます。Multistep API テストを作成する際に、(ユーザーのほかに) どのロールがテストの読み取りと書き込みを行えるかを選択します。

テストの権限の設定

その他の参考資料