Skip to content

Commit e1b9900

Browse files
sheerunHazAT
authored andcommitted
Add auto-flushing to middleware integration (#2017)
* Add auto-flushing to middleware integration * wait -> flushTimeout
1 parent 93200d1 commit e1b9900

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

packages/node/src/handlers.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { captureException, getCurrentHub } from '@sentry/core';
22
import { Event } from '@sentry/types';
33
import { forget, isString, logger, normalize } from '@sentry/utils';
4+
import { flush } from './sdk';
45
import * as cookie from 'cookie';
56
import * as domain from 'domain';
67
import * as http from 'http';
@@ -221,12 +222,21 @@ export function requestHandler(options?: {
221222
transaction?: boolean | TransactionTypes;
222223
user?: boolean | string[];
223224
version?: boolean;
225+
flushTimeout?: number;
224226
}): (req: http.IncomingMessage, res: http.ServerResponse, next: (error?: any) => void) => void {
225227
return function sentryRequestMiddleware(
226228
req: http.IncomingMessage,
227229
res: http.ServerResponse,
228230
next: (error?: any) => void,
229231
): void {
232+
if (options && options.flushTimeout && options.flushTimeout > 0) {
233+
const _end = res.end
234+
235+
res.end = async function end (chunk?: any, encodingOrCb?: string | Function, cb?: Function) {
236+
await flush(options.flushTimeout)
237+
return _end.call(this, chunk, encodingOrCb, cb)
238+
}
239+
}
230240
const local = domain.create();
231241
local.add(req);
232242
local.add(res);

0 commit comments

Comments
 (0)