正しく機能することを確認するために、合成モニタリングのAPIテスト を使用してAPIエンドポイントを監視します。New Relicは、内部でgotモジュール を使用してエンドポイントへHTTPコールをして、結果を検証します。
ここには、リクエストを送信するために$http
オブジェクトを使用する方法を示すサンプル関数が記載されています。この$http
オブジェクトは、非推奨のrequest
モジュールを使用せずに、got
を利用して基本的なユースケースに下位互換性を提供し、カスタムrequest
のような体験を実現します。request
の高度なユースケースのすべてが、この下位互換性によってサポートされているわけではありません。このオブジェクトで使用できるオプションの詳細については、gotモジュールreadme を参照してください。got
モジュールのドキュメントには、以前のスクリプト化されたAPIランタイムで使用したrequest
モジュールから予想される変更点をハイライトした移行ガイド が含まれています。
$http
オブジェクトが提供するrequest
のような体験は、Node.js 16以降のスクリプト化されたAPIランタイムで、request
を直接使用しようとする顧客にも返されます。
API gotモジュールを使用 APIテストは、$http
オブジェクトで使用できるgotモジュール によって作動します。モニターの作成方法については、モニターの追加 を参照してください。
リクエストのためのメタデータの定義方法 、GETリクエストの作成方法 、POSTリクエストの作成方法 、および結果の検証方法 を読んでください。
$http
オブジェクトを使用している限り、結果のタイミングの詳細が表示されます。$http
オブジェクトに含まれていないスクリプト化されたAPIユースケースでは、$har
オブジェクトを使用してカスタムタイミングの詳細をレポート できます。
重要 最大で3分間のランタイム後、New Relicはスクリプトを手動で停止します。
one.newrelic.com > Synthetic monitoring >Create monitor :スクリプトエディタは、スクリプトコマンド(GitHubで使用可能) を単純化するために、関数、セレクタ、その他の要素を提案します。
リクエストオプションを設定する スクリプトを開始する場合は、以下の手順に従います。
変数(options
など)を宣言して、gotオプションオブジェクト を保存します。 URLエンドポイントやカスタムヘッダーなどのリクエストオプションを定義します。 ヒント サポートされているオプションの完全なリストについては、GitHubのgot
ドキュメントにあるgotオプション を参照してください。
オプションオブジェクト内のオプションメタデータの例:
//Declare optional metadata
//Specify the endpoint URL
url: 'https://api-endpoint.example.com',
//Specify optional headers
'Endpoint-Key': 'uqNTC57Phe72pnnB8JuJmwAr7b09nKSKSz',
'Additional-Header': 'Additional-Header-Data'
GETリクエストを送信する GETリクエストをするには、$http.get
メソッドを使用してリクエストを送信します。リクエストオプション を定義し、$http.get
を使用してリクエストを行ってから、レスポンスを検証 してエンドポイントが正しい結果を返していることを確認します。
POSTリクエストを送信する POSTリクエストをするには、$http.post
メソッドを使用してリクエストを送信します。リクエストオプション を定義し、$http.post
を使用してリクエストを行ってから、レスポンスを検証 してエンドポイントが正しい結果を返していることを確認します。
NerdGraphの例 この例では、NerdGraph API をクエリします。
// Define your authentication credentials
const myAccountId = '{YOUR_ACCOUNT_ID}';
const myAPIKey = '{YOUR_API_KEY}';
// Define endpoint URI, https://api.eu.newrelic.com/graphql for EU accounts
uri: 'https://api.newrelic.com/graphql',
'Content-Type': 'application/json',
query getNrqlResults($accountId: Int!, $nrql: Nrql!) {
account(id: $accountId) {
accountId: Number(myAccountId),
nrql: 'SELECT average(duration) FROM Transaction',
// Define expected results using callback function
function callback(err, response, body) {
// Log JSON results from endpoint to Synthetics console
console.log('Script execution completed');
// Make POST request, passing in options and callback
$http.post(options, callback);
イベントAPI POSTの例 この例は、静的な整数を含むカスタムイベントをイベントAPI にPOSTします。
//Define your authentication credentials.
var myAccountID = '{YOUR_ACCOUNT_ID}';
var myAPIKey = '{YOUR_API_KEY}';
//Import the 'assert' module to validate results.
var assert = require('assert');
var options = {
//Define endpoint URL.
url: "https://insights-collector.newrelic.com/v1/accounts/"+myAccountID+"/events",
//Define body of POST request.
body: '[{"eventType":"SyntheticsEvent","integer1":1000,"integer2":2000}]',
//Define New Relic API key and expected data type.
headers: {
'Api-Key': myAPIKey,
'Content-Type': 'application/json'
}
};
//Define expected results using callback function.
function callback(error, response, body) {
//Log status code to Synthetics console.
console.log(response.statusCode + " status code")
//Verify endpoint returns 200 (OK) response code.
assert.ok(response.statusCode == 200, 'Expected 200 OK response');
//Verify that `body` contains element named `success` with a value of `true`.
assert.ok(body.success == true, 'Expected True results in Response Body, result was ' + body.success);
//Log end of script.
console.log("End reached");
}
//Make POST request, passing in options and callback.
$http.post(options, callback);
結果を検証する 結果を検証するには、assert
モジュールをインポートして、テストケースを定義します。assert
メソッドを呼び出して、エンドポイントのレスポンスを検証します。assert
関数のいずれかが失敗する場合、モニター全体が失敗したチェックとみなされます。これにより、アラート通知 がトリガされ、メトリクスに影響する場合があります。
重要 合成モニタリングは例外のスローを許可しません。例外がスローされると、スクリプトが失敗します。assert
モジュールを使用して結果を検証し、console.log()
を使用して合成コンソールに結果をログします 。
イベントAPIの検証例 この例は、イベントAPIに投稿してから、レスポンスが{"success":true}
であることを検証します。
//Define your authentication credentials.
var myAccountID = '{YOUR_ACCOUNT_ID}';
var myAPIKey = '{YOUR_API_KEY}';
//Import the `assert` module to validate results.
var assert = require('assert');
url: "https://insights-collector.newrelic.com/v1/accounts/"+myAccountID+"/events",
//Define body of POST request.
body: '[{"eventType":"SyntheticsEvent","integer1":1000,"integer2":2000}]',
//Define New Relic API key and expected data type.
'Content-Type': 'application/json'
$http.post(options, function(error, response, body) {
//Log status code to Synthetics console. The status code is logged before the `assert` function,
//because a failed assert function ends the script.
console.log(response.statusCode + " status code")
//Call `assert` method, expecting a `200` response code.
//If assertion fails, log `Expected 200 OK response` as error message to Synthetics console.
assert.ok(response.statusCode == 200, 'Expected 200 OK response');
//Call `assert` method, expecting body to return `{"success":true}`.
//If assertion fails, log `Expected True results in Response Body,` plus results as error message to Synthetics console.
assert.ok(body.success == true, 'Expected True results in Response Body, result was ' + body.success);