From 61e4343a8c280ff2267183d25e1502090f992d54 Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Thu, 23 Mar 2017 14:11:49 -0300 Subject: [PATCH 1/7] Implements group in status code metrics --- src/index.js | 9 ++++++++- src/normalizePath.js | 1 - src/normalizeStatusCode.js | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/normalizeStatusCode.js diff --git a/src/index.js b/src/index.js index c76b526..7bc2872 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const onFinished = require('on-finished'); const promClient = require('prom-client'); const normalizePath = require('./normalizePath'); +const normalizeStatusCode = require('./normalizeStatusCode') function matchVsRegExps(element, regexps) { for (let regexp of regexps) { @@ -116,7 +117,12 @@ function main(opts) { labels = {'status_code': 0}; let timer = metrics[httpMtricName].startTimer(labels); onFinished(res, () => { - labels.status_code = res.statusCode; + if (opts.normalizeStatusCode) { + labels.status_code = main.normalizeStatusCode(req, opts); + } else { + labels.status_code = res.statusCode; + } + if (opts.includeMethod) { labels.method = req.method; } @@ -139,4 +145,5 @@ function main(opts) { main.promClient = promClient; main.normalizePath = normalizePath; +main.normalizeStatusCode = normalizeStatusCode; module.exports = main; diff --git a/src/normalizePath.js b/src/normalizePath.js index 2a9332f..d0efc64 100644 --- a/src/normalizePath.js +++ b/src/normalizePath.js @@ -24,4 +24,3 @@ module.exports = function(req, opts) { } return urlValueParser.replacePathValues(path); }; - diff --git a/src/normalizeStatusCode.js b/src/normalizeStatusCode.js new file mode 100644 index 0000000..85023ed --- /dev/null +++ b/src/normalizeStatusCode.js @@ -0,0 +1,22 @@ +'use strict'; + +module.exports = function(req, opts) { + opts = opts || {}; + + if (opts.formatStatusCode !== undefined && !opts.formatStatusCode) { + return req.status_code; + } + if (typeof opts.formatStatusCode === 'function') { + return opts.formatStatusCode(req, opts); + } + + // Group Status code in 1xx, 2xx, 3xx, 4xx, 5xx or other + const status_code = ({ + '1': '1xx', + '2': '2xx', + '3': '3xx', + '4': '4xx', + '5': '5xx', + })[(req.status_code || '').substr(0,1)] || 'other'; + return status_code; +}; From 48f8b992fdd60baaec8741714b56c01a8ad746bd Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Fri, 24 Mar 2017 02:18:41 -0300 Subject: [PATCH 2/7] Make normalizeStatusCode generic --- src/index.js | 2 +- src/normalizeStatusCode.js | 14 +++----------- 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/index.js b/src/index.js index 7bc2872..296932d 100644 --- a/src/index.js +++ b/src/index.js @@ -118,7 +118,7 @@ function main(opts) { let timer = metrics[httpMtricName].startTimer(labels); onFinished(res, () => { if (opts.normalizeStatusCode) { - labels.status_code = main.normalizeStatusCode(req, opts); + labels.status_code = main.normalizeStatusCode(res, opts); } else { labels.status_code = res.statusCode; } diff --git a/src/normalizeStatusCode.js b/src/normalizeStatusCode.js index 85023ed..166d676 100644 --- a/src/normalizeStatusCode.js +++ b/src/normalizeStatusCode.js @@ -1,22 +1,14 @@ 'use strict'; -module.exports = function(req, opts) { +module.exports = function(res, opts) { opts = opts || {}; if (opts.formatStatusCode !== undefined && !opts.formatStatusCode) { return req.status_code; } if (typeof opts.formatStatusCode === 'function') { - return opts.formatStatusCode(req, opts); + return opts.formatStatusCode(res, opts); } - // Group Status code in 1xx, 2xx, 3xx, 4xx, 5xx or other - const status_code = ({ - '1': '1xx', - '2': '2xx', - '3': '3xx', - '4': '4xx', - '5': '5xx', - })[(req.status_code || '').substr(0,1)] || 'other'; - return status_code; + return req.status_code; }; From c92b85ae96870b217a224a18cb2c793c3d9a0117 Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Fri, 24 Mar 2017 11:08:29 -0300 Subject: [PATCH 3/7] Update prom-client --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c66d7f2..ed219f1 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "MIT", "dependencies": { "on-finished": "^2.3.0", - "prom-client": "^6.3.0", + "prom-client": "^7.2.0", "url-value-parser": "^1.0.0" }, "devDependencies": { From d8c6492163060732d8348cf5c72b41641071fd6b Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Mon, 27 Mar 2017 12:03:53 -0300 Subject: [PATCH 4/7] Implement test --- spec/normalizeStatusCode.spec.js | 23 +++++++++++++++++++++++ src/normalizeStatusCode.js | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 spec/normalizeStatusCode.spec.js diff --git a/spec/normalizeStatusCode.spec.js b/spec/normalizeStatusCode.spec.js new file mode 100644 index 0000000..1721fe8 --- /dev/null +++ b/spec/normalizeStatusCode.spec.js @@ -0,0 +1,23 @@ +'use strict'; +/* eslint-env jasmine */ + +const normalizeStatusCode = require('../src/normalizeStatusCode'); + +describe('normalizeStatusCode', () => { + it('returns original if disabled in opts', () => { + expect( + normalizeStatusCode({status_code: 404}, {normalizeStatusCode: false}) + ).toBe(404); + }); + + it('returns run callback if configured', () => { + expect( + normalizeStatusCode( + {status_code: 500}, + { + formatStatusCode: res => String(res.status_code).slice(0, -2) + 'xx' + } + ) + ).toBe('5xx'); + }); +}); diff --git a/src/normalizeStatusCode.js b/src/normalizeStatusCode.js index 166d676..69d0637 100644 --- a/src/normalizeStatusCode.js +++ b/src/normalizeStatusCode.js @@ -10,5 +10,5 @@ module.exports = function(res, opts) { return opts.formatStatusCode(res, opts); } - return req.status_code; + return res.status_code; }; From 5c6ed64a31a7d956cd83e5555e8ec72135acb223 Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Mon, 27 Mar 2017 17:16:51 -0300 Subject: [PATCH 5/7] Fix lint error --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 296932d..f36f7cc 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,7 @@ const onFinished = require('on-finished'); const promClient = require('prom-client'); const normalizePath = require('./normalizePath'); -const normalizeStatusCode = require('./normalizeStatusCode') +const normalizeStatusCode = require('./normalizeStatusCode'); function matchVsRegExps(element, regexps) { for (let regexp of regexps) { From 52865dfb0206c6b310d713b062eadd931da2ab1f Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Mon, 27 Mar 2017 17:17:59 -0300 Subject: [PATCH 6/7] Back version to prom-client --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ed219f1..c66d7f2 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "license": "MIT", "dependencies": { "on-finished": "^2.3.0", - "prom-client": "^7.2.0", + "prom-client": "^6.3.0", "url-value-parser": "^1.0.0" }, "devDependencies": { From de83ac09a0bcc09558fd825305e7bd5dc3a8898a Mon Sep 17 00:00:00 2001 From: Paulo Duarte Date: Mon, 27 Mar 2017 17:35:13 -0300 Subject: [PATCH 7/7] Remove if unnecessary --- src/normalizeStatusCode.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/normalizeStatusCode.js b/src/normalizeStatusCode.js index 69d0637..6c56354 100644 --- a/src/normalizeStatusCode.js +++ b/src/normalizeStatusCode.js @@ -3,9 +3,6 @@ module.exports = function(res, opts) { opts = opts || {}; - if (opts.formatStatusCode !== undefined && !opts.formatStatusCode) { - return req.status_code; - } if (typeof opts.formatStatusCode === 'function') { return opts.formatStatusCode(res, opts); }