-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(browser): Add View Hierarchy integration #14981
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
timfish
wants to merge
18
commits into
develop
Choose a base branch
from
timfish/feat/browser-view-hierarchy
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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1c0b9ce
feat(browser): Add browser View Hierarchy integration
timfish df78783
Merge branch 'develop' into timfish/feat/browser-view-hierarchy
timfish 83c103a
Merge branch 'develop' into timfish/feat/browser-view-hierarchy
timfish 013d473
Merge branch 'develop' into timfish/feat/browser-view-hierarchy
timfish 16d9153
Changes after rcf merge
timfish d6ad717
Merge branch 'develop' into timfish/feat/browser-view-hierarchy
timfish 899e5c0
more
timfish 0b0bd5e
Merge remote-tracking branch 'upstream/develop' into timfish/feat/bro…
timfish 50629fb
fix linting
timfish 9aa2489
Add integration test
timfish e8a8d7b
Skip for cdn bundles
timfish f225a3b
add null check from PR review
timfish 61243cd
only run for non-cdn bundles
timfish 56e005c
lint
timfish e5768ca
Limit `getComponentName` traverse depth
timfish a733a15
Allow limiting traversal depth via `onElement` callback
timfish b065040
only capture for events
timfish 52ebf89
Merge branch 'develop' into timfish/feat/browser-view-hierarchy
timfish 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
9 changes: 9 additions & 0 deletions
9
dev-packages/browser-integration-tests/suites/integrations/viewHierarchy/init.js
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,9 @@ | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { viewHierarchyIntegration } from '@sentry/browser'; | ||
|
|
||
| window.Sentry = Sentry; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| integrations: [viewHierarchyIntegration()], | ||
| }); |
1 change: 1 addition & 0 deletions
1
dev-packages/browser-integration-tests/suites/integrations/viewHierarchy/subject.js
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 @@ | ||
| throw new Error('Some error'); |
11 changes: 11 additions & 0 deletions
11
dev-packages/browser-integration-tests/suites/integrations/viewHierarchy/template.html
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,11 @@ | ||
| <!doctype html> | ||
| <html> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <title></title> | ||
| </head> | ||
| <body> | ||
| <h1>Some title</h1> | ||
| <p>Some text</p> | ||
| </body> | ||
| </html> |
39 changes: 39 additions & 0 deletions
39
dev-packages/browser-integration-tests/suites/integrations/viewHierarchy/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,39 @@ | ||
| import { expect } from '@playwright/test'; | ||
| import type { ViewHierarchyData } from '@sentry/core'; | ||
| import { sentryTest } from '../../../utils/fixtures'; | ||
| import { getMultipleSentryEnvelopeRequests, envelopeParser } from '../../../utils/helpers'; | ||
|
|
||
| sentryTest('Captures view hierarchy as attachment', async ({ getLocalTestUrl, page }) => { | ||
| const bundle = process.env.PW_BUNDLE; | ||
| if (bundle != null && !bundle.includes('esm') && !bundle.includes('cjs')) { | ||
| sentryTest.skip(); | ||
| } | ||
|
|
||
| const url = await getLocalTestUrl({ testDir: __dirname }); | ||
|
|
||
| const [, events] = await Promise.all([ | ||
| page.goto(url), | ||
| getMultipleSentryEnvelopeRequests<ViewHierarchyData>( | ||
| page, | ||
| 1, | ||
| {}, | ||
| req => envelopeParser(req)?.[4] as ViewHierarchyData, | ||
| ), | ||
| ]); | ||
|
|
||
| expect(events).toHaveLength(1); | ||
| const event: ViewHierarchyData = events[0]; | ||
|
|
||
| expect(event.rendering_system).toBe('DOM'); | ||
| expect(event.positioning).toBe('absolute'); | ||
| expect(event.windows).toHaveLength(2); | ||
| expect(event.windows[0].type).toBe('h1'); | ||
| expect(event.windows[0].visible).toBe(true); | ||
| expect(event.windows[0].alpha).toBe(1); | ||
| expect(event.windows[0].children).toHaveLength(0); | ||
|
|
||
| expect(event.windows[1].type).toBe('p'); | ||
| expect(event.windows[1].visible).toBe(true); | ||
| expect(event.windows[1].alpha).toBe(1); | ||
| expect(event.windows[1].children).toHaveLength(0); | ||
| }); | ||
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 |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import type { Attachment, Event, EventHint, ViewHierarchyData, ViewHierarchyWindow } from '@sentry/core'; | ||
| import { defineIntegration, getComponentName } from '@sentry/core'; | ||
| import { WINDOW } from '../helpers'; | ||
|
|
||
| interface OnElementArgs { | ||
| /** | ||
| * The element being processed. | ||
| */ | ||
| element: HTMLElement; | ||
| /** | ||
| * Lowercase tag name of the element. | ||
| */ | ||
| tagName: string; | ||
| /** | ||
| * The component name of the element. | ||
| */ | ||
| componentName?: string; | ||
|
|
||
| /** | ||
| * The current depth of the element in the view hierarchy. The root element will have a depth of 0. | ||
| * | ||
| * This allows you to limit the traversal depth for large DOM trees. | ||
| */ | ||
| depth?: number; | ||
| } | ||
|
|
||
| interface Options { | ||
| /** | ||
| * Whether to attach the view hierarchy to the event. | ||
| * | ||
| * Default: Always attach. | ||
| */ | ||
| shouldAttach?: (event: Event, hint: EventHint) => boolean; | ||
|
|
||
| /** | ||
| * A function that returns the root element to start walking the DOM from. | ||
| * | ||
| * Default: `window.document.body` | ||
| */ | ||
| rootElement?: () => HTMLElement | undefined; | ||
|
|
||
| /** | ||
| * Called for each HTMLElement as we walk the DOM. | ||
| * | ||
| * Return an object to include the element with any additional properties. | ||
| * Return `skip` to exclude the element and its children. | ||
| * Return `children` to skip the element but include its children. | ||
| */ | ||
| onElement?: (prop: OnElementArgs) => Record<string, string | number | boolean> | 'skip' | 'children'; | ||
| } | ||
|
|
||
| /** | ||
| * An integration to include a view hierarchy attachment which contains the DOM. | ||
| */ | ||
| export const viewHierarchyIntegration = defineIntegration((options: Options = {}) => { | ||
| const skipHtmlTags = ['script']; | ||
|
|
||
| /** Walk an element */ | ||
| function walk(element: HTMLElement, windows: ViewHierarchyWindow[], depth = 0): void { | ||
| if (!element) { | ||
| return; | ||
| } | ||
|
|
||
| // With Web Components, we need to walk into shadow DOMs | ||
| const children = 'shadowRoot' in element && element.shadowRoot ? element.shadowRoot.children : element.children; | ||
|
|
||
| for (const child of children) { | ||
| if (!(child instanceof HTMLElement)) { | ||
| continue; | ||
| } | ||
|
|
||
| const componentName = getComponentName(child, 1) || undefined; | ||
| const tagName = child.tagName.toLowerCase(); | ||
|
|
||
| if (skipHtmlTags.includes(tagName)) { | ||
| continue; | ||
| } | ||
|
|
||
| const result = options.onElement?.({ element: child, componentName, tagName, depth }) || {}; | ||
|
|
||
| if (result === 'skip') { | ||
| continue; | ||
| } | ||
|
|
||
| // Skip this element but include its children | ||
| if (result === 'children') { | ||
| walk(child, windows, depth + 1); | ||
| continue; | ||
| } | ||
|
|
||
| const { x, y, width, height } = child.getBoundingClientRect(); | ||
|
|
||
| const window: ViewHierarchyWindow = { | ||
| identifier: (child.id || undefined) as string, | ||
| type: componentName || tagName, | ||
| visible: true, | ||
| alpha: 1, | ||
| height, | ||
| width, | ||
| x, | ||
| y, | ||
| ...result, | ||
| }; | ||
|
|
||
| const children: ViewHierarchyWindow[] = []; | ||
| window.children = children; | ||
timfish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Recursively walk the children | ||
| walk(child, window.children, depth + 1); | ||
|
|
||
| windows.push(window); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| name: 'ViewHierarchy', | ||
| processEvent: (event, hint) => { | ||
| // only capture for error events | ||
| if (event.type !== undefined || options.shouldAttach?.(event, hint) === false) { | ||
| return event; | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const root: ViewHierarchyData = { | ||
| rendering_system: 'DOM', | ||
| positioning: 'absolute', | ||
| windows: [], | ||
| }; | ||
|
|
||
| walk(options.rootElement?.() || WINDOW.document.body, root.windows); | ||
sentry[bot] marked this conversation as resolved.
Show resolved
Hide resolved
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const attachment: Attachment = { | ||
| filename: 'view-hierarchy.json', | ||
| attachmentType: 'event.view_hierarchy', | ||
| contentType: 'application/json', | ||
| data: JSON.stringify(root), | ||
| }; | ||
|
|
||
| hint.attachments = hint.attachments || []; | ||
| hint.attachments.push(attachment); | ||
|
|
||
| return event; | ||
| }, | ||
| }; | ||
| }); | ||
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.