Merge pull request #4 from PauloPaquielli/feature/normalizeStatusCode

This commit is contained in:
The Experimentalist
2017-03-28 17:08:57 +02:00
committed by GitHub
4 changed files with 42 additions and 2 deletions

View File

@@ -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');
});
});

View File

@@ -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(res, 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;

View File

@@ -24,4 +24,3 @@ module.exports = function(req, opts) {
}
return urlValueParser.replacePathValues(path);
};

View File

@@ -0,0 +1,11 @@
'use strict';
module.exports = function(res, opts) {
opts = opts || {};
if (typeof opts.formatStatusCode === 'function') {
return opts.formatStatusCode(res, opts);
}
return res.status_code;
};