Skip to content
This repository has been archived by the owner on Sep 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from boomtrain/customize
Browse files Browse the repository at this point in the history
Customizing for our needs
  • Loading branch information
gogreen53 committed Jul 10, 2015
2 parents 4e3ee7d + cddea3e commit e32e151
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/node_modules
.idea
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# express-statsd [![Build status](https://travis-ci.org/uber/express-statsd.png?branch=master)](https://travis-ci.org/uber/express-statsd)
# express-statsd

[StatsD](https://github.com/etsy/statsd/) route monitoring middleware for
[Connect](https://github.com/senchalabs/connect)/[Express](https://github.com/visionmedia/express).
Expand Down
62 changes: 53 additions & 9 deletions lib/express-statsd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var assert = require('assert');
var extend = require('obj-extend');
var Lynx = require('lynx');

Expand All @@ -9,25 +8,24 @@ module.exports = function expressStatsdInit (options) {
port: 8125
}, options);

assert(options.requestKey, 'express-statsd expects a requestKey');

var client = options.client || new Lynx(options.host, options.port, options);

return function expressStatsd (req, res, next) {
var startTime = new Date().getTime();

// Function called on response finish that sends stats to statsd
function sendStats() {
var key = req[options.requestKey];
key = key ? key + '.' : '';

// Status Code
var statusCode = res.statusCode || 'unknown_status';
client.increment(key + 'status_code.' + statusCode);

// Response Time
var duration = new Date().getTime() - startTime;
client.timing(key + 'response_time', duration);

var keys = generateStatNames(req.route);

keys.forEach(function(key) {
client.increment([key, 'status_code', statusCode].join('.'));
client.timing([key, 'response_time'].join('.'), duration);
})

cleanup();
}
Expand All @@ -49,3 +47,49 @@ module.exports = function expressStatsdInit (options) {
}
};
};

/* Automatically generate the stat paths from express routing by accessing
* req.route object.
*
* {
* path: '/users/:list/:subscribed',
* method: 'get',
* callbacks: [ [Function] ],
* keys:
* [ { name: 'list', optional: false },
* { name: 'subscribed', optional: false },
* regexp: /^\/users\/(?:([^\/]+?))\/(?:([^\/]+?))\/?$/i,
* params: [ list: '666', subscribed: 'true' ]
* }
*
* We replace the ':' of the url params with a '~' because the following
* characters are reserved by statsd or graphite: !#:@*[{?/
*
* This will result in stats like so:
*
* express.http.get.users
* - status_code.[2xx,4xx].count
* - response_time.upper_90
* express.http.post.users
* express.http.get.users.~id
* - status_code.[2xx,4xx].count
* - response_time.upper_90
* express.http.post.users.~list
* express.http.get.users.~list.~subscribed
*/
function generateStatNames(routeObject) {
var path = routeObject.path;
path = path.replace(/:/g, '~');
var pathParts = path.split('/');

var resource = pathParts[1];
var resourcePath = pathParts.slice(2, pathParts.length).join('_');

var requestMethod = routeObject.method || 'unknown_method';
var baseStat = ['express.http', requestMethod.toLowerCase(), resource].join('.');

return [
baseStat,
[baseStat, resourcePath].join('.')
];
};
19 changes: 2 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@
"name": "express-statsd",
"description": "Statsd route monitoring middleware for connect/express",
"version": "0.3.0",
"homepage": "https://github.com/uber/express-statsd",
"author": {
"name": "Matt Morgan",
"email": "matt@mlmorg.com",
"url": "http://mlmorg.com"
},
"keywords": [
"connect",
"express",
"statsd",
"lynx",
"monitoring"
],
"homepage": "https://github.com/boomtrain/express-statsd",
"repository": {
"type": "git",
"url": "git://github.com/uber/express-statsd.git"
},
"bugs": {
"url": "https://github.com/uber/express-statsd/issues"
"url": "git://github.com/boomtrain/express-statsd.git"
},
"licenses": [
{
Expand Down

0 comments on commit e32e151

Please sign in to comment.