From ab208ca8e93a12b16caca4a680557c379d83eddc Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 24 Oct 2024 11:17:26 +0200 Subject: [PATCH 1/3] add flush function --- packages/astro/debug-build.ts | 8 ++++++++ packages/astro/src/server/middleware.ts | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 packages/astro/debug-build.ts diff --git a/packages/astro/debug-build.ts b/packages/astro/debug-build.ts new file mode 100644 index 000000000000..60aa50940582 --- /dev/null +++ b/packages/astro/debug-build.ts @@ -0,0 +1,8 @@ +declare const __DEBUG_BUILD__: boolean; + +/** + * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code. + * + * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. + */ +export const DEBUG_BUILD = __DEBUG_BUILD__; diff --git a/packages/astro/src/server/middleware.ts b/packages/astro/src/server/middleware.ts index 95d099ff0526..770162c61621 100644 --- a/packages/astro/src/server/middleware.ts +++ b/packages/astro/src/server/middleware.ts @@ -3,6 +3,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, captureException, continueTrace, + flush, getActiveSpan, getClient, getCurrentScope, @@ -14,11 +15,14 @@ import { import type { Scope, SpanAttributes } from '@sentry/types'; import { addNonEnumerableProperty, + logger, objectify, stripUrlQueryAndFragment, + vercelWaitUntil, winterCGRequestToRequestData, } from '@sentry/utils'; import type { APIContext, MiddlewareResponseHandler } from 'astro'; +import { DEBUG_BUILD } from '../../debug-build'; type MiddlewareOptions = { /** @@ -188,6 +192,8 @@ async function instrumentRequest( } catch (e) { sendErrorToSentry(e); throw e; + } finally { + vercelWaitUntil(flushSafelyWithTimeout()); } // TODO: flush if serverless (first extract function) }, @@ -213,6 +219,19 @@ function addMetaTagToHead(htmlChunk: string): string { return htmlChunk.replace('', content); } +/** + * Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections. + */ +export async function flushSafelyWithTimeout(): Promise { + 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); + } +} + /** * Interpolates the route from the URL and the passed params. * Best we can do to get a route name instead of a raw URL. From 005a1c1eed763ef02ccd271ba47e2248bdcea8d4 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 24 Oct 2024 15:27:34 +0200 Subject: [PATCH 2/3] inline flush --- packages/astro/src/server/middleware.ts | 26 ++++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/astro/src/server/middleware.ts b/packages/astro/src/server/middleware.ts index 770162c61621..dedb0199f149 100644 --- a/packages/astro/src/server/middleware.ts +++ b/packages/astro/src/server/middleware.ts @@ -193,7 +193,18 @@ async function instrumentRequest( sendErrorToSentry(e); throw e; } finally { - vercelWaitUntil(flushSafelyWithTimeout()); + vercelWaitUntil( + (async () => { + // Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections. + 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); + } + })(), + ); } // TODO: flush if serverless (first extract function) }, @@ -219,19 +230,6 @@ function addMetaTagToHead(htmlChunk: string): string { return htmlChunk.replace('', content); } -/** - * Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections. - */ -export async function flushSafelyWithTimeout(): Promise { - 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); - } -} - /** * Interpolates the route from the URL and the passed params. * Best we can do to get a route name instead of a raw URL. From d5c85cfeca36099bf7a6013b11b3ee1dc49a1e1d Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 31 Oct 2024 14:43:09 +0100 Subject: [PATCH 3/3] delete debug build --- packages/astro/debug-build.ts | 8 -------- packages/astro/src/server/middleware.ts | 5 +---- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 packages/astro/debug-build.ts diff --git a/packages/astro/debug-build.ts b/packages/astro/debug-build.ts deleted file mode 100644 index 60aa50940582..000000000000 --- a/packages/astro/debug-build.ts +++ /dev/null @@ -1,8 +0,0 @@ -declare const __DEBUG_BUILD__: boolean; - -/** - * This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code. - * - * ATTENTION: This constant must never cross package boundaries (i.e. be exported) to guarantee that it can be used for tree shaking. - */ -export const DEBUG_BUILD = __DEBUG_BUILD__; diff --git a/packages/astro/src/server/middleware.ts b/packages/astro/src/server/middleware.ts index dedb0199f149..50c8e973adfe 100644 --- a/packages/astro/src/server/middleware.ts +++ b/packages/astro/src/server/middleware.ts @@ -22,7 +22,6 @@ import { winterCGRequestToRequestData, } from '@sentry/utils'; import type { APIContext, MiddlewareResponseHandler } from 'astro'; -import { DEBUG_BUILD } from '../../debug-build'; type MiddlewareOptions = { /** @@ -197,11 +196,9 @@ async function instrumentRequest( (async () => { // Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections. 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); + logger.log('Error while flushing events:\n', e); } })(), );