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

Commit cc04b2d

Browse files
rue-mchangofek
authored andcommitted
Pipe character bug fix with tests (#2)
* update package.json, version bump to 0.0.7 * general cleanup * replace pipe char * improve the function documentation * improve the documentation * New unit tests for connect-datadog library, full coverage * Add tracking for request method type: - Configurable with 'method' option on middleware initialization * Remove duplicate `method` handling It already existed so adding the code was not the appropriate path to take. In the mean time, I standardized the `method` option to be treated similarly to how the other options are treated * Re-add mistakenly removed test * Remove unnecessary setup on req.method That’s now part of test setup for the request object * Enable base_url in tags, base_url tests, handle passed options consistently
1 parent 6574a3a commit cc04b2d

File tree

5 files changed

+4989
-24
lines changed

5 files changed

+4989
-24
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ All options are optional.
1818
* `stat` *string* name for the stat. `default = "node.express.router"`
1919
* `tags` *array* of tags to be added to the histogram. `default = []`
2020
* `path` *boolean* include path tag. `default = false`
21+
* `base_url` *boolean* include baseUrl. `default = false`
2122
* `method` *boolean* include http method tag. `default = false`
2223
* `protocol` *boolean* include protocol tag. `default = false`
2324
* `response_code` *boolean* include http response codes. `default = false`
25+
* `delim` *string* char to replace pipe char with in the route `default = '-'`
2426

2527
## License
2628

lib/index.js

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
var DD = require("node-dogstatsd").StatsD;
1+
const DD = require("node-dogstatsd").StatsD;
22

33
module.exports = function (options) {
4-
var datadog = options.dogstatsd || new DD();
5-
var stat = options.stat || "node.express.router";
6-
var tags = options.tags || [];
7-
var options.path = options.path || false;
8-
var response_code = options.response_code || false;
4+
let datadog = options.dogstatsd || new DD();
5+
let stat = options.stat || "node.express.router";
6+
let tags = options.tags || [];
7+
let path = options.path || false;
8+
let base_url = options.base_url || false;
9+
let method = options.method || false;
10+
let protocol = options.protocol || false;
11+
let response_code = options.response_code || false;
12+
let DELIM = options.delim || '-';
13+
let REGEX_PIPE = /\|/g;
14+
15+
/**
16+
* Checks if str is a regular expression and stringifies it if it is.
17+
* Returns a string with all instances of the pipe character replaced with
18+
* the delimiter.
19+
* @param {*} str The string to check for pipe chars
20+
* @return {string} The input string with pipe chars replaced
21+
*/
22+
function replacePipeChar(str) {
23+
if (str instanceof RegExp) {
24+
str = str.toString();
25+
}
26+
27+
return str && str.replace(REGEX_PIPE, DELIM);
28+
}
929

1030
return function (req, res, next) {
1131
if (!req._startTime) {
1232
req._startTime = new Date();
1333
}
1434

15-
var end = res.end;
35+
let end = res.end;
1636
res.end = function (chunk, encoding) {
1737
res.end = end;
1838
res.end(chunk, encoding);
@@ -21,29 +41,28 @@ module.exports = function (options) {
2141
return;
2242
}
2343

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

28-
if (options.method) {
29-
statTags.push("method:" + req.method.toLowerCase());
47+
if (method !== false) {
48+
statTags.push(`method:${req.method.toLowerCase()}`);
3049
}
3150

32-
if (options.protocol && req.protocol) {
33-
statTags.push("protocol:" + req.protocol);
51+
if (protocol && req.protocol) {
52+
statTags.push(`protocol:${req.protocol}`);
3453
}
3554

36-
if (options.path !== false) {
37-
statTags.push("path:" + req.path);
55+
if (path !== false) {
56+
statTags.push(`path:${req.path}`);
3857
}
3958

4059
if (response_code) {
41-
statTags.push("response_code:" + res.statusCode);
42-
datadog.increment(stat + '.response_code.' + res.statusCode , 1, statTags);
43-
datadog.increment(stat + '.response_code.all' , 1, statTags);
60+
statTags.push(`response_code:${res.statusCode}`);
61+
datadog.increment(`${stat}.response_code.${res.statusCode}`, 1, statTags);
62+
datadog.increment(`${stat}.response_code.all`, 1, statTags);
4463
}
4564

46-
datadog.histogram(stat + '.response_time', (new Date() - req._startTime), 1, statTags);
65+
datadog.histogram(`${stat}.response_time`, new Date() - req._startTime, 1, statTags);
4766
};
4867

4968
next();

0 commit comments

Comments
 (0)