Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(node): Ensure startNewTrace propagates traceId in OTel environments#19963
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
Merged
logaretm
merged 4 commits into
develop
from
awad/js-1994-startnewtrace-does-not-use-same-trace-id-for-spans-inMar 26, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
228bc72
fix(node): Ensure `startNewTrace` propagates traceId in OTel environm…
logaretm 96e2cda
fix(node): Move setPropagationContext inside context.with to prevent …
logaretm 46e1ab9
test(node): Add test verifying startNewTrace respects tracesSampleRate
logaretm ebd5c4c
fix(test): Make scope leak test deterministic
logaretm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,7 +21,7 @@ import { | ||
| } from '@sentry/core'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { getParentSpanId } from '../../../packages/opentelemetry/src/utils/getParentSpanId'; | ||
| import { continueTrace, startInactiveSpan, startSpan, startSpanManual } from '../src/trace'; | ||
| import { continueTrace, startInactiveSpan, startNewTrace, startSpan, startSpanManual } from '../src/trace'; | ||
| import type { AbstractSpan } from '../src/types'; | ||
| import { getActiveSpan } from '../src/utils/getActiveSpan'; | ||
| import { getSamplingDecision } from '../src/utils/getSamplingDecision'; | ||
| @@ -2093,6 +2093,99 @@ describe('span.end() timestamp conversion', () => { | ||
| }); | ||
| }); | ||
| describe('startNewTrace', () => { | ||
| beforeEach(() => { | ||
| mockSdkInit({ tracesSampleRate: 1 }); | ||
| }); | ||
| afterEach(async () => { | ||
| await cleanupOtel(); | ||
| }); | ||
| it('sequential startInactiveSpan calls share the same traceId', () => { | ||
| startNewTrace(() => { | ||
| const propagationContext = getCurrentScope().getPropagationContext(); | ||
| const span1 = startInactiveSpan({ name: 'span-1' }); | ||
| const span2 = startInactiveSpan({ name: 'span-2' }); | ||
| const span3 = startInactiveSpan({ name: 'span-3' }); | ||
| const traceId1 = span1.spanContext().traceId; | ||
| const traceId2 = span2.spanContext().traceId; | ||
| const traceId3 = span3.spanContext().traceId; | ||
| expect(traceId1).toBe(propagationContext.traceId); | ||
| expect(traceId2).toBe(propagationContext.traceId); | ||
| expect(traceId3).toBe(propagationContext.traceId); | ||
| span1.end(); | ||
| span2.end(); | ||
| span3.end(); | ||
| }); | ||
| }); | ||
| it('startSpan inside startNewTrace uses the correct traceId', () => { | ||
| startNewTrace(() => { | ||
| const propagationContext = getCurrentScope().getPropagationContext(); | ||
| startSpan({ name: 'parent-span' }, parentSpan => { | ||
| const parentTraceId = parentSpan.spanContext().traceId; | ||
| expect(parentTraceId).toBe(propagationContext.traceId); | ||
| const child = startInactiveSpan({ name: 'child-span' }); | ||
| expect(child.spanContext().traceId).toBe(propagationContext.traceId); | ||
| child.end(); | ||
| }); | ||
| }); | ||
| }); | ||
| it('generates a different traceId than the outer trace', () => { | ||
| startSpan({ name: 'outer-span' }, outerSpan => { | ||
| const outerTraceId = outerSpan.spanContext().traceId; | ||
| startNewTrace(() => { | ||
| const innerSpan = startInactiveSpan({ name: 'inner-span' }); | ||
| const innerTraceId = innerSpan.spanContext().traceId; | ||
| expect(innerTraceId).not.toBe(outerTraceId); | ||
| const propagationContext = getCurrentScope().getPropagationContext(); | ||
| expect(innerTraceId).toBe(propagationContext.traceId); | ||
| innerSpan.end(); | ||
| }); | ||
| }); | ||
| }); | ||
logaretm marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| it('allows spans to be sampled based on tracesSampleRate', () => { | ||
| startNewTrace(() => { | ||
| const span = startInactiveSpan({ name: 'sampled-span' }); | ||
| // tracesSampleRate is 1 in mockSdkInit, so spans should be sampled | ||
| // This verifies that TraceFlags.NONE on the remote span context does not | ||
| // cause the sampler to inherit a "not sampled" decision from the parent | ||
| expect(spanIsSampled(span)).toBe(true); | ||
| span.end(); | ||
| }); | ||
| }); | ||
| it('does not leak the new traceId to the outer scope', () => { | ||
| const outerScope = getCurrentScope(); | ||
| const outerTraceId = outerScope.getPropagationContext().traceId; | ||
| startNewTrace(() => { | ||
| // Manually set a known traceId on the inner scope to verify it doesn't leak | ||
| getCurrentScope().setPropagationContext({ | ||
| traceId: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', | ||
| sampleRand: 0.5, | ||
| }); | ||
| }); | ||
| const afterTraceId = outerScope.getPropagationContext().traceId; | ||
| expect(afterTraceId).toBe(outerTraceId); | ||
| expect(afterTraceId).not.toBe('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'); | ||
| }); | ||
| }); | ||
| function getSpanName(span: AbstractSpan): string | undefined { | ||
| return spanHasName(span) ? span.name : undefined; | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.