11import {
22 captureException ,
3- flush ,
43 SEMANTIC_ATTRIBUTE_SENTRY_OP ,
54 SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ,
65 SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ,
98} from '@sentry/core' ;
109import { setAsyncLocalStorageAsyncContextStrategy } from './async' ;
1110import type { CloudflareOptions } from './client' ;
11+ import { makeFlushAfterAll } from './flush' ;
1212import { isInstrumented , markAsInstrumented } from './instrument' ;
1313import { getFinalOptions } from './options' ;
1414import { wrapRequestHandler } from './request' ;
@@ -72,6 +72,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
7272 handler . scheduled = new Proxy ( handler . scheduled , {
7373 apply ( target , thisArg , args : Parameters < ExportedHandlerScheduledHandler < Env > > ) {
7474 const [ event , env , context ] = args ;
75+ const flushAfterAll = makeFlushAfterAll ( context ) ;
7576 return withIsolationScope ( isolationScope => {
7677 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
7778
@@ -99,7 +100,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
99100 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
100101 throw e ;
101102 } finally {
102- context . waitUntil ( flush ( 2000 ) ) ;
103+ flushAfterAll ( 2000 ) ;
103104 }
104105 } ,
105106 ) ;
@@ -114,6 +115,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
114115 handler . email = new Proxy ( handler . email , {
115116 apply ( target , thisArg , args : Parameters < EmailExportedHandler < Env > > ) {
116117 const [ emailMessage , env , context ] = args ;
118+ const flushAfterAll = makeFlushAfterAll ( context ) ;
117119 return withIsolationScope ( isolationScope => {
118120 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
119121
@@ -139,7 +141,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
139141 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
140142 throw e ;
141143 } finally {
142- context . waitUntil ( flush ( 2000 ) ) ;
144+ flushAfterAll ( 2000 ) ;
143145 }
144146 } ,
145147 ) ;
@@ -154,6 +156,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
154156 handler . queue = new Proxy ( handler . queue , {
155157 apply ( target , thisArg , args : Parameters < ExportedHandlerQueueHandler < Env , QueueHandlerMessage > > ) {
156158 const [ batch , env , context ] = args ;
159+ const flushAfterAll = makeFlushAfterAll ( context ) ;
157160
158161 return withIsolationScope ( isolationScope => {
159162 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
@@ -185,7 +188,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
185188 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
186189 throw e ;
187190 } finally {
188- context . waitUntil ( flush ( 2000 ) ) ;
191+ flushAfterAll ( 2000 ) ;
189192 }
190193 } ,
191194 ) ;
@@ -200,6 +203,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
200203 handler . tail = new Proxy ( handler . tail , {
201204 apply ( target , thisArg , args : Parameters < ExportedHandlerTailHandler < Env > > ) {
202205 const [ , env , context ] = args ;
206+ const flushAfterAll = makeFlushAfterAll ( context ) ;
203207
204208 return withIsolationScope ( async isolationScope => {
205209 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
@@ -215,7 +219,7 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
215219 captureException ( e , { mechanism : { handled : false , type : 'cloudflare' } } ) ;
216220 throw e ;
217221 } finally {
218- context . waitUntil ( flush ( 2000 ) ) ;
222+ flushAfterAll ( 2000 ) ;
219223 }
220224 } ) ;
221225 } ,
0 commit comments