Skip to content

Commit

Permalink
Merge pull request #14062 from getsentry/sig/astro-waitUntil
Browse files Browse the repository at this point in the history
fix(astro): `waitUntil` flush at end of request
  • Loading branch information
s1gr1d authored Oct 31, 2024
2 parents 989ee77 + 53638f7 commit 9444c9c
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/astro/src/server/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
captureException,
continueTrace,
flush,
getActiveSpan,
getClient,
getCurrentScope,
Expand All @@ -14,8 +15,10 @@ import {
import type { Scope, SpanAttributes } from '@sentry/types';
import {
addNonEnumerableProperty,
logger,
objectify,
stripUrlQueryAndFragment,
vercelWaitUntil,
winterCGRequestToRequestData,
} from '@sentry/utils';
import type { APIContext, MiddlewareResponseHandler } from 'astro';
Expand Down Expand Up @@ -188,6 +191,17 @@ async function instrumentRequest(
} catch (e) {
sendErrorToSentry(e);
throw e;
} finally {
vercelWaitUntil(
(async () => {
// Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
try {
await flush(2000);
} catch (e) {
logger.log('Error while flushing events:\n', e);
}
})(),
);
}
// TODO: flush if serverless (first extract function)
},
Expand Down

0 comments on commit 9444c9c

Please sign in to comment.