Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Pipe character bug fix with tests #2

Merged
merged 13 commits into from
Apr 27, 2018
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ All options are optional.
* `stat` *string* name for the stat. `default = "node.express.router"`
* `tags` *array* of tags to be added to the histogram. `default = []`
* `path` *boolean* include path tag. `default = false`
* `base_url` *boolean* include baseUrl. `default = false`
* `method` *boolean* include http method tag. `default = false`
* `protocol` *boolean* include protocol tag. `default = false`
* `response_code` *boolean* include http response codes. `default = false`
* `delim` *string* char to replace pipe char with in the route `default = '-'`

## License

Expand Down
59 changes: 39 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
var DD = require("node-dogstatsd").StatsD;
const 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 options.path = options.path || false;
var response_code = options.response_code || false;
let datadog = options.dogstatsd || new DD();
let stat = options.stat || "node.express.router";
let tags = options.tags || [];
let path = options.path || false;
let base_url = options.base_url || false;
let method = options.method || false;
let protocol = options.protocol || false;
let response_code = options.response_code || false;
let DELIM = options.delim || '-';
let REGEX_PIPE = /\|/g;

/**
* Checks if str is a regular expression and stringifies it if it is.
* Returns a string with all instances of the pipe character replaced with
* the delimiter.
* @param {*} str The string to check for pipe chars
* @return {string} The input string with pipe chars replaced
*/
function replacePipeChar(str) {
if (str instanceof RegExp) {
str = str.toString();
}

return str && str.replace(REGEX_PIPE, DELIM);
}

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

var end = res.end;
let end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);
Expand All @@ -21,29 +41,28 @@ module.exports = function (options) {
return;
}

var statTags = [
"route:" + req.route.path
].concat(tags);
const baseUrl = (base_url !== false) ? req.baseUrl : '';
let statTags = [`route:${baseUrl + replacePipeChar(req.route.path)}`].concat(tags);

if (options.method) {
statTags.push("method:" + req.method.toLowerCase());
if (method !== false) {
statTags.push(`method:${req.method.toLowerCase()}`);
}

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

if (options.path !== false) {
statTags.push("path:" + req.path);
if (path !== false) {
statTags.push(`path:${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);
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);
datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
};

next();
Expand Down
Loading