From 20eb668e363e3f15863ceb57d7786c55f57715f7 Mon Sep 17 00:00:00 2001 From: Konstantin Pogorelov Date: Wed, 4 Jan 2017 23:41:09 +0100 Subject: [PATCH] export and make replaceable normalizePath(), fix/extend readme, version 2.0.2 --- README.md | 52 +++++++++++++++++++++++++++++++++++++++++++--- package.json | 5 +++-- spec/index.spec.js | 24 +++++++++++++++++++++ src/index.js | 3 ++- 4 files changed, 78 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e6834f2..337d973 100644 --- a/README.md +++ b/README.md @@ -56,13 +56,34 @@ See the example below. The goal is to have separate latency statistics by URL path, e.g. `/my-app/user/`, `/products/by-category` etc. -Just taking `req.path` as a label value won't work as IDs are often part of the URL, like `/user/12352/profile`. So what we actually need is a path template. The module tries to figure out what parts of the path are values or IDs, and what is an actual path. The example mentioned before would be normalized to `/user/#val/profile` and that will become the value for the label. +Just taking `req.path` as a label value won't work as IDs are often part of the URL, +like `/user/12352/profile`. So what we actually need is a path template. +The module tries to figure out what parts of the path are values or IDs, +and what is an actual path. The example mentioned before would be +normalized to `/user/#val/profile` and that will become the value for the label. -You can override this magical behavior and define your own function by providing an optional callback using **normalizePath** option. +You can override this magical behavior and define your own function by +providing an optional callback using **normalizePath** option. +You can also replace the default **normalizePath** function globally. +This is handy if the rest of the middleware is done elsewhere +e.g. via `kraken.js meddleware`. + +```javascript +app.use(promBundle(/* options? */)); + +// let's reuse the existing one and just add some +// functionality on top +const originalNormalize = promBunle.normalizePath; +promBunle.normalizePath = (req, opts) => { + const path = originalNormalize(req, opts); + // count all docs (no matter which file) as a single path + return path.match(/^\/docs/) ? '/docs/*' : path; +}; +``` For more details: * [url-value-parser](https://www.npmjs.com/package/url-value-parser) - magic behind automatic path normalization - * [normalizePath.js](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/src/normalizePath.js) - source code for path processing, for you + * [normalizePath.js](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/src/normalizePath.js) - source code for path processing @@ -113,6 +134,31 @@ app.use(/* your middleware */); app.listen(3000); ``` +## using with kraken.js + +Here is a sample **kraken.js** config file: + +```json +{ + "middleware": { + "prom": { + "priority": 0, + "module": { + "name": "express-prom-bundle", + "arguments": [ + { + "excludeRoutes": ["/", "/robots.txt", "/status"], + "includePath": true, + "includeMethod": true + } + ] + } + } + } +} +``` + + ## Changelog * **2.0.0** diff --git a/package.json b/package.json index 66a08b8..4c9f199 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { "name": "express-prom-bundle", - "version": "2.0.1", + "version": "2.0.2", "description": "express middleware with popular prometheus metrics in one bundle", "main": "src/index.js", "keywords": [ "prometheus", "metrics", "express", - "bundle" + "path", + "method" ], "scripts": { "test": "node_modules/jasme/run.js" diff --git a/spec/index.spec.js b/spec/index.spec.js index 796ce47..4368993 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -169,6 +169,30 @@ describe('index', () => { }); }); + it('normalizePath can be replaced', done => { + const app = express(); + const original = bundle.normalizePath; + bundle.normalizePath = () => 'dummy'; + const instance = bundle({ + includePath: true, + }); + app.use(instance); + app.use('/test', (req, res) => res.send('it worked')); + const agent = supertest(app); + agent + .get('/test') + .end(() => { + agent + .get('/metrics') + .end((err, res) => { + expect(res.status).toBe(200); + expect(res.text).toMatch(/"dummy"/m); + bundle.normalizePath = original; + done(); + }); + }); + }); + it('Koa: metrics returns up=1', done => { const app = koa(); const bundled = bundle({ diff --git a/src/index.js b/src/index.js index aef181d..5326ffa 100644 --- a/src/index.js +++ b/src/index.js @@ -123,7 +123,7 @@ function main(opts) { labels.method = req.method; } if (opts.includePath) { - labels.path = normalizePath(req, opts); + labels.path = main.normalizePath(req, opts); } timer(); }); @@ -140,4 +140,5 @@ function main(opts) { } main.promClient = promClient; +main.normalizePath = normalizePath; module.exports = main;