-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(replays): Record replay trace_ids with span streaming
#21714
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
19 changes: 19 additions & 0 deletions
19
packages/replay-internal/src/coreHandlers/handleAfterSegmentSpanEnd.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 type { Span } from '@sentry/core'; | ||
| import { spanIsSampled } from '@sentry/core'; | ||
| import type { ReplayContainer } from '../types'; | ||
| import { addTraceIdToContext } from './util/addTraceIdToContext'; | ||
|
|
||
| type AfterSegmentSpanEndCallback = (segmentSpan: Span) => void; | ||
|
|
||
| export function handleAfterSegmentSpanEnd(replay: ReplayContainer): AfterSegmentSpanEndCallback { | ||
| return (segmentSpan: Span) => { | ||
| if (!replay.isEnabled() || !spanIsSampled(segmentSpan)) { | ||
| return; | ||
| } | ||
|
|
||
| const traceId = segmentSpan.spanContext().traceId; | ||
| if (traceId) { | ||
| addTraceIdToContext(replay, traceId); | ||
| } | ||
| }; | ||
| } | ||
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
10 changes: 10 additions & 0 deletions
10
packages/replay-internal/src/coreHandlers/util/addTraceIdToContext.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,10 @@ | ||
| import type { ReplayContainer } from '../../types'; | ||
|
|
||
| const MAX_TRACE_IDS = 100; | ||
|
|
||
| export function addTraceIdToContext(replay: ReplayContainer, traceId: string): void { | ||
| const replayContext = replay.getContext(); | ||
| if (replayContext.traceIds.size < MAX_TRACE_IDS) { | ||
| replayContext.traceIds.add(traceId); | ||
| } | ||
| } |
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
114 changes: 114 additions & 0 deletions
114
packages/replay-internal/test/integration/coreHandlers/handleAfterSegmentSpanEnd.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,114 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| import type { Span } from '@sentry/core'; | ||
| import { getClient } from '@sentry/core'; | ||
| import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; | ||
| import type { ReplayContainer } from '../../../src/replay'; | ||
| import { resetSdkMock } from '../../mocks/resetSdkMock'; | ||
|
|
||
| let replay: ReplayContainer; | ||
|
|
||
| describe('Integration | coreHandlers | handleAfterSegmentSpanEnd', () => { | ||
| beforeAll(() => { | ||
| vi.useFakeTimers(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| replay.stop(); | ||
| }); | ||
|
|
||
| it('records traceIds from afterSegmentSpanEnd', async () => { | ||
| ({ replay } = await resetSdkMock({ | ||
| replayOptions: { | ||
| stickySession: false, | ||
| }, | ||
| sentryOptions: { | ||
| replaysSessionSampleRate: 1.0, | ||
| replaysOnErrorSampleRate: 0.0, | ||
| }, | ||
| })); | ||
|
|
||
| const client = getClient()!; | ||
|
|
||
| client.emit('afterSegmentSpanEnd', { | ||
| spanContext: () => ({ traceId: 'trace-stream-1', spanId: 'span1', traceFlags: 1 }), | ||
| } as unknown as Span); | ||
|
|
||
| client.emit('afterSegmentSpanEnd', { | ||
| spanContext: () => ({ traceId: 'trace-stream-2', spanId: 'span2', traceFlags: 1 }), | ||
| } as unknown as Span); | ||
|
|
||
| expect(Array.from(replay.getContext().traceIds)).toEqual(['trace-stream-1', 'trace-stream-2']); | ||
| }); | ||
|
|
||
| it('limits traceIds from afterSegmentSpanEnd to max. 100', async () => { | ||
| ({ replay } = await resetSdkMock({ | ||
| replayOptions: { | ||
| stickySession: false, | ||
| }, | ||
| sentryOptions: { | ||
| replaysSessionSampleRate: 1.0, | ||
| replaysOnErrorSampleRate: 0.0, | ||
| }, | ||
| })); | ||
|
|
||
| const client = getClient()!; | ||
|
|
||
| for (let i = 0; i < 150; i++) { | ||
| client.emit('afterSegmentSpanEnd', { | ||
| spanContext: () => ({ traceId: `tr-${i}`, spanId: `sp-${i}`, traceFlags: 1 }), | ||
| } as unknown as Span); | ||
| } | ||
|
|
||
| expect(replay.getContext().traceIds.size).toBe(100); | ||
| expect(Array.from(replay.getContext().traceIds)).toEqual( | ||
| Array(100) | ||
| .fill(undefined) | ||
| .map((_, i) => `tr-${i}`), | ||
| ); | ||
| }); | ||
|
|
||
| it('does not record traceIds from afterSegmentSpanEnd when replay is disabled', async () => { | ||
| ({ replay } = await resetSdkMock({ | ||
| replayOptions: { | ||
| stickySession: false, | ||
| }, | ||
| sentryOptions: { | ||
| replaysSessionSampleRate: 1.0, | ||
| replaysOnErrorSampleRate: 0.0, | ||
| }, | ||
| })); | ||
|
|
||
| const client = getClient()!; | ||
|
|
||
| replay['_isEnabled'] = false; | ||
|
|
||
| client.emit('afterSegmentSpanEnd', { | ||
| spanContext: () => ({ traceId: 'trace-stream-1', spanId: 'span1', traceFlags: 1 }), | ||
| } as unknown as Span); | ||
|
|
||
| expect(Array.from(replay.getContext().traceIds)).toEqual([]); | ||
| }); | ||
|
|
||
| it('does not record traceIds for unsampled spans', async () => { | ||
| ({ replay } = await resetSdkMock({ | ||
| replayOptions: { | ||
| stickySession: false, | ||
| }, | ||
| sentryOptions: { | ||
| replaysSessionSampleRate: 1.0, | ||
| replaysOnErrorSampleRate: 0.0, | ||
| }, | ||
| })); | ||
|
|
||
| const client = getClient()!; | ||
|
|
||
| client.emit('afterSegmentSpanEnd', { | ||
| spanContext: () => ({ traceId: 'trace-unsampled', spanId: 'span1', traceFlags: 0 }), | ||
| } as unknown as Span); | ||
|
|
||
| expect(Array.from(replay.getContext().traceIds)).toEqual([]); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.