@@ -15,6 +15,7 @@ import { getFinalOptions } from './options';
1515import { wrapRequestHandler } from './request' ;
1616import { addCloudResourceContext } from './scope-utils' ;
1717import { init } from './sdk' ;
18+ import { copyExecutionContext } from './utils/copyExecutionContext' ;
1819
1920/**
2021 * Wrapper for Cloudflare handlers.
@@ -38,7 +39,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
3839 if ( 'fetch' in handler && typeof handler . fetch === 'function' && ! isInstrumented ( handler . fetch ) ) {
3940 handler . fetch = new Proxy ( handler . fetch , {
4041 apply ( target , thisArg , args : Parameters < ExportedHandlerFetchHandler < Env , CfHostMetadata > > ) {
41- const [ request , env , context ] = args ;
42+ const [ request , env , ctx ] = args ;
43+ const context = copyExecutionContext ( ctx ) ;
44+ args [ 2 ] = context ;
4245
4346 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
4447
@@ -72,7 +75,10 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
7275 if ( 'scheduled' in handler && typeof handler . scheduled === 'function' && ! isInstrumented ( handler . scheduled ) ) {
7376 handler . scheduled = new Proxy ( handler . scheduled , {
7477 apply ( target , thisArg , args : Parameters < ExportedHandlerScheduledHandler < Env > > ) {
75- const [ event , env , context ] = args ;
78+ const [ event , env , ctx ] = args ;
79+ const context = copyExecutionContext ( ctx ) ;
80+ args [ 2 ] = context ;
81+
7682 return withIsolationScope ( isolationScope => {
7783 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
7884 const waitUntil = context . waitUntil . bind ( context ) ;
@@ -115,7 +121,10 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
115121 if ( 'email' in handler && typeof handler . email === 'function' && ! isInstrumented ( handler . email ) ) {
116122 handler . email = new Proxy ( handler . email , {
117123 apply ( target , thisArg , args : Parameters < EmailExportedHandler < Env > > ) {
118- const [ emailMessage , env , context ] = args ;
124+ const [ emailMessage , env , ctx ] = args ;
125+ const context = copyExecutionContext ( ctx ) ;
126+ args [ 2 ] = context ;
127+
119128 return withIsolationScope ( isolationScope => {
120129 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
121130 const waitUntil = context . waitUntil . bind ( context ) ;
@@ -156,7 +165,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
156165 if ( 'queue' in handler && typeof handler . queue === 'function' && ! isInstrumented ( handler . queue ) ) {
157166 handler . queue = new Proxy ( handler . queue , {
158167 apply ( target , thisArg , args : Parameters < ExportedHandlerQueueHandler < Env , QueueHandlerMessage > > ) {
159- const [ batch , env , context ] = args ;
168+ const [ batch , env , ctx ] = args ;
169+ const context = copyExecutionContext ( ctx ) ;
170+ args [ 2 ] = context ;
160171
161172 return withIsolationScope ( isolationScope => {
162173 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
@@ -206,7 +217,9 @@ export function withSentry<Env = unknown, QueueHandlerMessage = unknown, CfHostM
206217 if ( 'tail' in handler && typeof handler . tail === 'function' && ! isInstrumented ( handler . tail ) ) {
207218 handler . tail = new Proxy ( handler . tail , {
208219 apply ( target , thisArg , args : Parameters < ExportedHandlerTailHandler < Env > > ) {
209- const [ , env , context ] = args ;
220+ const [ , env , ctx ] = args ;
221+ const context = copyExecutionContext ( ctx ) ;
222+ args [ 2 ] = context ;
210223
211224 return withIsolationScope ( async isolationScope => {
212225 const options = getFinalOptions ( optionsCallback ( env ) , env ) ;
0 commit comments