합성 모니터링의 API 테스트 를 사용하여 API 엔드포인트를 모니터링하여 올바르게 작동하는지 확인하십시오. New Relic은 내부적으로 got 모듈 을 사용하여 엔드포인트에 대한 HTTP 호출을 수행하고 결과를 검증합니다.
여기에 $http 객체를 사용하여 요청을 제출하는 방법을 보여주는 몇 가지 예시 함수가 있습니다. $http 객체는 사용되지 않는 request 모듈의 사용을 피하면서 기본 사용 사례에 대한 이전 버전과의 호환성을 제공하기 위해 got 에 의해 구동되는 동안 사용자 정의 request 와 유사한 환경을 제공합니다. request 의 모든 고급 사용 사례가 이 이전 버전과의 호환성에 의해 지원되거나 지원되는 것은 아닙니다. 이 객체에 사용할 수 있는 옵션에 대한 자세한 문서는 got 모듈 readme 를 참조하십시오. got 모듈 문서에는 이전 스크립팅된 API 런타임에 사용된 request 모듈에서 예상되는 변경 사항을 강조 하는 마이그레이션 가이드 가 포함되어 있습니다.
$http 객체가 제공하는 request 유사 환경은 Node.js 16 및 최신 스크립트 API 런타임에서 직접 request 를 사용하려는 모든 고객에게도 반환됩니다.
//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() 을 사용 하여 합성 콘솔에 결과를 기록합니다 .
이 예시는 Event API에 POST한 다음 응답이 {"success":true} 인지 확인합니다.