Skip to content
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(breadcrumbs): Send component names on UI breadcrumbs #9946

Merged
merged 5 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<body>
<button id="button1" type="button">Button 1</button>
<button id="button2" type="button">Button 2</button>
<button id="annotated-button" type="button" data-sentry-component="AnnotatedButton">Button 3</button>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,39 @@ sentryTest('captures Breadcrumb for clicks & debounces them for a second', async
},
]);
});

sentryTest(
'uses the annotated component name in the breadcrumb messages and adds it to the data object',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

await page.route('**/foo', route => {
return route.fulfill({
status: 200,
body: JSON.stringify({
userNames: ['John', 'Jane'],
}),
headers: {
'Content-Type': 'application/json',
},
});
});

const promise = getFirstSentryEnvelopeRequest<Event>(page);

await page.goto(url);
await page.click('#annotated-button');
await page.evaluate('Sentry.captureException("test exception")');

const eventData = await promise;

expect(eventData.breadcrumbs).toEqual([
{
timestamp: expect.any(Number),
category: 'ui.click',
message: 'body > AnnotatedButton',
data: { componentName: 'AnnotatedButton' },
},
]);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<body>
<input id="input1" type="text" />
<input id="input2" type="text" />
<input id="annotated-input" data-sentry-component="AnnotatedInput" type="text" />
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,48 @@ sentryTest('captures Breadcrumb for events on inputs & debounced them', async ({
},
]);
});

sentryTest(
'includes the annotated component name within the breadcrumb message and data',
async ({ getLocalTestUrl, page }) => {
const url = await getLocalTestUrl({ testDir: __dirname });

await page.route('**/foo', route => {
return route.fulfill({
status: 200,
body: JSON.stringify({
userNames: ['John', 'Jane'],
}),
headers: {
'Content-Type': 'application/json',
},
});
});

const promise = getFirstSentryEnvelopeRequest<Event>(page);

await page.goto(url);

await page.click('#annotated-input');
await page.type('#annotated-input', 'John', { delay: 1 });

await page.evaluate('Sentry.captureException("test exception")');
const eventData = await promise;
expect(eventData.exception?.values).toHaveLength(1);

expect(eventData.breadcrumbs).toEqual([
{
timestamp: expect.any(Number),
category: 'ui.click',
message: 'body > AnnotatedInput',
data: { componentName: 'AnnotatedInput' },
},
{
timestamp: expect.any(Number),
category: 'ui.input',
message: 'body > AnnotatedInput',
data: { componentName: 'AnnotatedInput' },
},
]);
},
);
29 changes: 18 additions & 11 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
IntegrationFn,
} from '@sentry/types';
import type {
Breadcrumb,
FetchBreadcrumbData,
FetchBreadcrumbHint,
XhrBreadcrumbData,
Expand All @@ -24,6 +25,7 @@ import {
addFetchInstrumentationHandler,
addHistoryInstrumentationHandler,
addXhrInstrumentationHandler,
getComponentName,
getEventDescription,
htmlTreeAsString,
logger,
Expand Down Expand Up @@ -133,6 +135,7 @@ function _getDomBreadcrumbHandler(
}

let target;
let componentName;
let keyAttrs = typeof dom === 'object' ? dom.serializeAttribute : undefined;

let maxStringLength =
Expand All @@ -155,6 +158,7 @@ function _getDomBreadcrumbHandler(
target = _isEvent(event)
? htmlTreeAsString(event.target, { keyAttrs, maxStringLength })
: htmlTreeAsString(event, { keyAttrs, maxStringLength });
componentName = _isEvent(event) ? getComponentName(event.target) : getComponentName(event);
0Calories marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
target = '<unknown>';
}
Expand All @@ -163,17 +167,20 @@ function _getDomBreadcrumbHandler(
return;
}

addBreadcrumb(
{
category: `ui.${handlerData.name}`,
message: target,
},
{
event: handlerData.event,
name: handlerData.name,
global: handlerData.global,
},
);
const breadcrumb: Breadcrumb = {
category: `ui.${handlerData.name}`,
message: target,
};

if (componentName) {
breadcrumb.data = { componentName };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call this ui.component_name and align it with the other setting of data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do 👍

}

addBreadcrumb(breadcrumb, {
event: handlerData.event,
name: handlerData.name,
global: handlerData.global,
});
};
}

Expand Down