mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-01-19 00:37:36 +01:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b1aa494cb | ||
|
|
65549a769b | ||
|
|
de83ac09a0 | ||
|
|
52865dfb02 | ||
|
|
5c6ed64a31 | ||
|
|
d8c6492163 | ||
|
|
c92b85ae96 | ||
|
|
48f8b992fd | ||
|
|
61e4343a8c |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "express-prom-bundle",
|
||||
"version": "2.1.0",
|
||||
"version": "2.2.0",
|
||||
"description": "express middleware with popular prometheus metrics in one bundle",
|
||||
"main": "src/index.js",
|
||||
"keywords": [
|
||||
|
||||
23
spec/normalizeStatusCode.spec.js
Normal file
23
spec/normalizeStatusCode.spec.js
Normal 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');
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
@@ -24,4 +24,3 @@ module.exports = function(req, opts) {
|
||||
}
|
||||
return urlValueParser.replacePathValues(path);
|
||||
};
|
||||
|
||||
|
||||
11
src/normalizeStatusCode.js
Normal file
11
src/normalizeStatusCode.js
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user