Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Tracking Baseline Deviation on Throughput

synthetics-script
developer

#1

The following is a Synthetics API Test script which queries against the New Relic Insights API. This implementation of the script is checking to see if your application has had more than at 10% change in PageViews over the last week. If it detects a change larger than 10 percent, it fails the monitor and send an email to let you know about the failure.

You can change the data that you are querying by editing the nrql variable.

Note that for the script to work as written, you must have more than 1 week of Insights data retention.

// Using Synthetics scripted monitors to alert on % baseline deviation

// Add libs
var assert = require('assert');
var util = require('util');

// Define threshold and http request options
var THRESHOLD = 10, // 10 percent 
    appname = 'APP_NAME',
    insightsquerykey = 'API_KEY',
    accountnumber = 'ACCOUNT_NUMBER';
var options = {
  url: 'https://insights-api.newrelic.com/v1/accounts/'+accountnumber+'/query',
  method: 'GET',
  json: true,
  headers: {
    'X-Query-Key': insightsquerykey,
    'Accept': 'application/json '
  },
  qs : {
// NOTE : You can change this query depending on the data that you would like to look at. 
//We are getting a count of the last hour's page views for a given application and comparing that to the same data one week ago. 
//In order for this to work you will need Insights retention of at least 1 week
    'nrql': "SELECT count(*) FROM PageView WHERE appName = '"+appname+"' since 1 hour ago compare with 1 week ago"
  }
};

// Callback to run when after HTTP GET request returns
callback = function(error, response, body) {
  
  // Fail unless Insights API returns with a 200 status code
  assert.equal(response.statusCode, 200, 'invalid response from insights - this monitor is no longer reliable');
  
  var current,
      previous;
  
  
  // Take response JSON and fill up the 'current' and 'previous' objects with data points
	current = body.current.results[0].count;
  previous = body.previous.results[0].count;

  
  // Look at the current data point, subtract the data point from the same time last week, if greater than threshold fail check
  var delta = (current - previous) / current;
  console.log(util.format("Page view delta for %s is %d %", appname, delta.toFixed(2)*100));
  assert.ok(delta < THRESHOLD, util.format("Page view for location %s has increased by %d% % in the last week and is above the %d% threshold", appname, delta.toFixed(2)*100, THRESHOLD));


};

// firing the request
$http.get(options, callback);