Skip to content

Commit ed97658

Browse files
committed
revert vercel-specific functionality
1 parent 5360260 commit ed97658

File tree

9 files changed

+38
-18
lines changed

9 files changed

+38
-18
lines changed

packages/nextjs/src/common/captureRequestError.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { RequestEventData } from '@sentry/core';
2-
import { captureException, flushIfServerless, headersToDict, withScope } from '@sentry/core';
2+
import { captureException, headersToDict, vercelWaitUntil, withScope } from '@sentry/core';
3+
import { flushSafelyWithTimeout } from './utils/responseEnd';
34

45
type RequestInfo = {
56
path: string;
@@ -40,6 +41,6 @@ export function captureRequestError(error: unknown, request: RequestInfo, errorC
4041
},
4142
});
4243

43-
flushIfServerless().catch(() => /* no-op */ {});
44+
vercelWaitUntil(flushSafelyWithTimeout());
4445
});
4546
}

packages/nextjs/src/common/pages-router-instrumentation/_error.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { captureException, flushIfServerless, httpRequestToRequestData, withScope } from '@sentry/core';
1+
import { captureException, httpRequestToRequestData, vercelWaitUntil, withScope } from '@sentry/core';
22
import type { NextPageContext } from 'next';
3+
import { flushSafelyWithTimeout } from '../utils/responseEnd';
34

45
type ContextOrProps = {
56
req?: NextPageContext['req'];
@@ -53,5 +54,5 @@ export async function captureUnderscoreErrorException(contextOrProps: ContextOrP
5354
});
5455
});
5556

56-
flushIfServerless().catch(() => /* no-op */ {});
57+
vercelWaitUntil(flushSafelyWithTimeout());
5758
}

packages/nextjs/src/common/pages-router-instrumentation/wrapApiHandlerWithSentry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
captureException,
33
continueTrace,
44
debug,
5-
flushIfServerless,
65
getActiveSpan,
76
httpRequestToRequestData,
87
isString,
@@ -11,6 +10,7 @@ import {
1110
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1211
setHttpStatus,
1312
startSpanManual,
13+
vercelWaitUntil,
1414
withIsolationScope,
1515
} from '@sentry/core';
1616
import type { NextApiRequest } from 'next';
@@ -95,7 +95,7 @@ export function wrapApiHandlerWithSentry(apiHandler: NextApiHandler, parameteriz
9595
apply(target, thisArg, argArray) {
9696
setHttpStatus(span, res.statusCode);
9797
span.end();
98-
flushIfServerless().catch(() => /* no-op */ {});
98+
vercelWaitUntil(flushSafelyWithTimeout());
9999
return target.apply(thisArg, argArray);
100100
},
101101
});

packages/nextjs/src/common/withServerActionInstrumentation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ import {
33
captureException,
44
continueTrace,
55
debug,
6-
flushIfServerless,
76
getActiveSpan,
87
getClient,
98
getIsolationScope,
109
handleCallbackErrors,
1110
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1211
SPAN_STATUS_ERROR,
1312
startSpan,
13+
vercelWaitUntil,
1414
withIsolationScope,
1515
} from '@sentry/core';
16+
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1617
import { DEBUG_BUILD } from './debug-build';
1718
import { isNotFoundNavigationError, isRedirectNavigationError } from './nextNavigationErrorUtils';
1819

@@ -151,7 +152,7 @@ async function withServerActionInstrumentationImplementation<A extends (...args:
151152
},
152153
);
153154
} finally {
154-
flushIfServerless().catch(() => /* no-op */ {});
155+
vercelWaitUntil(flushSafelyWithTimeout());
155156
}
156157
},
157158
);

packages/nextjs/src/common/wrapMiddlewareWithSentry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { TransactionSource } from '@sentry/core';
22
import {
33
captureException,
4-
flushIfServerless,
54
getActiveSpan,
65
getCurrentScope,
76
getRootSpan,
@@ -10,9 +9,11 @@ import {
109
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1110
setCapturedScopesOnSpan,
1211
startSpan,
12+
vercelWaitUntil,
1313
winterCGRequestToRequestData,
1414
withIsolationScope,
1515
} from '@sentry/core';
16+
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1617
import type { EdgeRouteHandler } from '../edge/types';
1718

1819
/**
@@ -107,7 +108,7 @@ export function wrapMiddlewareWithSentry<H extends EdgeRouteHandler>(
107108
});
108109
},
109110
() => {
110-
flushIfServerless().catch(() => /* no-op */ {});
111+
vercelWaitUntil(flushSafelyWithTimeout());
111112
},
112113
);
113114
},

