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

funnel

v0.1.0

Published

Funnel metrics from various sources into StatsD

Downloads

12

Readme

Funnel

An easy way to fetch aggregate metrics from various sources, and transport them into StatsD.

Sources

The following is a list of currently supported sources (via plugin); examples of their usage can be found further below:

Support for additional sources is always welcome.

Why

At Fictive Kin, we use StatsD and Graphite to measure everything (for small values of everything).

Our apps send event metrics (such as "a user grabbed a new image with Gimme Bar") to StatsD in the code, but we also needed a system that could fetch aggregate metrics (such as "the Gimme Bar load balancer's current average latency"). Enter Funnel.

Funnel makes it easy to fetch all of those measurements in one common framework, and handles transportation to StatsD.

Usage and Examples

Funnel was designed to be easy to use and maintain. Here's a very simple example that uses the json plugin to fetch the current USD to CAD conversion rate, and simply display it (the data is not actually sent to StatsD in this example):

var funnel = require('../funnel/funnel');

funnel.collect(
  funnel.json({
    services: {
      'usdcad': 'rates.CAD'
    },
    from: 'http://openexchangerates.org/api/latest.json'
  })
).display();

To send the data to StatsD, you'd simply change the call to display() to toStatsD(host, port) (and port is optional):

funnel.collect(
  /* same as before */
).toStatsD('statsd.example.com');

The collect() method takes any number of parameters, so you can queue them up:

var cad = funnel.json({
  services: {'usdcad': 'rates.CAD'},
  from: 'http://openexchangerates.org/api/latest.json'
});

var eur = funnel.json({
  services: {'usdeur': 'rates.EUR'},
  from: 'http://openexchangerates.org/api/latest.json'
});

funnel.collect(cad, eur).display();

The above code actually fetches latest.json twice. If you'd like to optimize this, you can collect multiple services for each from sources:

var currency = funnel.json({
  services: {
    'usdcad': 'rates.CAD'
    'usdeur': 'rates.EUR'
  },
  from: 'http://openexchangerates.org/api/latest.json'
});
funnel.collect(currency).display();

That's the gist of how Funnel works. Next, we'll cover the specific fetcher plugins. To make the code more clear, assume the following structure for these examples:

var funnel = require('../funnel/funnel');

var source = EXAMPLE_GOES_HERE;

funnel.collect(source).display();

cloudwatch

Fetch data from Amazon's CloudWatch API. This is useful for correlating AWS-specific metrics with code metrics (think average load balancer latency vs. API method call volume).

source = funnel.cloudwatch({
  services: {
    'load-balancer-bar-requests': { // metric name
      namespace: 'AWS/ELB', // CloudWatch-specific data
      metric: 'RequestCount',
      name: 'LoadBalancerName',
      value: 'load-balancer',
      unit: 'Count',
      type: 'Sum',
    }
    // more services can be collected from the same `from`, here
  }
  from: {
    id: AWS_ID,
    secret: AWS_SECRET
  }
});

The CloudWatch data is very specific to how CloudWatch (and the AWS API) returns data. A little bit more information can be found in this aws-lib example.

dbi

Query and fetch data from any database supported by Node-DBI.

source = funnel.dbi({
  services: {
    'example-active-users': {
      query: "SELECT COUNT(user_id) FROM api_last_visited WHERE activity > NOW() - INTERVAL '1 minute'",
      callback: funnel.dbiSolo
    },
  }.
  from: {
    host: 'db.example.com',
    user: 'username',
    password: 'password',
    database: 'database_name',
    adapter: 'pg' // postgres
  }
});

This plugin uses a callback method to fetch data. Notice the funnel.dbiSolo call above. This is a simple helper method that fetches the first column from the first row of the resultset, but sometimes a more robust callback is necessary. Sometimes you might even want to fetch multiple metrics from the same query. Here's an example of how to do that:

source = funnel.dbi({
  'content-type.%': {
    query: 'SELECT content_type AS name, COUNT(content_type) AS num FROM content GROUP BY content_type',
    callback: function (result) {
      var ret = {};
      result.forEach(function (row) {
        ret[row['name']] = row['num'];
      });
      return ret;
    }
  },
  from: example_db_params
});

This callback returns an object containing keys and values. Note also that the metric name contains a %. For each pair returned from the callback, a metric is generated for the key (which replaces % in the metric name), and the reading is the pair's value. The query is only run once, however.

json

This plugin is covered in some detail above, but here's a simple example:

source = funnel.json({
  services: {
    'usdcad': 'rates.CAD',
    'usdeur': 'rates.EUR'
  },
  from: 'http://openexchangerates.org/api/latest.json'
});

mongodb

Collect data from MongoDB.

source = funnel.mongo({
  services: {
    'users': funnel.COUNT,
    'assets': funnel.COUNT,
    'pro_users': {
      'collection': 'users',
      'query': {'type': 'pro'}
    }
  },
  from: 'mongodb://db.example.com/databasename'
});

This example is pretty straightforward. This collects counts for the users and assets MongoDB collections (note the special funnel helper funnel.COUNT). For the pro_users metrics, a query and collection are supplied. The from is a MongoDB connection URL.

munin

Collect data from nodes that are running a Munin (plugin) client.

source = funnel.munin({
  services: {
    'uptime': funnel.ALL,
    'cpu': ['user', 'idle', 'steal']
  },
  from: 'server.example.com'
});

The helper funnel.ALL collects all plugin/returned data under this heading.

nagios

Most of the data available to Nagios is queryable via Munin, but since we're already running Nagios (and collecting this data as part of our general systems monitoring strategy), it's useful to collect some data directly from Nagios.

source = funnel.nagios({
  source: 'http://nagios.example.com:64001/state',
  services: {
    'MongoDB queues': ['readers_queues','writers_queues'],
    'MongoDB Connect Check': funnel.ALL
  },
  from: ['db1.example.com', 'db2.example.com']
});

You'll notice that funnel.ALL is used here, too. This collects all performance data under this heading. from can be an array (or a string for a single server, but you are running MongoDB in a replica set, right? (-: ).