chore: improve breadcrumbs in Sentry with providing more info - #864
Merged
Conversation
fredrikekelund
approved these changes
Jan 31, 2025
fredrikekelund
left a comment
Contributor
There was a problem hiding this comment.
The solution itself looks good to me 👍 As you mentioned, we can probably improve it in the future, but this is a good start. A few thoughts:
textContentwill depend on which translation is being used. Worth keeping in mind.- The parent element traversal can go all the way up to
<html>. Probably won't be an issue, though, especially since we clearly label it withparent-aria-labeletc.
There are two things with the code I think we should improve:
- Refine the comments
- "Unwrap" the code to reduce indentation
Here's a suggestion for what it could look like
// Enhances Sentry breadcrumb messages by extracting meaningful information from DOM elements.
const getExtraInformation = ( targetElement: HTMLElement ) => {
// Check for custom data-sentry attribute first, which is used for elements
// that need explicit tracking identification
const sentryData = targetElement.getAttribute( 'data-sentry' );
if ( sentryData ) {
return `[data-sentry="${ sentryData }"]`;
}
// Skip adding extra context if aria-label is present, as it already provides
// sufficient identification for both tracking and accessibility
if ( targetElement.getAttribute( 'aria-label' ) ) {
return '';
}
// Fall back to the element's text content if available
const textContent = targetElement.textContent?.trim() || targetElement.innerText?.trim();
if ( textContent ) {
return `[text-content="${ textContent }"]`;
}
// If no identifying information is found on the target element,
// traverse up the DOM tree looking for identifiable parent elements.
// This helps with SVG icons or other elements wrapped in interactive parents.
let element = targetElement.parentElement;
while ( element ) {
const ariaLabel = element.getAttribute( 'aria-label' );
const sentryData = element.getAttribute( 'data-sentry' );
const textContent = element.textContent?.trim() || element.innerText?.trim();
if ( ariaLabel ) {
return `[parent-aria-label="${ ariaLabel }"]`;
}
if ( sentryData ) {
return `[parent-data-sentry="${ sentryData }"]`;
}
if ( textContent ) {
return `[parent-text-content="${ textContent }"]`;
}
element = element.parentElement;
}
return '';
};
Sentry.init(
{
debug: true,
beforeBreadcrumb( breadcrumb, hint ) {
if ( breadcrumb.category !== 'ui.click' ) {
return breadcrumb;
}
const targetElement = hint?.event?.target;
if ( targetElement ) {
breadcrumb.message = ( breadcrumb.message || '' ) + getExtraInformation( targetElement );
}
return breadcrumb;
},
},
reactInit
);
Contributor
|
We could also rename |
sejas
reviewed
Jan 31, 2025
sejas
left a comment
Member
There was a problem hiding this comment.
Thanks for improving the Sentry logs.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Related issues
Proposed Changes
Sometimes in Sentry breadcrumbs it's not possible to understand which exactly element was clicked, either due to no selectors on element or tailwind classes which are not unique so easy to mix up buttons. Example:
Check out what is the result of the proposed changes. I think, as a first option for the nearest release is a good start and maybe in the future we will improve it, in the next releases.
Testing Instructions
4.1
4.2
4.3 Not big benefit but easier to understand at a glance:
4.4 Not perfect, but still very helpful:
4.5
4.6 Test as much as possible