Skip to content
This repository was archived by the owner on Feb 18, 2021. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions lib/express-statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@ var extend = require('obj-extend');
var Lynx = require('lynx');

module.exports = function expressStatsdInit (options) {
// Function called on response finish that sends stats to statsd
function sendStats(req, res, client, startTime, options) {
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);
}

options = extend({
requestKey: 'statsdKey',
host: '127.0.0.1',
port: 8125
port: 8125,
sendStats: sendStats
}, options);

assert(options.requestKey, 'express-statsd expects a requestKey');
Expand All @@ -16,31 +31,20 @@ module.exports = function expressStatsdInit (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);

function send () {
options.sendStats(req, res, client, startTime, options);
cleanup();
}

// Function to clean up the listeners we've added
function cleanup() {
res.removeListener('finish', sendStats);
res.removeListener('finish', send);
res.removeListener('error', cleanup);
res.removeListener('close', cleanup);
}

// Add response listeners
res.once('finish', sendStats);
res.once('finish', send);
res.once('error', cleanup);
res.once('close', cleanup);

Expand Down
28 changes: 28 additions & 0 deletions test/express-statsd.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,34 @@ describe('An express server', function () {
});
});

describe('with a custom sendStats function', function () {
utils.runServer(1337, [
expressStatsd({
sendStats: function (req, res, client, startTime, options) {
var statusCode = res.statusCode;
client.increment('node_test.int.' + statusCode);
client.decrement('node_test.int.fail');
client.timing('node_test.some_service.task.time', 500);
client.gauge('gauge.one', 100);
client.set('set.one', 10);
}
}),
function (req, res) {
res.send(200);
}
]);
utils.saveRequest('http://localhost:1337');
utils.getStatsdMessages();

it('should read from that key', function () {
expect(this.messages[0]).to.match(/^node_test\.int\.200:\d\|c$/);
expect(this.messages[1]).to.match(/^node_test\.int\.fail:-1\|c$/);
expect(this.messages[2]).to.match(/^node_test\.some_service\.task\.time:\d|ms$/);
expect(this.messages[3]).to.match(/^gauge\.one:100|g$/);
expect(this.messages[4]).to.match(/^set\.one:10|s$/);
});
});

describe('receiving a request with a custom lynx', function () {
utils.runServer(1337, [
function (req, res, next) {
Expand Down