From bffb4cf16ef40f4a886eaff1b691d9efbe2f6c22 Mon Sep 17 00:00:00 2001 From: yacine Date: Sun, 29 Nov 2020 15:13:01 +0100 Subject: [PATCH] changed wording + added doc + unit test --- README.md | 2 +- spec/index.spec.js | 30 ++++++++++++++++++++++++++++++ src/index.js | 2 +- types/index.d.ts | 2 +- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5b857ca..91058c1 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Which labels to include in `http_request_duration_seconds` metric: * **includeUp**: include an auxiliary "up"-metric which always returns 1, default: **true** * **metricsPath**: replace the `/metrics` route with a **regex** or exact **string**. Note: it is highly recommended to just stick to the default * **metricType**: histogram/summary selection. See more details below -* **filter**: whether the incoming request should be included in the metrics +* **bypass**: Function which takes express request as an argument. Determines whether the given request should be included in the metrics or not, default: **() => false** ### metricType option ### diff --git a/spec/index.spec.js b/spec/index.spec.js index 09b16af..5f8773c 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -200,6 +200,36 @@ describe('index', () => { }); }); + it('bypass requests', done => { + const app = express(); + const instance = bundle({ + bypass: (req)=> { + return ['/test', /bad.word/].includes(req.url) + } + }); + app.use(instance); + app.use('/test', (req, res) => res.send('it worked')); + app.use('/some/bad-word', (req, res) => res.send('it worked too')); + const agent = supertest(app); + agent + .get('/test') + .end(() => { + agent + .get('/some/bad-word') + .end(() => { + const metricHashMap = instance.metrics.http_request_duration_seconds.hashMap; + expect(metricHashMap['status_code:200']).not.toBeDefined(); + + agent + .get('/metrics') + .end((err, res) => { + expect(res.status).toBe(200); + done(); + }); + }); + }); + }); + it('complains about deprecated options', () => { expect(() => bundle({prefix: 'hello'})).toThrow(); }); diff --git a/src/index.js b/src/index.js index 900f901..5611cc7 100644 --- a/src/index.js +++ b/src/index.js @@ -131,7 +131,7 @@ function main(opts) { : new RegExp('^' + (opts.metricsPath || '/metrics') + '/?$'); const middleware = function (req, res, next) { - if (opts.filter && opts.filter(req)) { + if (opts.bypass && opts.bypass(req)) { return next(); } diff --git a/types/index.d.ts b/types/index.d.ts index 57469fc..cbf3457 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -28,7 +28,7 @@ declare namespace express_prom_bundle { includePath?: boolean; includeUp?: boolean; - filter?: (req: Request) => boolean; + bypass?: (req: Request) => boolean; metricType?: 'summary' | 'histogram'; metricsPath?: string;