From 5b1517ca91274c7001ca3d5914896cb1610bbedb Mon Sep 17 00:00:00 2001 From: Konstantin Pogorelov Date: Wed, 31 May 2017 15:27:08 +0200 Subject: [PATCH] #7 upgrade dependencies, workaround for koa-connect, update docs --- README.md | 13 ++++++++----- advanced-example.js | 5 ----- package.json | 16 ++++++++-------- spec/index.spec.js | 11 ++++------- src/index.js | 2 +- src/normalizePath.js | 2 +- 6 files changed, 22 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 368a857..6667e51 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # express prometheus bundle -Express middleware with popular prometheus metrics in one bundle. It's also compatible with koa v1 (see below). +Express middleware with popular prometheus metrics in one bundle. It's also compatible with koa v1 and v2 (see below). Internally it uses **prom-client**. See: https://github.com/siimon/prom-client @@ -116,15 +116,15 @@ app.listen(3000); See an [advanced example on github](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js) -## koa v1 example +## koa v2 example ```javascript const promBundle = require("express-prom-bundle"); -const koa = require("koa"); +const Koa = require("koa"); const c2k = require("koa-connect"); const metricsMiddleware = promBundle({/* options */ }); -const app = koa(); +const app = new Koa(); app.use(c2k(metricsMiddleware)); app.use(/* your middleware */); @@ -157,8 +157,11 @@ Here is meddleware config sample, which can be used in a standard **kraken.js** ## Changelog + * **3.0.0** + * upgrade dependencies, most notably **prom-client** to 9.0.0 + * switch to koa v2 in koa unittest * **2.1.0** - * deprecate **excludeRoutes**, use **req.originalPath** instead of **req.path** + * 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/ diff --git a/advanced-example.js b/advanced-example.js index 1516802..5725465 100644 --- a/advanced-example.js +++ b/advanced-example.js @@ -4,11 +4,6 @@ const express = require('express'); const app = express(); const promBundle = require('express-prom-bundle'); -// here we want to remove default metrics provided in prom-client -// this must be done before initializing promBundle -clearInterval(promBundle.promClient.defaultMetrics()); -promBundle.promClient.register.clear(); - const bundle = promBundle({ blacklist: [/up/], buckets: [0.1, 0.4, 0.7], diff --git a/package.json b/package.json index bc7532b..f8ff073 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "express-prom-bundle", - "version": "2.2.0", + "version": "3.0.0", "description": "express middleware with popular prometheus metrics in one bundle", "main": "src/index.js", "keywords": [ @@ -17,18 +17,18 @@ "license": "MIT", "dependencies": { "on-finished": "^2.3.0", - "prom-client": "^6.3.0", + "prom-client": "^9.0.0", "url-value-parser": "^1.0.0" }, "devDependencies": { - "coveralls": "^2.11.15", - "eslint": "^3.11.1", - "express": "^4.14.0", + "coveralls": "^2.13.1", + "eslint": "^3.19.0", + "express": "^4.15.3", "istanbul": "^0.4.5", "jasme": "^5.2.0", - "koa": "^1.2.4", - "koa-connect": "^1.0.0", - "supertest": "^2.0.1", + "koa": "^2.2.0", + "koa-connect": "^2.0.0", + "supertest": "^3.0.0", "supertest-koa-agent": "^0.3.0" }, "repository": { diff --git a/spec/index.spec.js b/spec/index.spec.js index 4368993..21801d7 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -4,7 +4,7 @@ const express = require('express'); const supertest = require('supertest'); const bundle = require('../'); -const koa = require('koa'); +const Koa = require('koa'); const c2k = require('koa-connect'); const supertestKoa = require('supertest-koa-agent'); const promClient = require('prom-client'); @@ -194,17 +194,14 @@ describe('index', () => { }); it('Koa: metrics returns up=1', done => { - const app = koa(); + const app = new Koa(); const bundled = bundle({ whitelist: ['up'] }); app.use(c2k(bundled)); - app.use(function*(next) { - if (this.path !== 'test') { - return yield next; - } - this.body = 'it worked'; + app.use(function(ctx, next) { + return next().then(() => ctx.body = 'it worked'); }); const agent = supertestKoa(app); diff --git a/src/index.js b/src/index.js index f36f7cc..00cd828 100644 --- a/src/index.js +++ b/src/index.js @@ -102,7 +102,7 @@ function main(opts) { }; const middleware = function (req, res, next) { - const path = req.originalUrl; + const path = req.originalUrl || req.url; // originalUrl gets lost in koa-connect? let labels; if (opts.autoregister && path.match(/^\/metrics\/?$/)) { diff --git a/src/normalizePath.js b/src/normalizePath.js index d0efc64..e8ea3cd 100644 --- a/src/normalizePath.js +++ b/src/normalizePath.js @@ -10,7 +10,7 @@ module.exports = function(req, opts) { // originalUrl is taken, because url and path can be changed // by middlewares such as 'router'. Note: this function is called onFinish /// i.e. always in the tail of the middleware chain - const path = url.parse(req.originalUrl).pathname; + const path = url.parse(req.originalUrl || req.url).pathname; if (opts.normalizePath !== undefined && !opts.normalizePath) { return path;