Skip to content

Commit 9444c9c

Browse files
authored
Merge pull request #14062 from getsentry/sig/astro-waitUntil
fix(astro): `waitUntil` flush at end of request
2 parents 989ee77 + 53638f7 commit 9444c9c

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

packages/astro/src/server/middleware.ts

+14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
44
captureException,
55
continueTrace,
6+
flush,
67
getActiveSpan,
78
getClient,
89
getCurrentScope,
@@ -14,8 +15,10 @@ import {
1415
import type { Scope, SpanAttributes } from '@sentry/types';
1516
import {
1617
addNonEnumerableProperty,
18+
logger,
1719
objectify,
1820
stripUrlQueryAndFragment,
21+
vercelWaitUntil,
1922
winterCGRequestToRequestData,
2023
} from '@sentry/utils';
2124
import type { APIContext, MiddlewareResponseHandler } from 'astro';
@@ -188,6 +191,17 @@ async function instrumentRequest(
188191
} catch (e) {
189192
sendErrorToSentry(e);
190193
throw e;
194+
} finally {
195+
vercelWaitUntil(
196+
(async () => {
197+
// Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
198+
try {
199+
await flush(2000);
200+
} catch (e) {
201+
logger.log('Error while flushing events:\n', e);
202+
}
203+
})(),
204+
);
191205
}
192206
// TODO: flush if serverless (first extract function)
193207
},

0 commit comments

Comments
 (0)