add promClient option for smuggling collectDefaultMetrics, upgrade prom-client to ~10.2.2, version 3.3.0

This commit is contained in:
Konstantin Pogorelov
2018-01-23 16:43:38 +01:00
parent c8996a7730
commit 06f55c9ab8
6 changed files with 47 additions and 9 deletions

View File

@@ -60,6 +60,9 @@ Other options:
* **buckets**: buckets used for `http_request_duration_seconds` histogram
* **autoregister**: if `/metrics` endpoint should be registered. (Default: **true**)
* **promClient**: options for promClient startup, e.g. **collectDefaultMetrics**. This option was added
to keep `express-prom-bundle` runnable using confit (e.g. with kraken.js) without writing any JS code,
see [advanced example](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js)
Deprecated:
@@ -171,19 +174,32 @@ Here is meddleware config sample, which can be used in a standard **kraken.js**
## Changelog
* **3.3.0**
* added option **promClient** to be able to call collectDefaultMetrics
* upgrade **prom-client** to ~10.2.2 (switch to semver "approximately")
* **3.2.0**
* added options **customLabels**, **transformLabels**
* upgrade **prom-client** to 10.1.0
* **3.1.0**
* upgrade **prom-client** to 10.0.0
* **3.0.0**
* upgrade dependencies, most notably **prom-client** to 9.0.0
* switch to koa v2 in koa unittest
* only node v6 or higher is supported (stop supporting node v4 and v5)
* switch to npm5 and use package-lock.json
* options added: includeStatusCode, formatStatusCode
* **2.1.0**
* deprecate **excludeRoutes**, use **req.originalUrl** instead of **req.path**
* **2.0.0**
* the reason for the version lift were:
* compliance to official naming recommendation: https://prometheus.io/docs/practices/naming/

View File

@@ -10,13 +10,18 @@ const bundle = promBundle({
includeMethod: true,
includePath: true,
customLabels: {year: null},
transformLabels: labels => Object.assign(labels, {year: new Date().getFullYear()})
transformLabels: labels => Object.assign(labels, {year: new Date().getFullYear()}),
promClient: {
collectDefaultMetrics: {
timeout: 1000
}
}
});
app.use(bundle);
// native prom-client metric (no prefix)
const c1 = new bundle.promClient.Counter('c1', 'c1 help');
const c1 = new bundle.promClient.Counter({name: 'c1', help: 'c1 help'});
c1.inc(10);
app.get('/foo/:id', (req, res) => {

8
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "express-prom-bundle",
"version": "3.2.0",
"version": "3.3.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -1896,9 +1896,9 @@
"dev": true
},
"prom-client": {
"version": "10.1.0",
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-10.1.0.tgz",
"integrity": "sha512-ny9EdxpG6UcNnuc3W3LedE9qC3RFLSD+aBCv1i9OtcLS/nyHFck5uncoWHpD+rxmdttUPVd8qYRT1rb15Lpmjg==",
"version": "10.2.2",
"resolved": "https://registry.npmjs.org/prom-client/-/prom-client-10.2.2.tgz",
"integrity": "sha512-d3qCBK41qZx00/WVzWOX4tau9FinCztqaECZiGuMI5vGYD//5VSdKMOZPRQKjVh5RkI4Ex98DI0YPsoFnEo1QQ==",
"requires": {
"tdigest": "0.1.1"
}

View File

@@ -1,6 +1,6 @@
{
"name": "express-prom-bundle",
"version": "3.2.1",
"version": "3.3.0",
"description": "express middleware with popular prometheus metrics in one bundle",
"main": "src/index.js",
"keywords": [
@@ -17,7 +17,7 @@
"license": "MIT",
"dependencies": {
"on-finished": "^2.3.0",
"prom-client": "^10.1.0",
"prom-client": "~10.2.2",
"url-value-parser": "^1.0.0"
},
"devDependencies": {

View File

@@ -343,4 +343,16 @@ describe('index', () => {
});
});
});
it('calls promClient.collectDefaultMetrics', () => {
const spy = spyOn(promClient, 'collectDefaultMetrics');
bundle({
promClient: {
collectDefaultMetrics: {
timeout: 3000
}
}
});
expect(spy).toHaveBeenCalledWith({timeout: 3000});
});
});

View File

@@ -44,7 +44,8 @@ function main(opts) {
autoregister: true,
includeStatusCode: true,
normalizePath: main.normalizePath,
formatStatusCode: main.normalizeStatusCode
formatStatusCode: main.normalizeStatusCode,
promClient: {}
},
opts
);
@@ -66,6 +67,10 @@ function main(opts) {
);
}
if (opts.promClient.collectDefaultMetrics) {
promClient.collectDefaultMetrics(opts.promClient.collectDefaultMetrics);
}
const httpMetricName = opts.httpDurationMetricName || 'http_request_duration_seconds';
const metricTemplates = {