Combined bypass and bypassOnFinish into one option

This commit is contained in:
Henrik Karlsson
2022-12-14 17:00:11 +01:00
parent ea9f34aa3e
commit 1171fb5be1
4 changed files with 32 additions and 7 deletions

View File

@@ -52,8 +52,6 @@ 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
* **bypass**: function taking express request as an argument and determines whether the given request should be excluded in the metrics, default: **() => false**
* **bypassOnFinish**: function taking express request and response as arguments and determines whether the given request should be excluded in the metrics, default: **() => false**. Prefer using **bypass** if you don't need the response object.
* **httpDurationMetricName**: Allows you change the name of HTTP duration metric, default: **`http_request_duration_seconds`**.
### metricType option ###
@@ -91,6 +89,19 @@ Additional options for **summary**:
see [advanced example](https://github.com/jochen-schweizer/express-prom-bundle/blob/master/advanced-example.js)
* **promRegistry**: Optional `promClient.Registry` instance to attach metrics to. Defaults to global `promClient.register`.
* **metricsApp**: Allows you to attach the metrics endpoint to a different express app. You probably want to use it in combination with `autoregister: false`.
* **bypass**: An object that takes onRequest and onFinish callbacks that determines whether the given request should be excluded in the metrics. Default:
```js
{
onRequest: (req) => false,
onFinish: (req, res) => false
}
```
`onRequest` is run directly in the middleware chain, before the request is processed. `onFinish` is run after the request has been processed, and has access to the express response object in addition to the request object. Both callbacks are optional, and if one or both returns true the request is excluded.
As a shorthand, just the onRequest callback can be used instead of the object.
### More details on includePath option

View File

@@ -243,7 +243,9 @@ describe('index', () => {
it('bypass requests, checking res', done => {
const app = express();
const instance = bundle({
bypassOnFinish: (req, res) => res.statusCode === 404,
bypass: {
onFinish: (req, res) => res.statusCode === 404
}
});
app.use(instance);
app.use('/200', (req, res) => res.send(''));

View File

@@ -161,6 +161,14 @@ function main(opts) {
const metricsMatch = opts.metricsPath instanceof RegExp ? opts.metricsPath
: new RegExp('^' + (opts.metricsPath || '/metrics') + '/?$');
if (typeof opts.bypass === 'function') {
opts.bypass = {
onRequest: opts.bypass
};
} else if (!opts.bypass) {
opts.bypass = {};
}
const middleware = function (req, res, next) {
const path = req.originalUrl || req.url; // originalUrl gets lost in koa-connect?
@@ -170,7 +178,7 @@ function main(opts) {
// bypass() is checked only after /metrics was processed
// if you wish to disable /metrics use autoregister:false instead
if (opts.bypass && opts.bypass(req)) {
if (opts.bypass.onRequest && opts.bypass.onRequest(req)) {
return next();
}
@@ -182,7 +190,7 @@ function main(opts) {
const timer = metrics[httpMetricName].startTimer(labels);
onFinished(res, () => {
if (opts.bypassOnFinish && opts.bypassOnFinish(req, res)) {
if (opts.bypass.onFinish && opts.bypass.onFinish(req, res)) {
return;
}

8
types/index.d.ts vendored
View File

@@ -27,8 +27,12 @@ declare namespace express_prom_bundle {
includePath?: boolean;
includeUp?: boolean;
bypass?: (req: Request) => boolean;
bypassOnFinish?: (req: Request, res: Response) => boolean;
bypass?:
| ((req: Request) => boolean)
| {
onRequest?: (req: Request) => boolean;
onFinish?: (req: Request, res: Response) => boolean;
};
excludeRoutes?: Array<string | RegExp>;