/metrics

Simple and pluggable business metrics

Primary LanguageJavaScript

Build Status

metrics

Simple and pluggable business metrics. It makes internal reporting much easier.

Segment uses metrics as an internal API to power our dashboards, auto-updating spreadsheets, and other admin services.

var Metrics = require('metrics');

Metrics()
  .every('5m', charges('stripe-key'))
  .every('10m', subscriptions('stripe-key'))
  .every('1h', helpscout('helpscout-key'))

  .use(function (metrics) {
    metrics.on('stripe charges last month', function (val) {
      geckoboard('widget-id').number(val));
    });
  });

Plugins add their own metrics using keys like stripe charges last month. Each company can customize their reporting by writing their own plugins.

The code above calculates revenue and support metrics that can then be visualized with a dashboard, like Geckoboard:

It's easy to get started: there's already plugins for Stripe, Helpscout, AWS, and others.

It separates data and views: split the raw data from how its presented.

It's dashboard agnostic: so you can use Geckoboard, Ducksboard, Leftronic, or your own internal dashboard.

It pushes you in the right direction: use Segment's metrics expertise to avoid the wrong metrics.

Its an internal metrics API: Segment uses the metrics-express plugin to serve our metrics to other internal services (like admin tools and auto-updating spreadsheets).

Installation

$ npm install segmentio/metrics

How does it work?

Metrics is super simple. Plugins write data into a key value store, and other plugins then send that data to dashboards or other reporting tools.

A plugin can learn about how much you're making on Stripe, and make that data available:

var stripe = require('stripe')(key);

module.exports = function (metrics) {
  stripe.charges.list(function (err, charges) {
    metrics.set('total charges', charges.length);
  });
};

and another plugin can push the data to a geckoboard:

var geckoboard = require('geckobard')('api-key');

module.exports = function (metrics) {
  metrics.on('total charges', geckoboard('widget-id').number);
}

and now you have your first dashboard.

Metrics()
  .every('5m', charges)
  .use(dashboard);

Plugins

Existing plugins for metrics can tell you:

It's normal for every company to care about different metrics. If your plugin can help others do easier reporting, pull request this Readme.md to add your own plugin to this list.

API

At its core, metrics is a simple key value store. Plugins put data into a hashtable, and other plugins then use that data to update dashboards, send emails, or really anything you want.

new Metrics()

Create a new Metrics instance.

.set(key, val)

Set a key / val pair.

.get(key)

Get a value at key.

.keys()

Get a list of keys.

.every(interval, plugin)

Add a metrics plugin to run on an interval.

var metrics = new Metrics()
  .every('5m', function (metrics) {
    metrics.set('hours', new Date().getHours());
    metrics.set('minutes', new Date().getMinutes());
  });

.on(keys.., cb)

Listen for when one or more keys become available.

var metrics = new Metrics()
  .every('5m', function (metrics) {
    metrics.set('hours', new Date().getHours());
    metrics.set('minutes', new Date().getMinutes());
  });

metrics.on('hours', 'minutes', function (h, m) {
  console.log('time update: ' + h + ':' + m);
});

.use(plugin)

Add a plugin that consumes metrics data.

new Metrics()
  .every('5m', function (metrics) {
    metrics.set('hours', new Date().getHours());
    metrics.set('minutes', new Date().getMinutes());
  })
  .use(function (metrics) {
    metrics.on('hours', 'minutes', function (h, m) {
      console.log('time update: ' + h + ':' + m);
    });
  });

License

WWWWWW||WWWWWW
 W W W||W W W
      ||
    ( OO )__________
     /  |           \
    /o o|    MIT     \
    \___/||_||__||_|| *
         || ||  || ||
        _||_|| _||_||
       (__|__|(__|__|