正しく機能することを確認するために、合成モニタリングのAPIテスト を使用してAPIエンドポイントを監視します。New Relicは、http-request モジュールを使用してエンドポイントへHTTPコールをして、結果を検証します。
ここには、リクエストを送信するために$http
オブジェクトを使用する方法を示すサンプル関数が記載されています。このオブジェクトで使用できるオプションの詳細については、http-request readme を参照してください。(リクエストは廃止されていますが、これらのオプションは適用されます。)
API http-requestモジュールを使用する APIテストは、$http
オブジェクトで使用できるhttp-request モジュールによって作動します。New Relicは、各周波数間隔ごとに、選択した各位置からエンドポイントをクエリします。モニターの作成方法については、モニターの追加 を参照してください。
リクエストのためのメタデータの定義方法 、GETリクエストの作成方法 、POSTリクエストの作成方法 、および結果の検証方法 を読んでください。
重要 最大で3分間のランタイム後、New Relicはスクリプトを手動で停止します。
one.newrelic.com > Synthetic monitoring >Create monitor :スクリプトエディタは、スクリプトコマンド(GitHubで使用可能) を単純化するために、関数、セレクタ、その他の要素を提案します。
リクエストオプションを設定する スクリプトを開始する場合は、以下の手順に従います。
オプションオブジェクト内のオプションメタデータの例:
//Declare optional metadata
//Specify the endpoint URL
url: 'https://api-endpoint.example.com',
//Specify optional headers
'Endpoint-Key': 'uqNTC57Phe72pnnB8JuJmwAr7b09nKSKSz',
'Additional-Header': 'Additional-Header-Data'
SSLおよびagentOptionsの場合: SSLオプションを設定、またはagentOptions
オブジェクトを提供する場合は、リクエストのoptions
オブジェクトのagent
プロパティを$globalAgents.https
または$globalAgents.http
に設定して、インストゥルメントされたグローバルエージェントをHTTPリクエストが確実に使用するようにする必要があります。
SSLオプションまたはagentOptions
の使用の例:
SSLオプションまたはagentOptionsの使用 この例では、agentOptions
を使用します。
//Declare optional metadata
var options = {
//Specify the endpoint URL
url: 'https://api-endpoint.example.com ',
//Specify optional headers
headers: {
'Endpoint-Key': 'uqNTC57Phe72pnnB8JuJmwAr7b09nKSKSz',
'Additional-Header': 'Additional-Header-Data'
}
//Specify global agent as the http agent
agent: $globalAgents.https,
//Set SSL option
strictSSL: true,
//Specify http agent options
agentOptions: {
maxVersion: 'TLSv1.1'
},
};
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_LICENSE_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(JSON.parse(body));
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 myLicenseKey = '{YOUR_LICENSE_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 license key and expected data type.
headers: {
'Api-Key': myLicenseKey,
'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');
//Parse JSON into variable.
var info = JSON.parse(body);
//Verify that `info` contains element named `success` with a value of `true`.
assert.ok(info.success == true, 'Expected True results in Response Body, result was ' + info.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 myLicenseKey = '{YOUR_LICENSE_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 license 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');
var info = JSON.parse(body);
//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(info.success == true, 'Expected True results in Response Body, result was ' + info.success);