express-prom-bundle
Advanced tools
Comparing version 4.3.0 to 5.0.0
{ | ||
"name": "express-prom-bundle", | ||
"version": "4.3.0", | ||
"version": "5.0.0", | ||
"description": "express middleware with popular prometheus metrics in one bundle", | ||
@@ -21,3 +21,2 @@ "main": "src/index.js", | ||
"on-finished": "^2.3.0", | ||
"prom-client": "~11.2.1", | ||
"url-value-parser": "^2.0.0" | ||
@@ -27,11 +26,14 @@ }, | ||
"coveralls": "^3.0.2", | ||
"eslint": "^5.3.0", | ||
"express": "^4.16.3", | ||
"eslint": "^5.11.0", | ||
"express": "^4.16.4", | ||
"istanbul": "^0.4.5", | ||
"jasme": "^5.2.0", | ||
"koa": "^2.5.2", | ||
"koa-connect": "^2.0.0", | ||
"supertest": "^3.0.0", | ||
"jasme": "^6.0.0", | ||
"koa": "^2.6.2", | ||
"koa-connect": "^2.0.1", | ||
"supertest": "^3.3.0", | ||
"supertest-koa-agent": "^0.3.0" | ||
}, | ||
"peerDependencies": { | ||
"prom-client": "^11.1.2" | ||
}, | ||
"repository": { | ||
@@ -38,0 +40,0 @@ "type": "git", |
@@ -7,3 +7,3 @@ [![build status](https://travis-ci.org/jochen-schweizer/express-prom-bundle.png)](https://travis-ci.org/jochen-schweizer/express-prom-bundle) [![Coverage Status](https://coveralls.io/repos/github/jochen-schweizer/express-prom-bundle/badge.svg?branch=master)](https://coveralls.io/github/jochen-schweizer/express-prom-bundle?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://www.tldrlegal.com/l/mit) [![NPM version](https://badge.fury.io/js/express-prom-bundle.png)](http://badge.fury.io/js/express-prom-bundle) | ||
Internally it uses **prom-client**. See: https://github.com/siimon/prom-client | ||
Since version 5 it uses **prom-client** as a peer dependency. See: https://github.com/siimon/prom-client | ||
@@ -18,3 +18,3 @@ Included metrics: | ||
``` | ||
npm install express-prom-bundle | ||
npm install prom-client express-prom-bundle | ||
``` | ||
@@ -54,2 +54,4 @@ | ||
Most useful together with **transformLabels** callback, otherwise it's better to use native Prometheus relabeling. | ||
* **includeUp**: include an auxiliary "up"-metric which always returns 1, default: **true** | ||
* **metricsPath**: replace the `/metrics` route with a **regex** or exact **string**. Note: it is highly recommended to just stick to the default | ||
@@ -56,0 +58,0 @@ Extra transformation callbacks: |
191
src/index.js
@@ -1,2 +0,1 @@ | ||
'use strict'; | ||
const onFinished = require('on-finished'); | ||
@@ -8,3 +7,3 @@ const promClient = require('prom-client'); | ||
function matchVsRegExps(element, regexps) { | ||
for (let regexp of regexps) { | ||
for (const regexp of regexps) { | ||
if (regexp instanceof RegExp) { | ||
@@ -21,27 +20,6 @@ if (element.match(regexp)) { | ||
function filterArrayByRegExps(array, regexps) { | ||
return array.filter(element => { | ||
return matchVsRegExps(element, regexps); | ||
}); | ||
} | ||
function prepareMetricNames(opts, metricTemplates) { | ||
const names = Object.keys(metricTemplates); | ||
if (opts.whitelist) { | ||
if (opts.blacklist) { | ||
throw new Error('you cannot have whitelist and blacklist at the same time'); | ||
} | ||
return filterArrayByRegExps(names, opts.whitelist); | ||
} | ||
if (opts.blacklist) { | ||
const blacklisted = filterArrayByRegExps(names, opts.blacklist); | ||
return names.filter(name => blacklisted.indexOf(name) === -1); | ||
} | ||
return names; | ||
} | ||
function clusterMetrics() { | ||
const aggregatorRegistry = new promClient.AggregatorRegistry(); | ||
const metricsMiddleware = function(req, res, next) { | ||
const metricsMiddleware = function(req, res) { | ||
aggregatorRegistry.clusterMetrics((err, clusterMetrics) => { | ||
@@ -61,2 +39,11 @@ if (err) { | ||
function main(opts) { | ||
if (arguments[2] && arguments[1] && arguments[1].send) { | ||
arguments[1].status(500) | ||
.send('<h1>500 Error</h1>\n' | ||
+ '<p>Unexpected 3rd param in express-prom-bundle.\n' | ||
+ '<p>Did you just put express-prom-bundle into app.use ' | ||
+ 'without calling it as a function first?'); | ||
return; | ||
} | ||
opts = Object.assign( | ||
@@ -70,20 +57,15 @@ { | ||
promClient: {} | ||
}, | ||
opts | ||
}, opts | ||
); | ||
if (arguments[2] && arguments[1] && arguments[1].send) { | ||
arguments[1].status(500) | ||
.send('<h1>500 Error</h1>\n' | ||
+ '<p>Unexpected 3rd param in express-prom-bundle.\n' | ||
+ '<p>Did you just put express-prom-bundle into app.use ' | ||
+ 'without calling it as a function first?'); | ||
return; | ||
} | ||
if (opts.prefix || opts.keepDefaultMetrics !== undefined) { | ||
if (opts.prefix | ||
|| opts.keepDefaultMetrics !== undefined | ||
|| opts.whitelist !== undefined | ||
|| opts.blacklist !== undefined | ||
) { | ||
throw new Error( | ||
'express-prom-bundle detected obsolete options:' | ||
+ 'prefix and/or keepDefaultMetrics. ' | ||
'express-prom-bundle detected one of the obsolete options: ' | ||
+ 'prefix, keepDefaultMetrics, whitelist, blacklist. ' | ||
+ 'Please refer to oficial docs. ' | ||
+ 'Most likely you upgraded the module without necessary code changes' | ||
+ 'Most likely you upgraded the module without the necessary code changes' | ||
); | ||
@@ -98,47 +80,42 @@ } | ||
const metricTemplates = { | ||
'up': () => new promClient.Gauge({ | ||
name: 'up', | ||
help: '1 = up, 0 = not up' | ||
}), | ||
[httpMetricName]: () => { | ||
const labels = ['status_code']; | ||
if (opts.includeMethod) { | ||
labels.push('method'); | ||
} | ||
if (opts.includePath) { | ||
labels.push('path'); | ||
} | ||
if (opts.customLabels){ | ||
labels.push.apply(labels, Object.keys(opts.customLabels)); | ||
} | ||
function makeHttpMetric() { | ||
const labels = ['status_code']; | ||
if (opts.includeMethod) { | ||
labels.push('method'); | ||
} | ||
if (opts.includePath) { | ||
labels.push('path'); | ||
} | ||
if (opts.customLabels) { | ||
labels.push.apply(labels, Object.keys(opts.customLabels)); | ||
} | ||
if (opts.metricType === 'summary') { | ||
return new promClient.Summary({ | ||
name: httpMetricName, | ||
help: 'duration summary of http responses labeled with: ' + labels.join(', '), | ||
labelNames: labels, | ||
percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999] | ||
}); | ||
} else if (opts.metricType === 'histogram' || !opts.metricType) { | ||
return new promClient.Histogram({ | ||
name: httpMetricName, | ||
help: 'duration histogram of http responses labeled with: ' + labels.join(', '), | ||
labelNames: labels, | ||
buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10] | ||
}); | ||
} else { | ||
throw new Error('metricType option must be histogram or summary'); | ||
} | ||
if (opts.metricType === 'summary') { | ||
return new promClient.Summary({ | ||
name: httpMetricName, | ||
help: 'duration summary of http responses labeled with: ' + labels.join(', '), | ||
labelNames: labels, | ||
percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999] | ||
}); | ||
} else if (opts.metricType === 'histogram' || !opts.metricType) { | ||
return new promClient.Histogram({ | ||
name: httpMetricName, | ||
help: 'duration histogram of http responses labeled with: ' + labels.join(', '), | ||
labelNames: labels, | ||
buckets: opts.buckets || [0.003, 0.03, 0.1, 0.3, 1.5, 10] | ||
}); | ||
} else { | ||
throw new Error('metricType option must be histogram or summary'); | ||
} | ||
} | ||
const metrics = { | ||
[httpMetricName]: makeHttpMetric() | ||
}; | ||
const metrics = {}; | ||
const names = prepareMetricNames(opts, metricTemplates); | ||
for (let name of names) { | ||
metrics[name] = metricTemplates[name](); | ||
} | ||
if (metrics.up) { | ||
if (opts.includeUp !== false) { | ||
metrics.up = new promClient.Gauge({ | ||
name: 'up', | ||
help: '1 = up, 0 = not up' | ||
}); | ||
metrics.up.set(1); | ||
@@ -152,7 +129,9 @@ } | ||
const metricsMatch = opts.metricsPath instanceof RegExp ? opts.metricsPath | ||
: new RegExp('^' + (opts.metricsPath || '/metrics') + '/?$'); | ||
const middleware = function (req, res, next) { | ||
const path = req.originalUrl || req.url; // originalUrl gets lost in koa-connect? | ||
let labels; | ||
if (opts.autoregister && path.match(/^\/metrics\/?$/)) { | ||
if (opts.autoregister && path.match(metricsMatch)) { | ||
return metricsMiddleware(req, res); | ||
@@ -165,31 +144,29 @@ } | ||
if (metrics[httpMetricName]) { | ||
labels = {}; | ||
let timer = metrics[httpMetricName].startTimer(labels); | ||
onFinished(res, () => { | ||
if (opts.includeStatusCode) { | ||
labels.status_code = opts.formatStatusCode(res, opts); | ||
} | ||
if (opts.includeMethod) { | ||
labels.method = req.method; | ||
} | ||
if (opts.includePath) { | ||
labels.path = typeof opts.normalizePath == 'function' | ||
? opts.normalizePath(req, opts) | ||
: main.normalizePath(req, opts); | ||
} | ||
if (opts.customLabels) { | ||
Object.assign(labels, opts.customLabels); | ||
} | ||
if (opts.transformLabels) { | ||
opts.transformLabels(labels, req, res); | ||
} | ||
timer(); | ||
}); | ||
} | ||
const labels = {}; | ||
const timer = metrics[httpMetricName].startTimer(labels); | ||
onFinished(res, () => { | ||
if (opts.includeStatusCode) { | ||
labels.status_code = opts.formatStatusCode(res, opts); | ||
} | ||
if (opts.includeMethod) { | ||
labels.method = req.method; | ||
} | ||
if (opts.includePath) { | ||
labels.path = opts.normalizePath instanceof Function | ||
? opts.normalizePath(req, opts) | ||
: main.normalizePath(req, opts); | ||
} | ||
if (opts.customLabels) { | ||
Object.assign(labels, opts.customLabels); | ||
} | ||
if (opts.transformLabels) { | ||
opts.transformLabels(labels, req, res); | ||
} | ||
timer(); | ||
}); | ||
next(); | ||
}; | ||
middleware.metricTemplates = metricTemplates; | ||
middleware.metrics = metrics; | ||
@@ -201,3 +178,5 @@ middleware.promClient = promClient; | ||
// this is kept only for compatibility with the code relying on older version | ||
main.promClient = promClient; | ||
main.normalizePath = normalizePath; | ||
@@ -204,0 +183,0 @@ main.normalizeStatusCode = normalizeStatusCode; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8
257
18408
184
+ Addedprom-client@11.5.3(transitive)
- Removedprom-client@~11.2.1
- Removedprom-client@11.2.1(transitive)