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

Pipe character bug fix with tests #20

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ All options are optional.
* `dogstatsd` node-dogstatsd client. `default = new require("node-dogstatsd").StatsD()`
* `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 = true`
* `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,17 +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 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 @@ -20,30 +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