-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathresponseEnd.ts
56 lines (52 loc) · 2.51 KB
/
responseEnd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { ServerResponse } from 'http';
import type { Span } from '@sentry/core';
import { fill, flush, logger, setHttpStatus } from '@sentry/core';
import { DEBUG_BUILD } from '../debug-build';
import type { ResponseEndMethod, WrappedResponseEndMethod } from '../types';
/**
* Wrap `res.end()` so that it ends the span and flushes events before letting the request finish.
*
* Note: This wraps a sync method with an async method. While in general that's not a great idea in terms of keeping
* things in the right order, in this case it's safe, because the native `.end()` actually *is* (effectively) async, and
* its run actually *is* (literally) awaited, just manually so (which reflects the fact that the core of the
* request/response code in Node by far predates the introduction of `async`/`await`). When `.end()` is done, it emits
* the `prefinish` event, and only once that fires does request processing continue. See
* https://github.com/nodejs/node/commit/7c9b607048f13741173d397795bac37707405ba7.
*
* Also note: `res.end()` isn't called until *after* all response data and headers have been sent, so blocking inside of
* `end` doesn't delay data getting to the end user. See
* https://nodejs.org/api/http.html#responseenddata-encoding-callback.
*
* @param span The span tracking the request
* @param res: The request's corresponding response
*/
export function autoEndSpanOnResponseEnd(span: Span, res: ServerResponse): void {
const wrapEndMethod = (origEnd: ResponseEndMethod): WrappedResponseEndMethod => {
return function sentryWrappedEnd(this: ServerResponse, ...args: unknown[]) {
finishSpan(span, this);
return origEnd.call(this, ...args);
};
};
// Prevent double-wrapping
// res.end may be undefined during build when using `next export` to statically export a Next.js app
if (res.end && !(res.end as WrappedResponseEndMethod).__sentry_original__) {
fill(res, 'end', wrapEndMethod);
}
}
/** Finish the given response's span and set HTTP status data */
export function finishSpan(span: Span, res: ServerResponse): void {
setHttpStatus(span, res.statusCode);
span.end();
}
/**
* Flushes pending Sentry events with a 2 second timeout and in a way that cannot create unhandled promise rejections.
*/
export async function flushSafelyWithTimeout(): Promise<void> {
try {
DEBUG_BUILD && logger.log('Flushing events...');
await flush(2000);
DEBUG_BUILD && logger.log('Done flushing events');
} catch (e) {
DEBUG_BUILD && logger.log('Error while flushing events:\n', e);
}
}