packages/nextjs/src/common/wrapServerComponentWithSentry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { RequestEventData } from '@sentry/core';
22
import {
33
captureException,
4-
flushIfServerless,
54
getActiveSpan,
65
getCapturedScopesOnSpan,
76
getClient,
@@ -16,12 +15,14 @@ import {
1615
SPAN_STATUS_OK,
1716
spanToJSON,
1817
startSpanManual,
18+
vercelWaitUntil,
1919
winterCGHeadersToDict,
2020
withIsolationScope,
2121
withScope,
2222
} from '@sentry/core';
2323
import { isNotFoundNavigationError, isRedirectNavigationError } from '../common/nextNavigationErrorUtils';
2424
import type { ServerComponentContext } from '../common/types';
25+
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
2526
import { TRANSACTION_ATTR_SENTRY_TRACE_BACKFILL } from './span-attributes-with-logic-attached';
2627
import { commonObjectToIsolationScope, commonObjectToPropagationContext } from './utils/tracingUtils';
2728
import { getSanitizedRequestUrl } from './utils/urls';
@@ -136,7 +137,7 @@ export function wrapServerComponentWithSentry<F extends (...args: any[]) => any>
136137
},
137138
() => {
138139
span.end();
139-
flushIfServerless().catch(() => /* no-op */ {});
140+
vercelWaitUntil(flushSafelyWithTimeout());
140141
},
141142
);
142143
},

packages/nextjs/src/edge/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
applySdkMetadata,
3-
flushIfServerless,
43
getGlobalScope,
54
getRootSpan,
65
GLOBAL_OBJ,
@@ -10,10 +9,12 @@ import {
109
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1110
spanToJSON,
1211
stripUrlQueryAndFragment,
12+
vercelWaitUntil,
1313
} from '@sentry/core';
1414
import type { VercelEdgeOptions } from '@sentry/vercel-edge';
1515
import { getDefaultIntegrations, init as vercelEdgeInit } from '@sentry/vercel-edge';
1616
import { isBuild } from '../common/utils/isBuild';
17+
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1718
import { distDirRewriteFramesIntegration } from './distDirRewriteFramesIntegration';
1819

1920
export * from '@sentry/vercel-edge';
@@ -89,7 +90,7 @@ export function init(options: VercelEdgeOptions = {}): void {
8990

9091
client?.on('spanEnd', span => {
9192
if (span === getRootSpan(span)) {
92-
flushIfServerless().catch(() => /* no-op */ {});
93+
vercelWaitUntil(flushSafelyWithTimeout());
9394
}
9495
});
9596

packages/nextjs/src/edge/wrapApiHandlerWithSentry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
captureException,
3-
flushIfServerless,
43
getActiveSpan,
54
getCurrentScope,
65
getRootSpan,
@@ -10,9 +9,11 @@ import {
109
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
1110
setCapturedScopesOnSpan,
1211
startSpan,
12+
vercelWaitUntil,
1313
winterCGRequestToRequestData,
1414
withIsolationScope,
1515
} from '@sentry/core';
16+
import { flushSafelyWithTimeout } from '../common/utils/responseEnd';
1617
import type { EdgeRouteHandler } from './types';
1718

1819
/**
@@ -87,7 +88,7 @@ export function wrapApiHandlerWithSentry<H extends EdgeRouteHandler>(
8788
});
8889
},
8990
() => {
90-
flushIfServerless().catch(() => /* no-op */ {});
91+
vercelWaitUntil(flushSafelyWithTimeout());
9192
},
9293
);
9394
},

packages/nuxt/src/server/sdk.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as path from 'node:path';
22
import type { Client, EventProcessor, Integration } from '@sentry/core';
3-
import { applySdkMetadata, debug, flushIfServerless, getGlobalScope } from '@sentry/core';
3+
import { applySdkMetadata, debug, flush, getGlobalScope, vercelWaitUntil } from '@sentry/core';
44
import {
55
type NodeOptions,
66
getDefaultIntegrations as getDefaultNodeIntegrations,
@@ -84,9 +84,22 @@ function getNuxtDefaultIntegrations(options: NodeOptions): Integration[] {
8484
instrumentation: {
8585
responseHook: () => {
8686
// Makes it possible to end the tracing span before closing the Vercel lambda (https://vercel.com/docs/functions/functions-api-reference#waituntil)
87-
flushIfServerless().catch(() => /* no-op */ {});
87+
vercelWaitUntil(flushSafelyWithTimeout());
8888
},
8989
},
9090
}),
9191
];
9292
}
93+
94+
/**
95+
* Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
96+
*/
97+
async function flushSafelyWithTimeout(): Promise<void> {
98+
try {
99+
DEBUG_BUILD && debug.log('Flushing events...');
100+
await flush(2000);
101+
DEBUG_BUILD && debug.log('Done flushing events');
102+
} catch (e) {
103+
DEBUG_BUILD && debug.log('Error while flushing events:\n', e);
104+
}
105+
}

0 commit comments

Comments
 (0)