HasanB
2 min readApr 19, 2022

--

Newrelic Scripted browsers: Best practices and API Automation

Newrelic offers many capabilities in Synthetic Monitoring, one of which is scripted browsers, which allows you to create a full-fledged selenium web driver-based script to monitor your Application workflows

Newrelic uses a fork of selenium, the documentation can be viewed here

In this blog, we cover 2 main aspects:

  1. How to create these tests in bulk in an automated fashion using Newrelic API and Bash
  2. How to attach your scripts to the tests

First up, to create these tests, you need to supply a number of variables

  • Newrelic API Key
  • Test Name
  • Test Location
  • endpoint URL (option)
  • Status
  • Frequency (mins)

You first need to create these tests, obtain their monitor IDs, then attach your selenium scripts to the tests bypassing the monitor IDs generated in step 1

Below is a solution I’ve developed to achieve this (after many erroneous attempts)

#!/bin/bashmonitorlist=$1# Extract and generate variables to be used by the API
cat $monitorlist | while IFS=',' read URL Test_Name location;
docurl -v \-X POST -H 'Api-Key:NRAK-YOUR_API_KEY' \-H 'Content-Type: application/json' https://synthetics.newrelic.com/synthetics/api/v3/monitors \-d '{ "name" : "'"$Test_Name"' Example test", "frequency" : 5, "uri" : "'"$URL"'", "locations" : [ "'"$location"'" ], "type" : "script_browser", "status" : "enabled", "slaThreshold" : "1.0"}'done

Above is a script that accepts the list of monitors in the first argument during execution

Your monitor list has to be the in the format below

websitename.tld,Test1 Name,AWS_US_WEST_1
websitename2.tld,Test Name,AWS_EU_WEST_3
prodcluster3.svc,Test3 Name,AWS_EU_WEST_2

Note: Newrelic’s public agents are based in AWS, thus locations have to be declared in AWS format as above, while in the UI they have friendly names like San-Francisco, Sydney etc.

Once you run the script against the list you should see the tests in the Newrelic Console in a few minutes!

Attaching scripts to your test

This is a tricky one, and Newrelic’s KB does give a script that frankly does not work

Luckily I have created a ready solution for our readers to consume!

#!/bin/bashscriptfile=$1  #script file (txt) in first argumentmonitorlist=$2  #list of monitors and their IDs in the 2nd Argumentcat $monitorlist | while IFS=' ' read qtp_portal monitor_id ;do#sed magic to replace the https portion with website in your scriptsed -i "s/https:\/\/.*/https:\/\/$qtp_portal\'\;/g" $scriptfilescript=$(cat $scriptfile)# base64 encode scriptencoded=`echo "$script" | base64 -w 0`scriptPayload="{\"scriptText\":\"$encoded\"}"curl -v -X PUT -H "Api-Key:NRAK-YOURKEY" -H 'Content-Type:application/json' "https://synthetics.newrelic.com/synthetics/api/v3/monitors/$monitor_id/script" -d $scriptPayloaddone

Monitorlist has to be of the format:

https://example.com,123456789-d5b2-12345-95fe-f3689078

--

--