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

Relic Solution: Insights Utility Library for Synthetics

synthetics-script
developer

#1

New Relic’s $util.insights is a set of tools to set and manipulate Synthetics data for Insights. All data is added to the SyntheticCheck event as additional attributes, with the prefix custom..

This functionality is available to monitors created after April 1, 2015 and more info can be found at Insights Utility Library for Synthetics.

Below is an example script utilizing $util.insights.

// this script uses $util to push custom attributes into Insights

var By = $driver.By
var startTime = new Date()
var thisStep = 1
var prevStep = thisStep - 1
var segmentTiming = {}

/**
 * logs message to console and pushes attribute to insights.
 * @param {string} msg - What message you want to log to the console.
 * @param {string} attrName - What you want to call your attribute in Insights.
 */
function log(msg, attrName) {
  var elapsedTime = ((new Date() - startTime) / 1000.0).toFixed(1)
  
  // set timing
  segmentTiming[thisStep] = elapsedTime
  var segmentTime = (segmentTiming[thisStep] - segmentTiming[prevStep]).toFixed(1)
  
  // send elapsed time to insights
  $util.insights.set('Step ' + thisStep + ' ' + attrName, elapsedTime + 's')
  
  // send segment time to insights and log timing to console
  if(thisStep === 1) {
    $util.insights.set('Step ' + thisStep + ' ' + 'segment time', segmentTiming[thisStep] + 's')
    console.log('Step ' + thisStep + ': ' + elapsedTime + 's: ' + msg)
    console.log('Segment time: ' + elapsedTime)
  } else {
    $util.insights.set('Step ' + thisStep + ' ' + 'segment time', segmentTime + 's')
    console.log('Step ' + thisStep + ': ' + elapsedTime + 's: ' + msg)
    console.log('Segment time: ' + segmentTime)
  }

  // increase counters
  thisStep++
  prevStep++
}

// request google and send search
$browser.get("https://google.com")
  .then(function(){
    log("got google", "got google")
    $browser.waitForAndFindElement(By.id("lst-ib")) // find search bar
  .then(function(element){
    element.sendKeys('monkeys\n').then(function(){ // send search term
      log('google search sent', 'google search sent')
    })
  })
})

// request yahoo and send search
$browser.get("https://yahoo.com")
  .then(function(){
    log("got yahoo", 'got yahoo')
    $browser.waitForAndFindElement(By.id("uh-search-box")) // find search bar
  .then(function(element){
    element.sendKeys("monkeys\n").then(function(){ // send search term
      log("yahoo search sent", 'yahoo search sent')    
    })
  })
})

// request bing and send search
$browser.get("https://bing.com")
  .then(function(){
    log("got bing", 'got bing')
  $browser.waitForAndFindElement(By.id("sb_form_q")) // find search bar
  .then(function(element){
    element.sendKeys("monkeys\n").then(function(){ // send search term
      log("bing search sent", 'bing search sent')
    })
  })
})

Is there a way to Start and Stop recording mid-script?