changed wording + added doc + unit test

This commit is contained in:
yacine
2020-11-29 15:13:01 +01:00
parent 52fdbf030f
commit bffb4cf16e
4 changed files with 33 additions and 3 deletions

View File

@@ -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 ###

View File

@@ -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();
});

View File

@@ -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();
}

2
types/index.d.ts vendored
View File

@@ -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;