Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Headers as Tag values, and Swagger Route values. #21

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ npm-debug.log
.DS_Store
*.swp
*~

.idea/
106 changes: 56 additions & 50 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
var DD = require("node-dogstatsd").StatsD;
var DD = require('node-dogstatsd').StatsD;

module.exports = function (options) {
var datadog = options.dogstatsd || new DD();
var stat = options.stat || "node.express.router";
var tags = options.tags || [];
var path = options.path || false;
var base_url = options.base_url || false;
var response_code = options.response_code || false;

return function (req, res, next) {
if (!req._startTime) {
req._startTime = new Date();
}

var end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);

if (!req.route || !req.route.path) {
return;
}

var baseUrl = (base_url !== false) ? req.baseUrl : '';
var statTags = [
"route:" + baseUrl + req.route.path
].concat(tags);

if (options.method) {
statTags.push("method:" + req.method.toLowerCase());
}

if (options.protocol && req.protocol) {
statTags.push("protocol:" + req.protocol);
}

if (path !== false) {
statTags.push("path:" + baseUrl + req.path);
}

if (response_code) {
statTags.push("response_code:" + res.statusCode);
datadog.increment(stat + '.response_code.' + res.statusCode , 1, statTags);
datadog.increment(stat + '.response_code.all' , 1, statTags);
}

datadog.histogram(stat + '.response_time', (new Date() - req._startTime), 1, statTags);
};

next();
};
var datadog = options.dogstatsd || new DD();
var stat = options.stat || 'node.express.router';
var tags = options.tags || [];
var response_code = options.response_code || false;

return function (req, res, next) {
if (!req._startTime) {
req._startTime = new Date();
}

var end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);

var route;

if (req.__route) {
route = req.__route;
} else {
var apiPath = (req.route && req.route.path) || (req.swagger && req.swagger.apiPath);
if (!apiPath) {
return;
}
var baseUrl = req.baseUrl;
route = baseUrl + apiPath;
}

var statTags = [].concat(tags);

statTags.push('route:' + route);

statTags.push('method:' + req.method.toLowerCase());

statTags.push('protocol:' + req.protocol);

if (options.headers && options.headers.length > 0) {
options.headers.forEach(function (header) {
if (req.headers[header]) {
statTags.push(header + ':' + req.headers[header]);
}
});
}

if (response_code) {
statTags.push('response_code:' + res.statusCode);
datadog.increment(stat + '.response_code.' + res.statusCode, 1, statTags);
datadog.increment(stat + '.response_code.all', 1, statTags);
}

datadog.histogram(stat + '.response_time', (new Date() - req._startTime), 1, statTags);
};

next();
};
};