Write synthetic API tests (legacy runtime - Node.js 10 and lower)
Important
As of August 26, 2024, you can no longer create new monitors using legacy runtimes on public or private locations.
On October 22, 2024, we will end of life the containerized private minion (CPM) and legacy synthetics runtime versions. For public locations, use the runtime upgrade UI to update your monitors to the newest runtimes. For private locations, please review our recommended migration steps to avoid monitor degradation.
Use synthetic monitoring's API tests to monitor your API endpoint to ensure it is functioning correctly. New Relic uses the http-request module internally to make HTTP calls to your endpoint and validate the results.
Here we present some example functions showing how to use the $http object to submit your request. For detailed documentation on the options available for this object, see the http-request readme. (Note that Request is deprecated, but these options still apply.)
API tests are powered by the http-request module, which is available through the $http object. Once each frequency interval, New Relic queries your endpoint from each of your selected locations. For instructions on creating a monitor, see Adding monitors.
Define request options such as the URL endpoint, and custom headers.
If you're setting SSL or agent options, see SSL and agentOptions requirements. We recommend using SSL to avoid exposing plain text credentials in your headers.
Tip
For a full list of supported request options, see request(options, callback) in the http-request documentation on GitHub.
Here's an example of optional metadata in the options object:
For SSL and agentOptions: If you are setting SSL options or providing an agentOptions object, the agent property in the request options object will need to be set to $globalAgents.https or $globalAgents.http to ensure your HTTP requests use the instrumented global agent.
Here's an example of using a SSL option or agentOptions:
This example uses 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'
},
};
Send a GET request
To make a GET request, use the $http.get method to submit your request. Define your request options, make your request using $http.get, then validate the response to ensure your endpoint is returning the correct results.
Send a POST request
To make a POST request, use the $http.post method to submit your request. Define your request options, make your request using $http.post, then validate the response to ensure your endpoint is returning the correct results.
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);
This example POSTs a custom event containing static integers to the Event API:
//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);
Validate results
To validate your results, import the assert module to define your test case. Call an assert method to validate your endpoint's response. If any assert functions fail, the entire monitor will be considered a failed check. This may trigger alert notifications and affect your metrics.
Important
Synthetic monitoring does not allow thrown exceptions. Thrown exceptions result in script failure. Use the assert module to validate your results, and use console.log() to log results to the synthetic's console.
This example POSTs to the Event API, then validates that the response is {"success":true}: