Git Product home page Git Product logo

node-insights's Introduction

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 app id, insert key, and url.

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

var insights = new Insights({
  appId: <YOUR_APP_ID>,
  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. Data is held in the queue until either the number of items exceeds maxPending or the send timer goes off.

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({
  '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:

{
  'appId': 42,
  '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

resources

New Relic docs about inserting custom events and default attributes

node-insights's People

Contributors

cirne avatar mattpage avatar pmark avatar wraithan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.