-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(browser): Extract and send frontend component name when available #9855
Closed
Closed
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9f52710
Iterate on method for retrieving component names and send it in bread…
0Calories 4b1418a
Add componentName in breadcrumb data
0Calories 4fb6c09
Track data-sentry-component attribute for replay breadcrumbs
0Calories 9a2e53c
Document the function
0Calories 3fe57fa
Add component name to interaction span databag
0Calories 873820f
Add component names to react profiler span databag
0Calories 665fd79
Add return type to getComponentName
0Calories 6fe6746
Run Biome formatter
0Calories ca37bcc
Add HTMLElement type to tests
0Calories 526c67d
Adjust tests to work with the new iteration
0Calories 41d9889
Adjust React profiler tests
0Calories 62eb261
Add integration test for replays
0Calories 365f24d
remove unnecessary null assignment
0Calories 0507f8c
Merge branch 'develop' into feat/component-names
0Calories 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 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 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 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 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 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 |
---|---|---|
|
@@ -6,6 +6,10 @@ const WINDOW = getGlobalObject<Window>(); | |
|
||
const DEFAULT_MAX_STRING_LENGTH = 80; | ||
|
||
type SimpleNode = { | ||
parentNode: SimpleNode; | ||
} | null; | ||
|
||
/** | ||
* Given a child DOM element, returns a query-selector statement describing that | ||
* and its ancestors | ||
|
@@ -16,9 +20,6 @@ export function htmlTreeAsString( | |
elem: unknown, | ||
options: string[] | { keyAttrs?: string[]; maxStringLength?: number } = {}, | ||
): string { | ||
type SimpleNode = { | ||
parentNode: SimpleNode; | ||
} | null; | ||
|
||
if (!elem) { | ||
return '<unknown>'; | ||
|
@@ -86,6 +87,11 @@ function _htmlElementAsString(el: unknown, keyAttrs?: string[]): string { | |
return ''; | ||
} | ||
|
||
// If using the component name annotation plugin, this value may be available on the DOM node | ||
if (elem instanceof HTMLElement && elem.dataset && elem.dataset['sentryComponent']) { | ||
return elem.dataset['sentryComponent'] | ||
} | ||
|
||
out.push(elem.tagName.toLowerCase()); | ||
|
||
// Pairs of attribute keys defined in `serializeAttribute` and their values on element. | ||
|
@@ -157,3 +163,29 @@ export function getDomElement<E = any>(selector: string): E | null { | |
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Given a DOM element, traverses up the tree until it finds the first ancestor node | ||
* that has the `data-sentry-component` attribute. This attribute is added at build-time | ||
* by projects that have the component name annotation plugin installed. | ||
* | ||
* @returns a string representation of the component for the provided DOM element, or `null` if not found | ||
*/ | ||
export function getComponentName(elem: unknown): string | null { | ||
let currentElem = elem as SimpleNode; | ||
const MAX_TRAVERSE_HEIGHT = 5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 5 might be a bit too high, but I'm unsure. We could lower this threshold to start out and gradually increase it if necessary, or keep it at 5 |
||
|
||
for (let i = 0; i < MAX_TRAVERSE_HEIGHT; i++) { | ||
if (!currentElem) { | ||
return null; | ||
} | ||
|
||
if (currentElem instanceof HTMLElement && currentElem.dataset['sentryComponent']) { | ||
return currentElem.dataset['sentryComponent']; | ||
} | ||
|
||
currentElem = currentElem.parentNode | ||
} | ||
|
||
return null; | ||
} |
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.
This line is unnecessary