-
-
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
Open
naaa760
wants to merge
6
commits into
getsentry:develop
Choose a base branch
from
naaa760:feat/monitor-trace
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−0
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b4f7c1
feat(core): Add isolateTrace option to MonitorConfig
naaa760 382fdc6
feat(core): Implement isolateTrace in withMonitor function
naaa760 4dfa0af
test(core): Add tests for isolateTrace functionality
naaa760 e06ae30
feat: Update isolateTrace implementation to use startNewTrace
naaa760 a16ae77
test: Add integration test for isolateTrace functionality
naaa760 8771d6f
Merge branch 'develop' into feat/monitor-trace
naaa760 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
dev-packages/node-integration-tests/suites/public-api/withMonitor/scenario.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }); | ||
| } |
19 changes: 19 additions & 0 deletions
19
dev-packages/node-integration-tests/suites/public-api/withMonitor/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Bug: Isolated Trace Creation Failing in Monitored Spans
withMonitor(..., { 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).