Make normalizeStatusCode generic

This commit is contained in:
Paulo Duarte
2017-03-24 02:18:41 -03:00
parent 61e4343a8c
commit 48f8b992fd
2 changed files with 4 additions and 12 deletions

View File

@@ -118,7 +118,7 @@ function main(opts) {
let timer = metrics[httpMtricName].startTimer(labels); let timer = metrics[httpMtricName].startTimer(labels);
onFinished(res, () => { onFinished(res, () => {
if (opts.normalizeStatusCode) { if (opts.normalizeStatusCode) {
labels.status_code = main.normalizeStatusCode(req, opts); labels.status_code = main.normalizeStatusCode(res, opts);
} else { } else {
labels.status_code = res.statusCode; labels.status_code = res.statusCode;
} }

View File

@@ -1,22 +1,14 @@
'use strict'; 'use strict';
module.exports = function(req, opts) { module.exports = function(res, opts) {
opts = opts || {}; opts = opts || {};
if (opts.formatStatusCode !== undefined && !opts.formatStatusCode) { if (opts.formatStatusCode !== undefined && !opts.formatStatusCode) {
return req.status_code; return req.status_code;
} }
if (typeof opts.formatStatusCode === 'function') { 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 return req.status_code;
const status_code = ({
'1': '1xx',
'2': '2xx',
'3': '3xx',
'4': '4xx',
'5': '5xx',
})[(req.status_code || '').substr(0,1)] || 'other';
return status_code;
}; };