Announcing N|Solid JS API - NodeSource

The NodeSource Blog

You have reached the beginning of time!

Announcing N|Solid JS API

NodeSource is excited to announce N|Solid JS API! 🥳 With this new API you can use all the power provided by the N|Solid console and CLI using your own JavaScript code programmatically.

In other words, now it is not necessary to listen to events from the console, but from your own code you can decide when to run a certain command under your chosen conditions.

Even though this API can be useful for developers that want to adopt or try N|Solid metrics and don't have access to static configuration data, it’s important to take into account that the API is using V8 API and runs in a JavaScript thread which can block the event loop and affect application performance. To get the maximum performance and security it is advisable to use the N|Solid console instead.

To get started, if you have a configuration management platform or database that knows the hostname and port to your Console, you may obtain it and then invoke nsolid.start() to connect and begin reporting metrics.

const nsolid = require('nsolid')
nsolid.start({
  command: 'nsolid-command-host.local:9001',
  tags: ['nsolid-awesome', 'Auth service'],
  app: 'nsolid-awesome',
  appVersion: '1.0.0'
})

You can now connect your processes running the code above and passing the required parameters.

To see your configuration, you can use nsolid.config and you can also take heap snapshots with nsolid.snapshot() and have the resulting snapshot(s) saved on your N|Solid console.

const nsolid = require('nsolid')
let takeNSnapshots = 3
let snapshotsInterval = null
function takeSnapshot () {
  // Stop taking spanshots 
  if (!takeNSnapshots) {
    console.log('No more snapshots will be taken')
    clearInterval(snapshotInterval)
      return
  }
    try {
    console.log('Took snapshot')
    takeNSnapshots--
      nsolid.snapshot()
  } catch (err) {
      // The snapshot could not be created!
  }
}
function veryMemoryExpensiveFunction () {
  // Take a snapshot every two seconds
  snapshotInterval = setInterval(takeSnapshot, 2000)
    doMemoryExpesinveWorkDontAsk()
}
veryMemoryExpensiveFunction()

The code above sets an interval to generate multiple heap snapshots so you can later compare them in the console in order to track down a memory leak.

You can also create CPU profiles with nsolid.profile([duration]) The profile duration is 600,000 milliseconds, which is the default if none is specified, and profiler can be stopped before the duration expires.

const nsolid = require('nsolid')
nsolid.profile(600000 /* durationMilliseconds */, err => {
  if (err) {
    // The profile could not be started!
  }
})

To find out more, about getting Heap Snapshots, CPU Profiles or other metrics using N|Solid JS API please read the docs here.

You can also create synchronous and asynchronous calls using nsolid.metrics() which will retrieve a list of environment and process metrics.

Advantages

It’s possible to configure your code as preferred
You can pause the metrics and consume it whenever necessary
Generate Heap Snapshots and CPU profiles programmatically
Access to all N|Solid metrics from your own code

Download NSolid 4.2.1 here

You can download the latest version of N|Solid via http://accounts.nodesource.com or visit https://downloads.nodesource.com/ directly. To keep up to date with new product releases, new features, and all the latest with Node.js and NodeSource, follow us on twitter @nodesource.

The NodeSource platform offers a high-definition view of the performance, security and behavior of Node.js applications and functions.

Start for Free