-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add isolateTrace option to Sentry.withMonitor()
#18079
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
2b4f7c1
382fdc6
4dfa0af
e06ae30
a16ae77
8771d6f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { withMonitor } from '@sentry/node'; | ||
|
|
||
| export async function run(): Promise<void> { | ||
| // First withMonitor call without isolateTrace (should share trace) | ||
| await withMonitor('cron-job-1', async () => { | ||
| // Simulate some work | ||
| await new Promise<void>((resolve) => { | ||
| setTimeout(() => { | ||
| resolve(); | ||
| }, 100); | ||
| }); | ||
| }, { | ||
| schedule: { type: 'crontab', value: '* * * * *' } | ||
| }); | ||
|
|
||
| // Second withMonitor call with isolateTrace (should have different trace) | ||
| await withMonitor('cron-job-2', async () => { | ||
| // Simulate some work | ||
| await new Promise<void>((resolve) => { | ||
| setTimeout(() => { | ||
| resolve(); | ||
| }, 100); | ||
| }); | ||
| }, { | ||
| schedule: { type: 'crontab', value: '* * * * *' }, | ||
| isolateTrace: true | ||
| }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import { expect } from 'vitest'; | ||
| import type { Event, TransactionEvent } from '@sentry/types'; | ||
|
|
||
| export async function testResults(events: Event[]): Promise<void> { | ||
| // Get all transaction events (which represent traces) | ||
| const transactionEvents = events.filter((event): event is TransactionEvent => event.type === 'transaction'); | ||
|
|
||
| // Should have at least 2 transaction events (one for each withMonitor call) | ||
| expect(transactionEvents.length).toBeGreaterThanOrEqual(2); | ||
|
|
||
| // Get trace IDs from the transactions | ||
| const traceIds = transactionEvents.map(event => event.contexts?.trace?.trace_id).filter(Boolean); | ||
|
|
||
| // Should have at least 2 different trace IDs (verifying trace isolation) | ||
| const uniqueTraceIds = [...new Set(traceIds)]; | ||
| expect(uniqueTraceIds.length).toBeGreaterThanOrEqual(2); | ||
|
|
||
| console.log('✅ Found traces with different trace IDs:', uniqueTraceIds); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { isThenable } from './utils/is'; | |
| import { uuid4 } from './utils/misc'; | ||
| import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent'; | ||
| import { parseEventHintOrCaptureContext } from './utils/prepareEvent'; | ||
| import { startNewTrace } from './tracing/trace'; | ||
| import { timestampInSeconds } from './utils/time'; | ||
| import { GLOBAL_OBJ } from './utils/worldwide'; | ||
|
|
||
|
|
@@ -167,6 +168,36 @@ export function withMonitor<T>( | |
| } | ||
|
|
||
| return withIsolationScope(() => { | ||
| // If isolateTrace is enabled, start a new trace for this monitor execution | ||
| if (upsertMonitorConfig?.isolateTrace) { | ||
| return startNewTrace(() => { | ||
| let maybePromiseResult: T; | ||
| try { | ||
| maybePromiseResult = callback(); | ||
| } catch (e) { | ||
| finishCheckIn('error'); | ||
| throw e; | ||
| } | ||
|
|
||
| if (isThenable(maybePromiseResult)) { | ||
| return maybePromiseResult.then( | ||
| r => { | ||
| finishCheckIn('ok'); | ||
| return r; | ||
| }, | ||
| e => { | ||
| finishCheckIn('error'); | ||
| throw e; | ||
| }, | ||
| ) as T; | ||
| } | ||
| finishCheckIn('ok'); | ||
|
|
||
| return maybePromiseResult; | ||
| }); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Isolated Trace Creation Failing in Monitored SpanswithMonitor(..., { isolateTrace: true }) does not actually start a new trace when an active span exists. It calls startSpan({ forceTransaction: true }) without resetting the propagation context or explicitly nulling the parent, so startSpan continues the existing traceId if a parent span is present (per createChildOrRootSpan’s parentSpan && forceTransaction branch). This contradicts the intent to create a separate trace per monitor execution. To truly isolate, start a new trace (e.g., via startNewTrace) before creating the monitor span (or set a new propagation context/parentSpan: null with a new traceId). |
||
|
|
||
| // Default behavior without isolateTrace | ||
| let maybePromiseResult: T; | ||
| try { | ||
| maybePromiseResult = callback(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this is not gonna work because it doesn't use our test runner at all. Did you test this?
I suggest taking a look at a test like this one and using the same runner, just that you assert on two transactions and check for distinct trace ids.