Last active
March 20, 2023 12:31
-
-
Save ryanv94/6f0785fcbd4a3e0b1eed4e77ace7517c to your computer and use it in GitHub Desktop.
Synthetics Script for alerting on COMPARE WITH query data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var assert = require('assert'); | |
var myAccountID = 'YOUR ACCOUNT ID'; | |
var myQueryKey = 'YOUR QUERY KEY'; | |
var encodedQuery = encodeURIComponent('SELECT count(*) FROM PageView SINCE 10 MINUTES AGO COMPARE WITH 1 WEEK AGO'); | |
var decreaseThreshold = -20, increaseThreshold = 20; | |
var options = { | |
//Define endpoint URI | |
uri: 'https://insights-api.newrelic.com/v1/accounts/' + myAccountID + '/query?nrql=' + encodedQuery, | |
//Define query key and expected data type. | |
headers: { | |
'X-Query-Key': myQueryKey, | |
'Accept': 'application/json' | |
} | |
}; | |
var jsonBody = {}; | |
var currentValue, previousValue; | |
var percentDiff = 0; | |
function callback(err, response, body) { | |
// ********************* IMPORTANT **************************** | |
// If you are using the latest Synthetics Runtime, then uncomment the line below (27), and comment out the line 30. | |
// jsonBody = body; | |
// If you are using the older Synthetics Runtime, ensure the line above (27) is commented out, and uncomment the line below (30). | |
jsonBody = JSON.parse(body) | |
// ********************* IMPORTANT **************************** | |
/* | |
NOTE - the query function is important here. My query is SELECT count(*), so I am looking for the count below. | |
If your query is looking at any other function (latest, average, max, min, etc..), | |
then you should use: jsonBody.current.results[0].average | jsonBody.current.results[0].max | jsonBody.current.results[0].min | etc.... | |
*/ | |
currentValue = jsonBody.current.results[0].count; | |
previousValue = jsonBody.previous.results[0].count; | |
analyse(currentValue, previousValue); | |
} | |
$http.get(options, callback); | |
function analyse(current, previous) { | |
if (current > previous) { | |
// Get the percentage of the difference between current and previous values & round to two decimal places. | |
var x = ((current - previous) / previous) * 100; | |
percentDiff = parseFloat(x).toFixed(2); | |
console.log('Current value: ' + current + '; Previous value: ' + previous) | |
console.log('Results are up comparitively'); | |
console.log('The difference is: ' + (current - previous)) | |
console.log('This is an increase of: ' + percentDiff + '%') | |
// UNCOMMENT THE BELOW 'IF' STATEMENT IF YOU ARE INTERESTED IN BEING ALERTED ON INCREASES | |
/* if (percentDiff > increaseThreshold) { | |
assert.fail('There has been a significant increase'); | |
} */ | |
// --------------------------------------------------------- | |
} else if (current < previous) { | |
// Get the percentage of the difference between current and previous values & round to two decimal places. | |
var x = ((current - previous) / previous) * 100; | |
percentDiff = parseFloat(x).toFixed(2); | |
console.log('Current value: ' + current + '; Previous value: ' + previous) | |
console.log('Results are down comparitively') | |
console.log('The difference is: ' + (previous - current)) | |
console.log('This is an decrease of: ' + percentDiff + '%') | |
if (percentDiff < decreaseThreshold) { | |
assert.fail('There has been a significant drop'); | |
} | |
} | |
} |
@Timtech4u, I commented out jsonBody = JSON.parse(body) and used body instead of jsonBody. It worked.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @ryanv94 , I am currently unable to get this to run. Have there been some changes?