Combining the use of Synthetic API Calls inside a Scripted Browser for New Relic Synthetics

There are probably better ways to do this and I encourage you to offer your methods.
I know that the New Relic Scripted Browser and the New Relic API synthetics use slightly different syntax but I need a check that did both the scripted browser work as well as the API call.

The interesting point here is that I am writing a New Relic Synthetic that calls an API as the action.
I have to use the URLLIB module to do this but it is pure selenium.

Bottom line is that I have a create function and a close function for alerting.

Also I did not want to use the webhook. This was an option I wanted to explore.

//URL to monitor https://your URL Here.com
//test Web Site by logging in and returning a display page
/**

  • This is where we shove comments if I had any.
    */
    var request = require(‘request’);
    var assert = require(‘assert’);
    var urllib = require(‘urllib’);
    var host = ‘hostname’

function create_event() {
urllib.request(‘https://api.yourcompany API’, {
method: ‘POST’,
headers: {
‘Authorization’: ‘Bearer CHANGEME’,
‘Content-Type’: ‘application/json’
},
content: ‘{“app_key”:“CHANGEME”,“status”:“critical”,“host”:"’+host+’",“check”:“CPU overloaded”,“description”:"CPU is above upper limit (80%)“Source”:“NewRelic”}’
}, function(err, data , res) {
console.log(err);
console.log(data);
console.log(res);
});
}

function close_event() {
urllib.request(‘https://api.yourcompany.com’, {
method: ‘POST’,
headers: {
‘Authorization’: ‘Bearer CHANGEME’,
‘Content-Type’: ‘application/json’
},
content: ‘{“app_key”:“CHANGEME”,“status”:“ok”,“host”:"’+host+’",“check”:“CPU overloaded”,“description”:“CPU is above upper limit (80%)”,“Source”:“NewRelic”}’
}, function(err, data , res) {
console.log(err);
console.log(data);
console.log(res);
});
}

console.log(‘starting script’);

$browser.get(“https://your.url.to.check.com”).then(function(){
//Click the submit button.
return $browser.findElement($driver.By.id(“UserID”)).sendKeys(“USERNAME”);
}).then(function(){
return $browser.findElement($driver.By.id(“Password”)).sendKeys(“CHANGEME”);
}).then(function(){
return $browser.findElement($driver.By.className(“btn-primary”)).click();
//return $browser.findElement($driver.By.className(“foo-btn-primary”)).click();
}).then(function() {
console.log(‘Browser script execution SUCCEEDED.’);
close_event();
}, function(err) {
console.log (‘Browser script execution FAILED.’);
create_event();
}); $browser.sleep(5000)
console.log(‘ending script’);

1 Like

Hi @john.monroe thank you for sharing your script! You should also be able to use the npm request module in a Scripted Browser.

We often recommend using urllib-sync when performing API calls in a Scripted Browser monitor when wanting to ensure synchronous action and that no browser actions are performed during the API call.

I did want to also post here an alternative method that uses the npm request module mixed with webdriver promises that would allow you to ensure synchronous action via a single promise chain. E.g:

makeAPIcall()
.then(function() {
    return $browser.get('https://your.url.to.check.com');
})
...

Here is a basic example that makes an HTTP request, using npm request, then uses the returned response code as a HTTP header value in Chrome:

var request = require('request');

function makeRequest() {
    var flow = $driver.promise.controlFlow();
    return flow.execute(function() {
        var p = $driver.promise.defer();
        var options = {
            uri: 'http://www.google.com',
            method: 'GET'
        }
        request(options, function(err, res, body) {
            if (err) { 
              console.log(err);
              p.reject(err);
            }
            p.fulfill(res.statusCode);
        });
        return p.promise;
    });
}

// Main script promise chain
makeRequest().then(function(responseCode) {
  return $browser.addHeader('responseCode',responseCode.toString());
}).then(function() {

   //see responseCode header listed on page.
  return $browser.get('http://www.xhaus.com/headers');
})
1 Like