Skip to content

Commit f433a55

Browse files
authored
feat(tracing): Send component name on interaction spans (#9948)
One of the PRs scoped from #9855 Sends component names on the databag of interaction spans
1 parent de6d41a commit f433a55

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

packages/browser-integration-tests/suites/tracing/browsertracing/interactions/assets/script.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ const delay = e => {
1414
};
1515

1616
document.querySelector('[data-test-id=interaction-button]').addEventListener('click', delay);
17+
document.querySelector('[data-test-id=annotated-button]').addEventListener('click', delay);

packages/browser-integration-tests/suites/tracing/browsertracing/interactions/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<body>
77
<div>Rendered Before Long Task</div>
88
<button data-test-id="interaction-button">Click Me</button>
9+
<button data-test-id="annotated-button" data-sentry-component="AnnotatedButton">Click Me</button>
910
<script src="https://example.com/path/to/script.js"></script>
1011
</body>
1112
</html>

packages/browser-integration-tests/suites/tracing/browsertracing/interactions/test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,35 @@ sentryTest(
8080
}
8181
},
8282
);
83+
84+
sentryTest(
85+
'should use the component name for a clicked element when it is available',
86+
async ({ browserName, getLocalTestPath, page }) => {
87+
const supportedBrowsers = ['chromium', 'firefox'];
88+
89+
if (shouldSkipTracingTest() || !supportedBrowsers.includes(browserName)) {
90+
sentryTest.skip();
91+
}
92+
93+
await page.route('**/path/to/script.js', (route: Route) =>
94+
route.fulfill({ path: `${__dirname}/assets/script.js` }),
95+
);
96+
97+
const url = await getLocalTestPath({ testDir: __dirname });
98+
99+
await page.goto(url);
100+
await getFirstSentryEnvelopeRequest<Event>(page);
101+
102+
await page.locator('[data-test-id=annotated-button]').click();
103+
104+
const envelopes = await getMultipleSentryEnvelopeRequests<TransactionJSON>(page, 1);
105+
expect(envelopes).toHaveLength(1);
106+
const eventData = envelopes[0];
107+
108+
expect(eventData.spans).toHaveLength(1);
109+
110+
const interactionSpan = eventData.spans![0];
111+
expect(interactionSpan.op).toBe('ui.interaction.click');
112+
expect(interactionSpan.description).toBe('body > AnnotatedButton');
113+
},
114+
);

packages/tracing-internal/src/browser/metrics/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable max-lines */
22
import type { IdleTransaction, Transaction } from '@sentry/core';
33
import { getActiveTransaction } from '@sentry/core';
4-
import type { Measurements } from '@sentry/types';
5-
import { browserPerformanceTimeOrigin, htmlTreeAsString, logger } from '@sentry/utils';
4+
import type { Measurements, SpanContext } from '@sentry/types';
5+
import { browserPerformanceTimeOrigin, getComponentName, htmlTreeAsString, logger } from '@sentry/utils';
66

77
import { DEBUG_BUILD } from '../../common/debug-build';
88
import {
@@ -102,13 +102,20 @@ export function startTrackingInteractions(): void {
102102
const startTime = msToSec((browserPerformanceTimeOrigin as number) + entry.startTime);
103103
const duration = msToSec(entry.duration);
104104

105-
transaction.startChild({
105+
const span: SpanContext = {
106106
description: htmlTreeAsString(entry.target),
107107
op: `ui.interaction.${entry.name}`,
108108
origin: 'auto.ui.browser.metrics',
109109
startTimestamp: startTime,
110110
endTimestamp: startTime + duration,
111-
});
111+
};
112+
113+
const componentName = getComponentName(entry.target);
114+
if (componentName) {
115+
span.data = { 'ui.component_name': componentName };
116+
}
117+
118+
transaction.startChild(span);
112119
}
113120
}
114121
});

0 commit comments

Comments
 (0)