mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-01-18 16:27:28 +01:00
export and make replaceable normalizePath(), fix/extend readme, version 2.0.2
This commit is contained in:
52
README.md
52
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**
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user