How to log specific parameter in response body to console

Hi everyone,

I am a bit new to this, so I will appreciate some help. I am trying to write a script to monitor an API that returns the below in its response;

{"success":true,"message":"Wallet fetched successfully","data":{"id":***,"_id":"*****","business":"*****","businessId":682,"ledgerBalance":73576977.678667,"availableBalance":64386659.038666,"lockedBalance":9190318.640001,"walletNumber":*****,"currency":"***","status":"enabled","createdAt":"2021-09-16T17:48:15.000Z","updatedAt":"2022-01-06T20:46:55.000Z"}}

The problem is I don’t care for the Status Code. I need to be able to create an alert for when a parameter in the body is less than or equal to a set threshold. For example, if “availableBalance” <= 500000.

I believe this default function below logs the Status Code, what do I need to do to tweak this script to log the desired parameter.

function callback (err, response, body) {
  //Log JSON results from endpoint to Synthetics console.
  console.log(JSON.parse(body));
  console.log('done with script');
}

Hi, @edward7: You can get availableBalance like this:

function (err, response, body) {
  var result = JSON.parse(body);
  var availableBalance = result.data.availableBalance; 
}

But logging to the console will not allow you to alert on it. You will need to send the value to NRDB as a custom attribute: Add custom attributes to synthetic monitoring data | New Relic Documentation.

1 Like

Thanks for the update @philweber

But the script fails validation. See error below;

Job failed before completion (id: “undefined”): Unexpected token (
SyntaxError: Unexpected token (
at Function ()
at JobResource.getScriptFn (/opt/runtimes/4.0.0/modules/synthetics-runner/lib/job-resource/index.js:79:19)
at ApiScriptRunner.value [as run] (/opt/runtimes/4.0.0/modules/synthetics-runner/lib/runner/0.6.0/script-runner.js:67:44)
at Object. (/opt/runtimes/4.0.0/main.js:15:35)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

Did I mess something up with the syntax? See script below

var options = {
uri: 'https://xxxxxxyyyy.com/ddd)’,
headers: {
‘Api-key’: $secure.API_KEY
}
};
function (err, response, body){
var result = JSON.parse(body);
var availableBalance = result.data.availableBalance;
}

I don’t see where you are calling the API. It should look like this:

var options = {
  uri: 'https://xxxxxxyyyy.com/ddd',
  headers: {
    'Api-key': $secure.API_KEY
  }
};

$http.get(options, 
  function (err, response, body) {
    var result = JSON.parse(body);
    var availableBalance = result.data.availableBalance;
    console.log(availableBalance);
  });
1 Like

Thank you very much. I figured out the utils.insights.set as well.

1 Like