@@ -144,26 +144,28 @@ export async function executeDOAlarm(alarmFn: NonNullable<AlarmFn>, id: DurableO
144144 return promise
145145}
146146
147- function instrumentFetchFn ( fetchFn : FetchFn , initialiser : Initialiser , env : Env , id : DurableObjectId ) : FetchFn {
147+ function instrumentFetchFn ( fetchFn : FetchFn , initialiser : Initialiser , env : Env , state : DurableObjectState ) : FetchFn {
148148 const fetchHandler : ProxyHandler < FetchFn > = {
149149 async apply ( target , thisArg , argArray : Parameters < FetchFn > ) {
150150 const request = argArray [ 0 ]
151151 const config = initialiser ( env , request )
152152 const context = setConfig ( config )
153153 try {
154154 const bound = target . bind ( unwrap ( thisArg ) )
155- return await api_context . with ( context , executeDOFetch , undefined , bound , request , id )
155+ return await api_context . with ( context , executeDOFetch , undefined , bound , request , state . id )
156156 } finally {
157- exportSpans ( ) . catch ( ( error : unknown ) => {
158- console . error ( 'Error exporting Durable Object fetch spans:' , error )
159- } )
157+ state . waitUntil (
158+ exportSpans ( ) . catch ( ( error : unknown ) => {
159+ console . error ( 'Error exporting Durable Object fetch spans:' , error )
160+ } )
161+ )
160162 }
161163 } ,
162164 }
163165 return wrap ( fetchFn , fetchHandler )
164166}
165167
166- function instrumentAlarmFn ( alarmFn : AlarmFn , initialiser : Initialiser , env : Env , id : DurableObjectId ) {
168+ function instrumentAlarmFn ( alarmFn : AlarmFn , initialiser : Initialiser , env : Env , state : DurableObjectState ) {
167169 if ( ! alarmFn ) {
168170 return undefined
169171 }
@@ -174,11 +176,13 @@ function instrumentAlarmFn(alarmFn: AlarmFn, initialiser: Initialiser, env: Env,
174176 const context = setConfig ( config )
175177 try {
176178 const bound = target . bind ( unwrap ( thisArg ) )
177- await api_context . with ( context , executeDOAlarm , undefined , bound , id )
179+ await api_context . with ( context , executeDOAlarm , undefined , bound , state . id )
178180 } finally {
179- exportSpans ( ) . catch ( ( error : unknown ) => {
180- console . error ( 'Error exporting Durable Object alarm spans:' , error )
181- } )
181+ state . waitUntil (
182+ exportSpans ( ) . catch ( ( error : unknown ) => {
183+ console . error ( 'Error exporting Durable Object alarm spans:' , error )
184+ } )
185+ )
182186 }
183187 } ,
184188 }
@@ -190,10 +194,10 @@ function instrumentDurableObject(doObj: DurableObject, initialiser: Initialiser,
190194 get ( target , prop ) {
191195 if ( prop === 'fetch' ) {
192196 const fetchFn = Reflect . get ( target , prop )
193- return instrumentFetchFn ( fetchFn , initialiser , env , state . id )
197+ return instrumentFetchFn ( fetchFn , initialiser , env , state )
194198 } else if ( prop === 'alarm' ) {
195199 const alarmFn = Reflect . get ( target , prop )
196- return instrumentAlarmFn ( alarmFn , initialiser , env , state . id )
200+ return instrumentAlarmFn ( alarmFn , initialiser , env , state )
197201 } else {
198202 const result = Reflect . get ( target , prop )
199203 if ( typeof result === 'function' ) {
0 commit comments