Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/core/src/utils/flushIfServerless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export async function flushIfServerless(
return;
}

// Note: vercelWaitUntil only does something in Vercel Edge runtime
// In Node runtime, we use process.on('SIGTERM') instead
// @ts-expect-error This is not typed
if (GLOBAL_OBJ[Symbol.for('@vercel/request-context')]) {
// Vercel has a waitUntil equivalent that works without execution context
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/utils/vercelWaitUntil.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GLOBAL_OBJ } from './worldwide';

declare const EdgeRuntime: string | undefined;

interface VercelRequestContextGlobal {
get?():
| {
Expand All @@ -14,6 +16,11 @@ interface VercelRequestContextGlobal {
* Vendored from https://www.npmjs.com/package/@vercel/functions
*/
export function vercelWaitUntil(task: Promise<unknown>): void {
// We only flush manually in Vercel Edge runtime
// In Node runtime, we use process.on('SIGTERM') instead
if (typeof EdgeRuntime !== 'string') {
return;
}
const vercelRequestContextGlobal: VercelRequestContextGlobal | undefined =
// @ts-expect-error This is not typed
GLOBAL_OBJ[Symbol.for('@vercel/request-context')];
Expand Down
9 changes: 9 additions & 0 deletions packages/node-core/src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ function _init(
enhanceDscWithOpenTelemetryRootSpanName(client);
setupEventContextTrace(client);

// Ensure we flush events when vercel functions are ended
// See: https://vercel.com/docs/functions/functions-api-reference#sigterm-signal
if (process.env.VERCEL) {
process.on('SIGTERM', async () => {
// We have 500ms for processing here, so we try to make sure to have enough time to send the events
await client.flush(200);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this on a vercel function? Generally makes sense but we could bump this more up I'd say?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did not test this so far at all, maybe you can take this PR over when you have some time and properly test it. The reason I went with 200 here is because this is just the timeout used for finishing events, we still need time to actually make the http request so there needs to be some wiggle room -to the max. 500ms - if we spend 500ms on processing events, we have no more time to actually send the events 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah that makes sense I thought this time is included already, I'll take it over

});
}

return client;
}

Expand Down
Loading