mirror of
https://github.com/BreizhHardware/express-prom-bundle.git
synced 2026-01-18 16:27:28 +01:00
changed wording + added doc + unit test
This commit is contained in:
@@ -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 ###
|
||||
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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
2
types/index.d.ts
vendored
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user