npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-insights

v1.1.0

Published

Submit and query New Relic Insights data

Downloads

772

Readme

node-insights

Submit and query New Relic Insights data.

installation

npm install node-insights --save

usage

Create an Insights instance and pass in an object with your New Relic account id, insert key, and query key.

For more information about NewRelic Insights Docs : https://docs.newrelic.com/docs/insights/new-relic-insights

configuration

The constructor to the insights object accepts an object with the following properties:

  • accountId (string, required) - your newrelic insights account id
  • insertKey (string, required if you are going to add or send data) - your newrelic insert key
  • queryKey (string, required if you are going to query data) - your newrelic query key
  • timerInterval (integer, default=10000) - the timer interval (in milliseconds) that is used for sending data
  • maxPending (integer, default=1000) - the maximum number of items held in the queue before being sent
  • defaultEventType (string, default='data') - when adding data, you can specify the eventType that is sent to New Relic. If you don't specify the eventType, the defaultEventType is used.
  • enabled (boolean, default=true) - enable/disable the sending of insights data.
  • gzip (boolean, default=false) - enable/disable the sending of insights data zipped via gzip.

NOTE: insertKey, queryKey, and accountId can all be found in one's NewRelic dashboard.

var Insights = require('node-insights');

var insights = new Insights({
  insertKey: '<YOUR_INSERT_KEY>',
  queryKey: '<YOUR_QUERY_KEY>',
  accountId: '<YOUR_ACCOUNT_ID>'
});

insights.add({
  someInt: 42,
  someArray: [ 'apples', 'peaches', 'bananas' ],
  someObject: {
    'foo': 'bar'
  }
});

insights.query('SELECT count(*) FROM data', function(err, responseBody) {
  // ...
});

// you can construct NRQL from objects using a similar pattern to Rails ActiveRecord, etc.
var q = { select : 'count(*)', from: 'PageView',
          where  : { userAgentOS: ['Windows', 'Mac'] },
          since  : '1 day ago', facet: 'countryCode'};


// nrql == "SELECT count(*) FROM PageView WHERE userAgentOs IN ('Windows', 'Mac') SINCE 1 day ago FACET countryCode"
var nrql = insights.nrql(q);

// will generate nrql from q and run normally
insights.query(q, function(err, responseBody) {
    // ...
})

adding data

By default, adding data will start the send timer (unless the enabled property is false). Data is held in the queue until either the number of items exceeds maxPending or the send timer goes off.

Call .finish() after the final call to .add() in order to stop the send timer after the queue is flushed.

NOTE: you may want to include an appId in your data. See here

data format

New Relic Insights expects key/value pairs. As a convenience, the Insights object will flatten Object and Array data.

Adding this data object:

insights.add({
  'appId': 42,
  'purchase': {
    "account":3,
    "amount":259.54
  }
}, 'purchase');

Actually flattens out and is sent like this:

{
  'appId': 42,
  'eventType': 'purchase',
  'purchase.account':3,
  'purchase.amount':259.54
}

Array data flattens out too:

  insights.add({
    'randomWords': [ "card", "bean", "chair", "box" ]
  });

but it is less pretty:

{
  'eventType': 'data',
  'randomWords.0': 'card',
  'randomWords.1': 'bean',
  'randomWords.2': 'chair',
  'randomWords.3': 'box'
}

event types

When you add data, you can specify the eventType that is sent to New Relic. If you don't specify the eventType, the defaultEventType (from the initial config is used). The defaultEventType defaults to the string 'data'. Awesome!

insights.add({ ... }, 'my-custom-event-type');

timestamps

By default Insights will use the time data is sent to the server as the timestamp. Because this library buffers data for up to 10s, we will automatically add a timestamp when you call insights.add().

If you provide your own timestamp (keeping in mind the date has to be within a day of the Insight server's time), the library will not overwrite the user provided one.

querying data

To retrieve data from Insights you will need to use your query key to execute NRQL queries.

insights.query(nrqlQuery, function(err, responseBody) {
  // ...
});

tests

Run the tests

grunt test

docs

Run js-doc and generate the documentation

grunt docs

resources

New Relic docs about inserting custom events and default attributes