@@ -2,6 +2,7 @@ import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } fro
22import { DEBUG_BUILD } from './debug-build' ;
33import type { CaptureContext } from './scope' ;
44import { closeSession , makeSession , updateSession } from './session' ;
5+ import { startNewTrace } from './tracing/trace' ;
56import type { CheckIn , FinishedCheckIn , MonitorConfig } from './types-hoist/checkin' ;
67import type { Event , EventHint } from './types-hoist/event' ;
78import type { EventProcessor } from './types-hoist/eventprocessor' ;
@@ -15,7 +16,6 @@ import { isThenable } from './utils/is';
1516import { uuid4 } from './utils/misc' ;
1617import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent' ;
1718import { parseEventHintOrCaptureContext } from './utils/prepareEvent' ;
18- import { startNewTrace } from './tracing/trace' ;
1919import { timestampInSeconds } from './utils/time' ;
2020import { GLOBAL_OBJ } from './utils/worldwide' ;
2121
@@ -160,43 +160,13 @@ export function withMonitor<T>(
160160 callback : ( ) => T ,
161161 upsertMonitorConfig ?: MonitorConfig ,
162162) : T {
163- const checkInId = captureCheckIn ( { monitorSlug, status : 'in_progress' } , upsertMonitorConfig ) ;
164- const now = timestampInSeconds ( ) ;
163+ function runCallback ( ) : T {
164+ const checkInId = captureCheckIn ( { monitorSlug, status : 'in_progress' } , upsertMonitorConfig ) ;
165+ const now = timestampInSeconds ( ) ;
165166
166- function finishCheckIn ( status : FinishedCheckIn [ 'status' ] ) : void {
167- captureCheckIn ( { monitorSlug, status, checkInId, duration : timestampInSeconds ( ) - now } ) ;
168- }
169-
170- return withIsolationScope ( ( ) => {
171- // If isolateTrace is enabled, start a new trace for this monitor execution
172- if ( upsertMonitorConfig ?. isolateTrace ) {
173- return startNewTrace ( ( ) => {
174- let maybePromiseResult : T ;
175- try {
176- maybePromiseResult = callback ( ) ;
177- } catch ( e ) {
178- finishCheckIn ( 'error' ) ;
179- throw e ;
180- }
181-
182- if ( isThenable ( maybePromiseResult ) ) {
183- return maybePromiseResult . then (
184- r => {
185- finishCheckIn ( 'ok' ) ;
186- return r ;
187- } ,
188- e => {
189- finishCheckIn ( 'error' ) ;
190- throw e ;
191- } ,
192- ) as T ;
193- }
194- finishCheckIn ( 'ok' ) ;
195-
196- return maybePromiseResult ;
197- } ) ;
167+ function finishCheckIn ( status : FinishedCheckIn [ 'status' ] ) : void {
168+ captureCheckIn ( { monitorSlug, status, checkInId, duration : timestampInSeconds ( ) - now } ) ;
198169 }
199-
200170 // Default behavior without isolateTrace
201171 let maybePromiseResult : T ;
202172 try {
@@ -221,7 +191,9 @@ export function withMonitor<T>(
221191 finishCheckIn ( 'ok' ) ;
222192
223193 return maybePromiseResult ;
224- } ) ;
194+ }
195+
196+ return withIsolationScope ( ( ) => ( upsertMonitorConfig ?. isolateTrace ? startNewTrace ( runCallback ) : runCallback ( ) ) ) ;
225197}
226198
227199/**
0 commit comments