Skip to content

Commit 382fdc6

Browse files
committed
feat(core): Implement isolateTrace in withMonitor function
Modify withMonitor to create new traces when isolateTrace option is enabled. When isolateTrace: true, starts a new span with monitor-specific naming to allow distinguishing between different cron job executions.
1 parent 2b4f7c1 commit 382fdc6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

packages/core/src/exports.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { isThenable } from './utils/is';
1515
import { uuid4 } from './utils/misc';
1616
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
1717
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
18+
import { startSpan } from './tracing/trace';
1819
import { timestampInSeconds } from './utils/time';
1920
import { GLOBAL_OBJ } from './utils/worldwide';
2021

@@ -167,6 +168,43 @@ export function withMonitor<T>(
167168
}
168169

169170
return withIsolationScope(() => {
171+
// If isolateTrace is enabled, start a new trace for this monitor execution
172+
if (upsertMonitorConfig?.isolateTrace) {
173+
return startSpan(
174+
{
175+
name: `monitor.${monitorSlug}`,
176+
op: 'monitor',
177+
forceTransaction: true,
178+
},
179+
() => {
180+
let maybePromiseResult: T;
181+
try {
182+
maybePromiseResult = callback();
183+
} catch (e) {
184+
finishCheckIn('error');
185+
throw e;
186+
}
187+
188+
if (isThenable(maybePromiseResult)) {
189+
return maybePromiseResult.then(
190+
r => {
191+
finishCheckIn('ok');
192+
return r;
193+
},
194+
e => {
195+
finishCheckIn('error');
196+
throw e;
197+
},
198+
) as T;
199+
}
200+
finishCheckIn('ok');
201+
202+
return maybePromiseResult;
203+
},
204+
);
205+
}
206+
207+
// Default behavior without isolateTrace
170208
let maybePromiseResult: T;
171209
try {
172210
maybePromiseResult = callback();

0 commit comments

Comments
 (0)