From 84f99cc49c9dc2ca9ebcf333061af3bf6fa129e3 Mon Sep 17 00:00:00 2001 From: Konstantin Pogorelov Date: Wed, 8 Aug 2018 11:23:27 +0200 Subject: [PATCH] add urlValueParser option, update docs accordingly --- README.md | 18 +++++++++++++++++- advanced-example.js | 5 +++++ src/normalizePath.js | 6 ++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5035914..cfca740 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ Which labels to include in `http_request_duration_seconds` metric: Extra transformation callbacks: +* **urlValueParser**: options passed when instantiating [url-value-parser](https://github.com/disjunction/url-value-parser). + This is the easiest way to customize which parts of the URL should be replaced with "#val". + See the [docs](https://github.com/disjunction/url-value-parser) of url-value-parser module for details. * **normalizePath**: `function(req)` generates path values from express `req` (see details below) * **formatStatusCode**: `function(res)` producing final status code from express `res` object, e.g. you can combine `200`, `201` and `204` to just `2xx`. * **transformLabels**: `function(labels, req, res)` transforms the **labels** object, e.g. setting dynamic values to **customLabels** @@ -150,7 +153,9 @@ app.listen(3000); ## using with kraken.js -Here is meddleware config sample, which can be used in a standard **kraken.js** application: +Here is meddleware config sample, which can be used in a standard **kraken.js** application. +In this case the stats for URI paths and HTTP methods are collected separately, +while replacing all HEX values starting from 5 characters and all emails in the path as #val. ```json { @@ -163,11 +168,18 @@ Here is meddleware config sample, which can be used in a standard **kraken.js** "arguments": [ { "includeMethod": true, + "includePath": true, "buckets": [0.1, 1, 5], "promClient": { "collectDefaultMetrics": { "timeout": 2000 } + }, + "urlValueParser": { + "minHexLength": 5, + "extraMasks": [ + "^[^@]+@[^@]+\\.[^@]+$" + ] } } ] @@ -179,6 +191,10 @@ Here is meddleware config sample, which can be used in a standard **kraken.js** ## Changelog +* **4.0.0** + * added option **urlValueParser** + * upgrade **prom-client** to ~11.1.1 + * **3.3.0** * added option **promClient** to be able to call collectDefaultMetrics * upgrade **prom-client** to ~10.2.2 (switch to semver "approximately") diff --git a/advanced-example.js b/advanced-example.js index ae49fb5..9ce4ba9 100644 --- a/advanced-example.js +++ b/advanced-example.js @@ -17,6 +17,10 @@ const bundle = promBundle({ } }, urlValueParser: { + minHexLength: 5, + extraMasks: [ + "^[^@]+@[^@]+\\.[^@]+$" + ] } }); @@ -42,6 +46,7 @@ app.listen(3000, () => console.info( // eslint-disable-line 'listening on 3000\n' + 'test in shell console\n\n' + 'curl localhost:3000/foo/1234\n' + + 'curl localhost:3000/foo/john%40example.com\n' + 'curl -X DELETE localhost:3000/foo/5432\n' + 'curl localhost:3000/bar\n' + 'curl localhost:3000/metrics\n' diff --git a/src/normalizePath.js b/src/normalizePath.js index 677cc8d..d40fd18 100644 --- a/src/normalizePath.js +++ b/src/normalizePath.js @@ -2,16 +2,18 @@ const UrlValueParser = require('url-value-parser'); const url = require('url'); + +// ATTENTION! urlValueParser is a singleton! let urlValueParser; -module.exports = function(req) { +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 || req.url).pathname; if (!urlValueParser) { - urlValueParser = new UrlValueParser(); + urlValueParser = new UrlValueParser(opts && opts.urlValueParser); } return urlValueParser.replacePathValues(path); };