From 2ce4eef5074102cb7b64a4574aec4b8f711c172f Mon Sep 17 00:00:00 2001 From: Ranjeetkaur Date: Wed, 17 May 2017 13:40:21 +0100 Subject: [PATCH 01/10] Add headers as stat tags name_of_the_header:value_of_the_header --- .idea/jsLibraryMappings.xml | 6 ++ .idea/misc.xml | 16 +++ .idea/modules.xml | 8 ++ .idea/node-connect-datadog.iml | 8 ++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 191 +++++++++++++++++++++++++++++++++ lib/index.js | 6 ++ 7 files changed, 241 insertions(+) create mode 100644 .idea/jsLibraryMappings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/node-connect-datadog.iml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml new file mode 100644 index 0000000..b8387eb --- /dev/null +++ b/.idea/jsLibraryMappings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..eabe228 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..472aeff --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/node-connect-datadog.iml b/.idea/node-connect-datadog.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/node-connect-datadog.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..6ee56bf --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,191 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1495010900003 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index dd47dbf..c2269cc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -39,6 +39,12 @@ module.exports = function (options) { statTags.push("path:" + baseUrl + req.path); } + if (options.headers && options.headers.length > 0) { + options.headers.forEach((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); From 7984059c8e3581d065824945fdfa8f4203c0b9bf Mon Sep 17 00:00:00 2001 From: Ranjeetkaur Date: Wed, 17 May 2017 14:33:44 +0100 Subject: [PATCH 02/10] Include the tag only if the header value exists --- .idea/workspace.xml | 14 ++++++++------ lib/index.js | 4 +++- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6ee56bf..b65be53 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -26,8 +27,8 @@ - - + + @@ -125,6 +126,7 @@ + @@ -141,12 +143,12 @@ - @@ -181,8 +183,8 @@ - - + + diff --git a/lib/index.js b/lib/index.js index c2269cc..e4e415f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -41,7 +41,9 @@ module.exports = function (options) { if (options.headers && options.headers.length > 0) { options.headers.forEach((header) => { - statTags.push(`${header}:${req.headers[header]}`); + if (req.headers[header]) { + statTags.push(`${header}:${req.headers[header]}`); + } }); } From 231238034fd1372d377f18f61f4c7b935cdea0a2 Mon Sep 17 00:00:00 2001 From: Ranjeetkaur Date: Wed, 17 May 2017 15:11:28 +0100 Subject: [PATCH 03/10] Added .idea to ignore --- .gitignore | 1 + .idea/jsLibraryMappings.xml | 6 - .idea/misc.xml | 16 --- .idea/modules.xml | 8 -- .idea/node-connect-datadog.iml | 8 -- .idea/vcs.xml | 6 - .idea/workspace.xml | 193 --------------------------------- 7 files changed, 1 insertion(+), 237 deletions(-) delete mode 100644 .idea/jsLibraryMappings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/node-connect-datadog.iml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.gitignore b/.gitignore index 54cfd51..11a405b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ npm-debug.log .DS_Store *.swp *~ +.idea diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml deleted file mode 100644 index b8387eb..0000000 --- a/.idea/jsLibraryMappings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index eabe228..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 472aeff..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/node-connect-datadog.iml b/.idea/node-connect-datadog.iml deleted file mode 100644 index c956989..0000000 --- a/.idea/node-connect-datadog.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index b65be53..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,193 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - DEFINITION_ORDER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1495010900003 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From d9cb920f814cbb472a9ddef01562c5d9f70df61c Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Mon, 19 Jun 2017 16:40:57 +0200 Subject: [PATCH 04/10] allows us to collect metrics from swagger api applications --- .gitignore | 2 ++ lib/index.js | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 54cfd51..88d109f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ npm-debug.log .DS_Store *.swp *~ + +.idea/ diff --git a/lib/index.js b/lib/index.js index dd47dbf..9d63368 100644 --- a/lib/index.js +++ b/lib/index.js @@ -18,14 +18,15 @@ module.exports = function (options) { res.end = end; res.end(chunk, encoding); - if (!req.route || !req.route.path) { + var apiPath = (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); + if (!apiPath) { return; } var baseUrl = (base_url !== false) ? req.baseUrl : ''; var statTags = [ - "route:" + baseUrl + req.route.path - ].concat(tags); + "route:" + baseUrl + apiPath + ].concat(tags); if (options.method) { statTags.push("method:" + req.method.toLowerCase()); From 94b63215ad2e4610d7a3f54a6cb1ddee612068fe Mon Sep 17 00:00:00 2001 From: Andy Duncan Date: Thu, 11 Jan 2018 13:32:21 +0200 Subject: [PATCH 05/10] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4b211be..88d109f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ npm-debug.log .DS_Store *.swp *~ + .idea/ From e8887522799d91971195dc856677f2e4cd89e66b Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Thu, 11 Jan 2018 14:29:21 +0200 Subject: [PATCH 06/10] revert to earlier node syntax --- lib/index.js | 106 +++++++++++++++++++++++++-------------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/lib/index.js b/lib/index.js index bd643cb..d90af96 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,62 +1,62 @@ 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); + 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); var apiPath = (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); - if (!apiPath) { - return; - } + if (!apiPath) { + return; + } - var baseUrl = (base_url !== false) ? req.baseUrl : ''; - var statTags = [ - "route:" + baseUrl + apiPath + var baseUrl = (base_url !== false) ? req.baseUrl : ''; + var statTags = [ + "route:" + baseUrl + apiPath ].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 (options.headers && options.headers.length > 0) { - options.headers.forEach((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(); - }; + 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 (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(); + }; }; From bc01c61a5c867d7c894ecb8cce7de5869a88b5f8 Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Fri, 12 Jan 2018 11:23:13 +0200 Subject: [PATCH 07/10] use the __route value if there is one. removed some configurable options as they are not suitable for our use. --- lib/index.js | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/index.js b/lib/index.js index d90af96..3aef10d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,9 @@ -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 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) { @@ -18,27 +16,20 @@ module.exports = function (options) { res.end = end; res.end(chunk, encoding); - var apiPath = (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); + var apiPath = req.__route || (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); if (!apiPath) { return; } - var baseUrl = (base_url !== false) ? req.baseUrl : ''; - var statTags = [ - "route:" + baseUrl + apiPath - ].concat(tags); + var baseUrl = req.baseUrl; - if (options.method) { - statTags.push("method:" + req.method.toLowerCase()); - } + var statTags = [].concat(tags); - if (options.protocol && req.protocol) { - statTags.push("protocol:" + req.protocol); - } + statTags.push('route:' + baseUrl + apiPath); - if (path !== false) { - statTags.push("path:" + baseUrl + req.path); - } + statTags.push('method:' + req.method.toLowerCase()); + + statTags.push('protocol:' + req.protocol); if (options.headers && options.headers.length > 0) { options.headers.forEach(function (header) { @@ -49,7 +40,7 @@ module.exports = function (options) { } if (response_code) { - statTags.push("response_code:" + res.statusCode); + statTags.push('response_code:' + res.statusCode); datadog.increment(stat + '.response_code.' + res.statusCode, 1, statTags); datadog.increment(stat + '.response_code.all', 1, statTags); } From 59f189e29370d03425a2b9c9b522e89c02dec3bd Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Fri, 12 Jan 2018 13:13:36 +0200 Subject: [PATCH 08/10] don't use the baseUrl if using __route --- lib/index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/index.js b/lib/index.js index 3aef10d..c146d12 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,16 +16,22 @@ module.exports = function (options) { res.end = end; res.end(chunk, encoding); - var apiPath = req.__route || (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); - if (!apiPath) { - return; + 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 baseUrl = req.baseUrl; - var statTags = [].concat(tags); - statTags.push('route:' + baseUrl + apiPath); + statTags.push('route:' + route); statTags.push('method:' + req.method.toLowerCase()); From b272d9959ffc0e3c422f4dfacd4494d7db16b876 Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Fri, 12 Jan 2018 13:46:46 +0200 Subject: [PATCH 09/10] don't use the baseUrl at all --- lib/index.js | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index c146d12..56a4ed1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,17 +16,9 @@ module.exports = function (options) { 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 route = req.__route || (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); + if (!route) { + return; } var statTags = [].concat(tags); From 1259915dbf53520f237ae6fa050ec4c2c394d51e Mon Sep 17 00:00:00 2001 From: andy-duncan Date: Fri, 12 Jan 2018 13:47:44 +0200 Subject: [PATCH 10/10] Revert "don't use the baseUrl at all" This reverts commit b272d9959ffc0e3c422f4dfacd4494d7db16b876. --- lib/index.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 56a4ed1..c146d12 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,9 +16,17 @@ module.exports = function (options) { res.end = end; res.end(chunk, encoding); - var route = req.__route || (req.route && req.route.path) || (req.swagger && req.swagger.apiPath); - if (!route) { - return; + 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);