Skip to content
Merged
Changes from 1 commit
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
30 changes: 27 additions & 3 deletions packages/nuxt/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applySdkMetadata, getGlobalScope } from '@sentry/core';
import { init as initNode } from '@sentry/node';
import { applySdkMetadata, flush, getGlobalScope } from '@sentry/core';
import { httpIntegration, init as initNode } from '@sentry/node';
import type { Client, EventProcessor } from '@sentry/types';
import { logger } from '@sentry/utils';
import { logger, vercelWaitUntil } from '@sentry/utils';
import { DEBUG_BUILD } from '../common/debug-build';
import type { SentryNuxtServerOptions } from '../common/types';

Expand All @@ -14,6 +14,17 @@ export function init(options: SentryNuxtServerOptions): Client | undefined {
const sentryOptions = {
...options,
registerEsmLoaderHooks: mergeRegisterEsmLoaderHooks(options),
integrations: [
httpIntegration({
instrumentation: {
responseHook: () => {
Copy link
Member

Choose a reason for hiding this comment

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

m: Can we add functionality to add this to our own SentryHttpInstrumentation instead? First, this will mean we do not rely on the otel http instrumentation for this. It also means that users may adde their own responseHook and it will not lead to problems.

// Makes it possible to end the tracing span before closing the Vercel lambda (https://vercel.com/docs/functions/functions-api-reference#waituntil)
Copy link
Member

Choose a reason for hiding this comment

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

q: How about netlify and others? I wonder if we also want to flush in those cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I know Netlify does not expose a function like waitUtil 🤔 But it might make a difference if the SDK just flushes in the response hook - I would have to try.

Copy link
Member

Choose a reason for hiding this comment

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

Right, maybe we can try flushing here in case of serverless.

vercelWaitUntil(flushSafelyWithTimeout());
},
},
}),
...(Array.isArray(options.integrations) ? options.integrations : []),
],
};

applySdkMetadata(sentryOptions, 'nuxt', ['nuxt', 'node']);
Expand Down Expand Up @@ -64,3 +75,16 @@ export function mergeRegisterEsmLoaderHooks(
}
return options.registerEsmLoaderHooks ?? { exclude: [/vue/] };
}

/**
* Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
*/
export async function flushSafelyWithTimeout(): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

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

l: A bunch of SDKs reimplement this. Maybe we can think of hoisting it into utils? No strong feelings though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea! But I'll create another PR for this change.

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);
}
}
Loading