-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(core): Add data-sentry-label support in htmlTreeAsString #18398
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
base: develop
Are you sure you want to change the base?
feat(core): Add data-sentry-label support in htmlTreeAsString #18398
Conversation
|
We have a solution for this already: https://docs.sentry.io/platforms/javascript/guides/react/features/component-names/ You can set <div
data-sentry-component="MyAwesomeComponent"
data-sentry-source-file="myAwesomeComponent.jsx"
>
This is a really cool and awesome component!
</div>If you're using React Native, this is bundled in by default: https://docs.sentry.io/platforms/react-native/integrations/component-names/. const { getDefaultConfig } = require("@react-native/metro-config");
const { withSentryConfig } = require("@sentry/react-native/metro");
const config = getDefaultConfig(__dirname);
module.exports = withSentryConfig(config, {
annotateReactComponents: true,
}); |
|
Do you have any ideas on how we can improve the behavior regarding which attribute values are added to HTML? |
Is the issue that you want to control what components get the attribute value? Can you set custom one's yourself? Or is it that for some components the injected component name is too generic so you actually lose information by enabling |
|
Instead of using |
Unfortunately, adding this attribute to all elements seems impossible. If I don't, I end up with the following example:
In this example I will never get the component information because the div will hit the 80 character limit via the css selector. <button data-sentry-component="DetailsButton">
<div class="css-view-g5y9jx r-WebkitUserSelect-9ffhgg r-alignItems-1awozwy r-backgroundColor-18lll9h r-flexDirection-18u37iz r-justifyContent-1h0z5md r-paddingBottom-kzbkwu r-userSelect-lrvibr r-width-13qz1uu r-paddingBlock-1mmae3n r-paddingInline-1fkl15p">
Text
</div>
</button>That's why it is so important for me to do the bypass:
|



Summary
This PR adds support for the
data-sentry-labelattribute inhtmlTreeAsStringfunction, allowing developers to annotate DOM elements with stable identifiers for better breadcrumb and INP span identification.This is a partial solution to getsentry/sentry#104128.
Changes
data-sentry-labelattribute detection in_htmlElementAsString()- takes priority overdata-sentry-componentanddata-sentry-element_getSentryLabel()helper function that traverses up to 15 DOM levels to find the nearestdata-sentry-labelattributedata-sentry-labelis found on an ancestor (beyond the standard 5-level traversal), it prefixes the CSS selector:[data-sentry-label="ProductCard"] div.container > button.btnWhy this helps
In React Native Web and other frameworks with long/generated CSS class names, the current 80-character limit causes selectors to be truncated immediately, producing highly ambiguous selectors that can match 100+ elements on a page.
With
data-sentry-label, developers can annotate interactive elements with stable identifiers:Before (current behavior):
This selector hits the 80-character limit immediately. If these classes represent common text styling, the selector matches every text element with that styling across the entire application.
After (with this PR):
The label prefix identifies which specific row the interaction occurred in.
This significantly improves the precision and usefulness of generated CSS selectors in INP spans.
Checklist
Before submitting a pull request, please take a look at our
Contributing guidelines and verify:
yarn lint) & (yarn test).