1
- import { applySdkMetadata , getGlobalScope } from '@sentry/core' ;
2
- import { init as initNode } from '@sentry/node' ;
3
- import type { Client , EventProcessor } from '@sentry/types' ;
4
- import { logger } from '@sentry/utils' ;
1
+ import { applySdkMetadata , flush , getGlobalScope } from '@sentry/core' ;
2
+ import {
3
+ type NodeOptions ,
4
+ getDefaultIntegrations as getDefaultNodeIntegrations ,
5
+ httpIntegration ,
6
+ init as initNode ,
7
+ } from '@sentry/node' ;
8
+ import type { Client , EventProcessor , Integration } from '@sentry/types' ;
9
+ import { logger , vercelWaitUntil } from '@sentry/utils' ;
5
10
import { DEBUG_BUILD } from '../common/debug-build' ;
6
11
import type { SentryNuxtServerOptions } from '../common/types' ;
7
12
@@ -14,6 +19,7 @@ export function init(options: SentryNuxtServerOptions): Client | undefined {
14
19
const sentryOptions = {
15
20
...options ,
16
21
registerEsmLoaderHooks : mergeRegisterEsmLoaderHooks ( options ) ,
22
+ defaultIntegrations : getNuxtDefaultIntegrations ( options ) ,
17
23
} ;
18
24
19
25
applySdkMetadata ( sentryOptions , 'nuxt' , [ 'nuxt' , 'node' ] ) ;
@@ -46,6 +52,21 @@ export function init(options: SentryNuxtServerOptions): Client | undefined {
46
52
return client ;
47
53
}
48
54
55
+ function getNuxtDefaultIntegrations ( options : NodeOptions ) : Integration [ ] {
56
+ return [
57
+ ...getDefaultNodeIntegrations ( options ) . filter ( integration => integration . name !== 'Http' ) ,
58
+ // The httpIntegration is added as defaultIntegration, so users can still overwrite it
59
+ httpIntegration ( {
60
+ instrumentation : {
61
+ responseHook : ( ) => {
62
+ // Makes it possible to end the tracing span before closing the Vercel lambda (https://vercel.com/docs/functions/functions-api-reference#waituntil)
63
+ vercelWaitUntil ( flushSafelyWithTimeout ( ) ) ;
64
+ } ,
65
+ } ,
66
+ } ) ,
67
+ ] ;
68
+ }
69
+
49
70
/**
50
71
* Adds /vue/ to the registerEsmLoaderHooks options and merges it with the old values in the array if one is defined.
51
72
* If the registerEsmLoaderHooks option is already a boolean, nothing is changed.
@@ -64,3 +85,16 @@ export function mergeRegisterEsmLoaderHooks(
64
85
}
65
86
return options . registerEsmLoaderHooks ?? { exclude : [ / v u e / ] } ;
66
87
}
88
+
89
+ /**
90
+ * Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
91
+ */
92
+ export async function flushSafelyWithTimeout ( ) : Promise < void > {
93
+ try {
94
+ DEBUG_BUILD && logger . log ( 'Flushing events...' ) ;
95
+ await flush ( 2000 ) ;
96
+ DEBUG_BUILD && logger . log ( 'Done flushing events' ) ;
97
+ } catch ( e ) {
98
+ DEBUG_BUILD && logger . log ( 'Error while flushing events:\n' , e ) ;
99
+ }
100
+ }
0 commit comments