-
Notifications
You must be signed in to change notification settings - Fork 176
✨ Add tab.id to all RUM and Logs events #4184
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
10 commits
Select commit
Hold shift + click to select a range
57b220e
✨ Add tab context module to core
mormubis 3182869
✨ Add browser_tab_id to RUM and Logs event schemas
mormubis 4f20844
✨ Wire tab context into RUM and Logs SDKs
mormubis 46f539c
👌 Address PR review feedback for tab context
mormubis 09456b9
👌 Remove explicit browser_tab_id from event type definitions
mormubis e802811
👌 Reset rum-events-format submodule and type files to main
mormubis 720dcc8
👌 Address PR review feedback: rename to tab.id, cache fallback ID, te…
mormubis ffe398f
👌 Export TAB_ID_STORAGE_KEY and make retrieveOrCreateTabId mockable f…
mormubis 2a504cf
👌 Remove mockable, cache tab ID unconditionally, add resetCachedTabId…
mormubis 23146a1
🐛 Reset cachedTabId in beforeEach to prevent cross-file test contamin…
mormubis 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
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,107 @@ | ||
| import type { Hooks } from '../../../test' | ||
| import { createHooks, registerCleanupTask } from '../../../test' | ||
| import { HookNames } from '../../tools/abstractHooks' | ||
| import type { RelativeTime } from '../../tools/utils/timeUtils' | ||
| import { TAB_ID_STORAGE_KEY, resetCachedTabId, startTabContext } from './tabContext' | ||
|
|
||
| const UUID_PATTERN = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/ | ||
|
|
||
| describe('tabContext', () => { | ||
| let hooks: Hooks | ||
|
|
||
| beforeEach(() => { | ||
| resetCachedTabId() | ||
| hooks = createHooks() | ||
| registerCleanupTask(() => { | ||
| sessionStorage.removeItem(TAB_ID_STORAGE_KEY) | ||
| resetCachedTabId() | ||
| }) | ||
| }) | ||
|
|
||
| it('should return a tab ID via the assemble hook', () => { | ||
| startTabContext(hooks) | ||
|
|
||
| const event = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(event).toEqual( | ||
| jasmine.objectContaining({ | ||
| tab: jasmine.objectContaining({ | ||
| id: jasmine.stringMatching(UUID_PATTERN), | ||
| }), | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('should return a consistent tab ID across multiple hook triggers', () => { | ||
| startTabContext(hooks) | ||
|
|
||
| const event1 = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
| const event2 = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(event1.tab.id).toBe(event2.tab.id) | ||
| }) | ||
|
|
||
| it('should persist the tab ID to sessionStorage', () => { | ||
| startTabContext(hooks) | ||
|
|
||
| const event = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(sessionStorage.getItem(TAB_ID_STORAGE_KEY)).toBe(event.tab.id) | ||
| }) | ||
|
|
||
| it('should reuse an existing tab ID from sessionStorage', () => { | ||
| sessionStorage.setItem(TAB_ID_STORAGE_KEY, 'existing-tab-id') | ||
| startTabContext(hooks) | ||
|
|
||
| const event = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(event.tab.id).toBe('existing-tab-id') | ||
| }) | ||
|
|
||
| it('should generate a tab ID when sessionStorage.getItem throws', () => { | ||
| spyOn(sessionStorage, 'getItem').and.throwError('SecurityError') | ||
| startTabContext(hooks) | ||
|
|
||
| const event = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(event.tab.id).toMatch(UUID_PATTERN) | ||
| }) | ||
|
|
||
| it('should generate a tab ID when sessionStorage.setItem throws', () => { | ||
| spyOn(sessionStorage, 'getItem').and.returnValue(null) | ||
| spyOn(sessionStorage, 'setItem').and.throwError('QuotaExceededError') | ||
| startTabContext(hooks) | ||
|
|
||
| const event = hooks.triggerHook(HookNames.Assemble, { | ||
| startTime: 0 as RelativeTime, | ||
| }) | ||
|
|
||
| expect(event.tab.id).toMatch(UUID_PATTERN) | ||
| }) | ||
|
|
||
| it('should return the same tab ID across multiple startTabContext calls when sessionStorage is unavailable', () => { | ||
| spyOn(sessionStorage, 'getItem').and.throwError('SecurityError') | ||
|
|
||
| const hooks1 = createHooks() | ||
| startTabContext(hooks1) | ||
| const hooks2 = createHooks() | ||
| startTabContext(hooks2) | ||
|
|
||
| const event1 = hooks1.triggerHook(HookNames.Assemble, { startTime: 0 as RelativeTime }) | ||
| const event2 = hooks2.triggerHook(HookNames.Assemble, { startTime: 0 as RelativeTime }) | ||
|
|
||
| expect(event1.tab.id).toBe(event2.tab.id) | ||
| }) | ||
| }) |
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,44 @@ | ||
| import { HookNames } from '../../tools/abstractHooks' | ||
| import type { AbstractHooks } from '../../tools/abstractHooks' | ||
| import { generateUUID } from '../../tools/utils/stringUtils' | ||
|
|
||
| export const TAB_ID_STORAGE_KEY = '_dd_tab_id' | ||
|
|
||
| let cachedTabId: string | undefined | ||
|
|
||
| // Exported for test cleanup only — allows specs to reset the in-memory cache between tests. | ||
| export function resetCachedTabId(): void { | ||
| cachedTabId = undefined | ||
| } | ||
|
|
||
| export function startTabContext(hooks: AbstractHooks) { | ||
| const tabId = retrieveOrCreateTabId() | ||
|
|
||
| hooks.register(HookNames.Assemble, () => ({ | ||
| tab: { | ||
| id: tabId, | ||
| }, | ||
| })) | ||
| } | ||
|
|
||
| function retrieveOrCreateTabId(): string { | ||
| if (!cachedTabId) { | ||
| cachedTabId = getOrCreateIdInSessionStorage() ?? generateUUID() | ||
| } | ||
| return cachedTabId | ||
| } | ||
|
|
||
| function getOrCreateIdInSessionStorage(): string | undefined { | ||
| try { | ||
| const existingId = sessionStorage.getItem(TAB_ID_STORAGE_KEY) | ||
| if (existingId) { | ||
| return existingId | ||
| } | ||
| const newId = generateUUID() | ||
| sessionStorage.setItem(TAB_ID_STORAGE_KEY, newId) | ||
| return newId | ||
| } catch { | ||
| // sessionStorage unavailable (e.g. Web Worker, sandboxed iframe, quota exceeded) | ||
| return undefined | ||
| } | ||
| } | ||
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
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.