11import {
22 captureException ,
3- flush ,
43 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
54 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
65 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
76 startSpan ,
87 withIsolationScope ,
98} from '@sentry/core' ;
9+ import { flushIfServerless } from '@sentry/core/src' ;
1010import { setAsyncLocalStorageAsyncContextStrategy } from './async' ;
1111import type { CloudflareOptions } from './client' ;
1212import { isInstrumented , markAsInstrumented } from './instrument' ;
@@ -74,7 +74,6 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
7474 const [ event , env , context ] = args ;
7575 return withIsolationScope ( isolationScope => {
7676 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
77- const waitUntil = context . waitUntil . bind ( context ) ;
7877
7978 const client = init ( { ...options , ctx : context } ) ;
8079 isolationScope . setClient ( client ) ;
@@ -100,7 +99,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
10099 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
101100 throw e ;
102101 } finally {
103- waitUntil ( flush ( 2000 ) ) ;
102+ await flushIfServerless ( { cloudflareCtx : context } ) ;
104103 }
105104 } ,
106105 ) ;
@@ -117,7 +116,6 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
117116 const [ emailMessage , env , context ] = args ;
118117 return withIsolationScope ( isolationScope => {
119118 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
120- const waitUntil = context . waitUntil . bind ( context ) ;
121119
122120 const client = init ( { ...options , ctx : context } ) ;
123121 isolationScope . setClient ( client ) ;
@@ -141,7 +139,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
141139 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
142140 throw e ;
143141 } finally {
144- waitUntil ( flush ( 2000 ) ) ;
142+ await flushIfServerless ( { cloudflareCtx : context } ) ;
145143 }
146144 } ,
147145 ) ;
@@ -159,7 +157,6 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
159157
160158 return withIsolationScope ( isolationScope => {
161159 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
162- const waitUntil = context . waitUntil . bind ( context ) ;
163160
164161 const client = init ( { ...options , ctx : context } ) ;
165162 isolationScope . setClient ( client ) ;
@@ -191,7 +188,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
191188 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
192189 throw e ;
193190 } finally {
194- waitUntil ( flush ( 2000 ) ) ;
191+ await flushIfServerless ( { cloudflareCtx : context } ) ;
195192 }
196193 } ,
197194 ) ;
@@ -210,8 +207,6 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
210207 return withIsolationScope ( async isolationScope => {
211208 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
212209
213- const waitUntil = context . waitUntil . bind ( context ) ;
214-
215210 const client = init ( { ...options , ctx : context } ) ;
216211 isolationScope . setClient ( client ) ;
217212
@@ -223,7 +218,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
223218 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
224219 throw e ;
225220 } finally {
226- waitUntil ( flush ( 2000 ) ) ;
221+ await flushIfServerless ( { cloudflareCtx : context } ) ;
227222 }
228223 } ) ;
229224 } ,
0 commit comments